To learn more about the concepts covered in this lesson, you can check out:
Creating the Flask Application
00:00 Creating the Flask Application. In this section, you’ll learn how to create a Python Flask example application and run it locally. You’re going to initialize the project, install Flask, create the application, and run it on your computer.
00:16 You’ll also learn how to use Git to version your application’s code. The project initialization consists of creating a directory for your application, setting up a Python virtual environment where dependencies will be installed, and initializing the Git repository.
00:33 You don’t have to use a virtual environment or Git for local development, but they are very convenient and will make development and deployment to Heroku simpler. For more on virtual environments, check out this Real Python course.
Start by creating a new directory for the Python Flask example app. You can do it by running these commands seen on-screen. Here you create a
realpython-flask-app folder, and then change the current working directory to that folder.
These commands create a virtual environment named
venv and activate it so packages will be loaded and installed from this environment instead of using the system-level packages. The next step is to install Flask.
requirements.txt when deploying the project to tell Heroku which packages must be installed to run your application’s code. Now the application directory should look as seen on-screen.
Next, you’ll create a small Flask application with a single route,
/ (index), that returns the text
"Hello World!" when requested. To create a Flask application, you have to create a Flask object that represents your app and then associate views to routes.
03:10 Flask takes care of dispatching incoming requests to the correct view based on the request URL and the routes that you’ve defined. For small applications like the one you’re working on within this course, you can write all the code in a single file, organizing your project as seen on-screen.
The view function
index() is linked to the main route using the
@app.route decorator. When the main route is requested, Flask will serve the request by calling
index() and using its returned value as the response.
There are different ways in which you can run this application. One of the most straightforward ways to launch a Flask app for local development is using the
flask run command from the terminal, as seen on-screen.
You’ll see a web page containing the message
"Hello World!", as you see on-screen. During development, you’d normally want to reload your application automatically whenever you make a change to it. You can do this by setting an environment variable,
You can now run Flask again, and you will see that the environment has changed. When you set
development, Flask will monitor changes to the app files and reload the server whenever there’s a change. This way, you don’t need to manually stop and restart the app server after each modification. In this course, you are going to track the changes to your project’s files using Git, a popular version control system. You’ll need to install Git, and the method of installation will vary depending on which platform you’re running on. Follow the link on-screen and install Git for your operating system.
Once Git is installed, as a first step, you should create a Git repository for your project. You can achieve this by executing the following command in your project’s directory. This command initializes the repository that will be used to track the project’s files. The repository metadata is stored in a hidden directory named
Note that there are some folders that you shouldn’t include in the Git repository, such as the virtual environment and
__pycache__/. You can tell Git to ignore them by creating a file named
.gitignore and adding the folders to it, as seen on-screen.
Become a Member to join the conversation.