PyCharm supports a wide range of Python frameworks. All the frameworks share common features such as setting the templating directory and mapping views to those templates; however, they also have their own unique feature sets, such as Django having very good code completion for its ORM. In the following subtopics, we will discuss common and specialized features for the different frameworks that PyCharm supports.
Although SQLAlchemy is not a web framework, PyCharm supports it just like it supports Django's ORM, giving you good code completion and being able to generate model dependency diagrams.
All the frameworks share some common features such as project creation. Here are a few of the common features that will help you with development in any framework.
Most frameworks will serve templated files. PyCharm supports a multitude of Python-based templating engines. Setting the templating engine and the template
folder allows us to get code completion inside our templates. We can demonstrate this by first creating a Flask (or any other framework, for that matter) project. All we need to create a Flask project is an app.py
file like this:
from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "Hello World!" if __name__ == '__main__': app.run()
The preceding code is very simple. It creates a simple Flask application using Flask(__name__)
, then assigns it to the variable app
, registers a route for the /
path, and finally, runs it using app.run()
. The route
function simply returns Hello World
. We are going to change the index
function to render a template. The first thing we're going to do is create Template Folder in our current directory and mark it as a template
directory.
Once we've set this, we need to set the templating language as well.
Flask uses Jinja2 by default, but we can use other templating languages as well such as Mako. We can now create an index.html
file and get code completion for variables, but before we do that, let's modify our index
function to render a template.
In the preceding example, we are using render_template
to render the Jinja2 template file in question. Once we input index.html
as our first parameter, PyCharm understands that such a template file does not exist in our templates
directory and allows us to use a quick-fix for it (Alt + Enter). If we choose the Create template 'index.html' option, PyCharm will create the index.html
file inside our Template Folder.
Furthermore, this template file will be linked to our view file and we will be able to get code completion for the variables we pass onto the template. In this case, we can see that message is popping up as an option when we type in m
.
PyCharm allows us to create projects for the different frameworks, with boilerplate code. For example, if we choose to create a Pyramid by navigating to File | New Project… | Pyramid, we will see a lot of customized options in More Settings.
This means that PyCharm will automatically set Templates folder, create all the boilerplate files and download as well as install all the libraries necessary for the project we wish to create. This is very useful for quickly creating a project, especially on a Windows system.
If we were to create a Google App Engine application, the project creator would ask for our app ID in the creation window. All the Project Creators are customized to their individual frameworks with some common options such as the templates folder.
However, it is worth noting that all this will create files and install packages for you; it does not mean that the files themselves will be adapted to our choices. For example, if we were to create a Flask project using Mako as our templating engine, it would not mean that Flask would render Mako templates, it would still render using the default Jinja2 templating engine. We would still need to configure Flask to use Mako as the templating engine instead of the default one.
Django is by far the best supported Python framework in PyCharm. Most of the support lies in code completion (and will therefore become self-evident), so this section looks at the tooling for Django.
For a Django project that was not created by the PyCharm project creator, we have a little bit of configuration that we need to take care of.
First off, if we have an old Django project, PyCharm will offer to convert it.
If we click on Details…, we will get a list of all the things that will be changed.
However, if PyCharm cannot recognize the project as a Django project, we need to point PyCharm to the correct locations inside Languages & Frameworks | Django.
PyCharm allows us to take a look at how Django models are related. All we need to do is right-click on any Django model to see the models inside a certain package.
This will show the dependency diagram for all the Django models that will be created using Python manage.py
migrate for that particular package. We can even generate diagrams for individual models (by right-clicking on the relevant class and then selecting the visualization option that we want).
This will generate a model dependency diagram, but only for the model Post
in the preceding example.
PyCharm allows you to quickly execute manage.py
tasks from a window (Alt + R).
In my own development, I almost never use this. I instead opt to use the command line and run the manage.py
script myself. However, this can be very useful for Windows systems since you don't have handy tools such as workon, which automatically links your virtualenv to your project.
Whenever we open up the console for a Django project, we are going to get a specialized console for Django that is essentially a PyCharm version of the manage.py
shell. What this basically means is that we get all the benefits of using manage.py's shell command as well as the code completion in the console session. We can even modify it inside the Django Console settings. In the previous versions of PyCharm, the Django Console was a separate console to the Python console, but in the newer versions, they have been merged.