Web frameworks

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.

Common features

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.

Support for templating engines

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.

Support for templating engines

Once we've set this, we need to set the templating language as well.

Support for templating engines

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.

Support for templating engines

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.

Support for templating engines

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.

Customized project creation

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.

Customized project creation

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.

Debugging in templates

PyCharm allows you to set breakpoints inside our templates. This means that when we are debugging (not just running) our application, PyCharm will stop at the template's breakpoint. We don't need to do anything other than click on the line on the left-hand side to enable a breakpoint.

Debugging in templates

Django

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.

Setting up 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.

Setting up Django

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.

Setting up Django

Model dependency diagrams

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.

Model dependency diagrams

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).

Model dependency diagrams

This will generate a model dependency diagram, but only for the model Post in the preceding example.

Model dependency diagrams

Manage.py tasks

PyCharm allows you to quickly execute manage.py tasks from a window (Alt + R).

Manage.py tasks

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.

Django Console

Django Console

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.12.172