Change skeleton templates in PyCharm

2019-07-06 20:52发布

is there a way I can change the main skeleton templates in PyCharm?

Whenever I start a new (for example) Flask project I have to alter the skeleton to fit my needs. It would be great if I could say PyCharm to start a new project with a static folder and a template folder, a specific base.html, database template and so on...

Is this somehow possible?

Adding a new skeleton or alter an existing one would be what I need. Any ideas?

标签: pycharm
2条回答
地球回转人心会变
2楼-- · 2019-07-06 21:05

This kind of customization is only possible by writing a plugin. The only thing that can be changed without writing a plugin is the main.py that PyCharm generates: it can be changed under Settings | File Templates | Flask Main.

查看更多
Viruses.
3楼-- · 2019-07-06 21:05

I just took a look at the plugin directory, and I think it's probably possible right now, although not for the faint of heart.

Look in PyCharm/plugins/pycharm-flask/ and it looks like there's a jar with the template files in it:

bloop:s r$ jar xvf resources_en.jar
  created: META-INF/
extracted: META-INF/MANIFEST.MF
  created: fileTemplates/
  created: fileTemplates/internal/
extracted: fileTemplates/internal/Flask Main.py.ft

bloop:s r$ cat fileTemplates/internal/Flask\ Main.py.ft
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

I replaced the Flask\ Main.py.ft file and it worked for me. Here's how I did it:

I closed PyCharm entirely, no open windows, no running processes, then in a shell:

$ cd /Applications/PyCharm.app/plugins/pycharm-flask/lib
$ cp resources_en.jar backup
$ mkdir -p fileTemplates/internal
$ cat >fileTemplates/internal/Flask\ Main.py.ft
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello Custom World!'

if __name__ == '__main__':
    app.run()
^D
$ jar -uf resources_en.jar fileTemplates/internal/Flask\ Main.py.ft

Then started up PyCharm and started a new project, specifying Flask as the type:

And when it opened up:

enter image description here

For what it's worth, this method is an extremely hacky means to an end. If you just want to change the default main, this works in a pinch if you're not picky.

If you want to do it the pro way, you can get the source code for the Flask plugin from the Intellij Plugin GIT. If you follow the instructions on Jetbrain's webpage they explain how to download the community version of Intellij and how to build and package plugins.

查看更多
登录 后发表回答