The first Flask project

The first Flask project

Introduction

Suppose you already know how to write some code with Flask. If not, read the article «Flask basics»

You can create some .py file and run it with the command python example.py. Everything works and, opening 127.0.0.1:5000 in a browser or making a request to your REST API, you see the expected result.

This is a good start, but so far only the first step towards creating a Flask application

FLASK_APP

To show Flask which file contains the application, you need to assign the correct value to the variable FLASK_APP.

Suppose you are making the site TopBicycle.ru and the code of your application is saved as topBicycle.py

If you are on Windows execute

set FLASK_APP=topBicycle.py

Or export for Linux

export FLASK_APP=topBicycle.py

Without spaces around =

For the duration of application development, assign the value development to the variable FLASK_ENV

set FLASK_ENV=development

Or export for Linux

export FLASK_ENV=development

development will make development easier for us; for example, in this mode Flask will watch for source code changes and restart the server automatically.

Now you can go to the folder with your .py file and run flask run to start the application

flask run

(topBicycle_venv) C:\Users\Andrei\PycharmProjects\topBicycle>flask run * Serving Flask app "topBicycle.py" (lazy loading) * Environment: development * Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 131-857-324 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Viewing all methods

To see which urls your application listens on and which HTTP methods each url currently supports, you can switch to interactive mode and use .url.map

CTRL + C python import topBicycle topBicycle.app.url_map

Map([<Rule '/' (OPTIONS, GET, HEAD) -> welcome>, <Rule '/static/<filename>' (OPTIONS, GET, HEAD) -> static>])

All served urls are shown, in our case one /, the function names and the allowed methods.

Example

As the first project we will make a simple application that shows static html on the home page and on the page /count_views will keep count of visits

Let us call the file project.py

from flask import Flask, render_template, abort, jsonify, request, redirect, url_for app = Flask(__name__) @app.route("/") def home(): return render_template("home.html") counter = 0 @app.route("/count_views/") def count_views(): global counter counter += 1 return "This page was served " + str(counter) + " times"

In the folder with project.py you need to create the templates folder and in it create some simple .html file and call it home.html

Also do not forget about export or set if you are on Windows

export FLASK_APP=project.py

Now at the address

127.0.0.1:5000/

there will be your home.html file and at the address

127.0.0.1:5000/count_views/

there will be the visit counter as text

This page was served X times

When the project is ready, you need to use Freeze to save the list of dependencies to the requirements.txt file

python -m pip freeze > projects/your_project/requirements.txt

This requirements.txt file will have to be sent to the server for deployment or simply saved so that you can return to working on the project later

General rules for creating Flask projects

Let us create the folder in the project static and put the image flasklogo.png there

Let us create the folder templates and inside this folder the files home.html and base.html

base.html will be the template that we will use for home.html and many other files that we will create in the future.

We will study working with templates in the next lesson, «Jinja templates»

↓ Articles about Flask ↓
Python
Flask
Running Flask on hosting
Running Flask on a Linux server
The first Flask project
Jinja templates
Flask FAQ
Web Forms
Errors

Article author: Andrei Olegovich