How to learn Flask

How to learn Flask

How to learn Flask. Starting with this lesson, we will get acquainted with the very popular Flask framework, which allows you to create sites of various types using the Python language. Flask belongs to the category of micro-frameworks, that is, it provides only basic tools for building sites, as they say, everything is minimal – nothing superfluous. However, this is enough to create most typical sites with support for page templates, databases, and other standard features. And, in addition, the simplicity allows you to quickly understand this package and start using it immediately. Probably, this is its main feature – simplicity and sufficient functionality for standard solutions.

To study this package on your home PC, you only need to install it using the pip installer:

pip install Flask

Now we can write our first WSGI application. In the simplest version, it looks like this:

from flask import Flask
 
app = Flask(__name__)
 
if __name__ == "__main__":
   app.run(debug=True)

First, we import the Flask class, which actually forms this application. Next, we create an instance of this class and use the first argument to specify the name of our application. If the entire program is written in a single file, pass the __name__ Directive, which will contain the name of the current file in the case of import, and the value “__main__”in the case of self – launch. For Flask, this is of fundamental importance, in particular, it depends on where to look for subdirectories with templates and static documents.

After that, the framework is launched using the run method and debug=True is specified as the parameter, so that we can see all the errors that will occur during the development of the site application in the browser. Of course, after creating it, you should specify debug=False here, so that the real user will not see random errors.

And finally, the condition. What is it for? See, when we directly launch our module, the __name__ Directive will take the value “_ _ main__ ” and the local web server will be launched to debug the current application. If the module is launched, for example, on a remote server, we don’t need to launch another server. In this case, the __name__ Directive will accept the name of this module and the app. run line will not be executed. In other words, we set this condition in order to launch the app directly on the local device.

Accordingly, as soon as the framework is launched, our local web server is activated and we can create a request in the browser using this initial address:

http://127.0.0.1:5000/

Let’s do this and see what happens. Enter the specified request in the browser and see that the requested page was not found. That’s right, and it should be, since we haven’t created any views in the program yet. Let’s do this and add it:

@app.route("/")
def index():
    return "index"

Here we use a special route decorator that creates a wrapper around our index function, which will be activated when accessing the main page of the site, that is, on request http://127.0.0.1:5000/

Run the program, refresh the page, and now in the browser we see what the index function returned: index.

To display the same page at multiple URLS, add multiple route constructs:

@app.route("/index")
@app.route("/")
def index():
    return "index"

Accordingly, for any other address, we can also add our own separate handler by adding another route decorator:

@app.route("/about")
def about():
    return "<h1>О сайте</h1>"

Now our site has two pages: home and /about – about the site. Moreover, our handlers return an HTML document and all tags will be displayed on the page in the browser accordingly.