Flask is a lightweight framework for quick web application development in Python. This shot aims to teach you how to quickly bootstrap a basic app with it.
Let’s get started.
Note: For this exercise, make sure you have Python 3 installed on your computer.
First thing first, let’s see how to work with Flask the right way.
mkdir flask101 # move to the newly created folder cd flask101
The virtual environment is important, as it helps avoid packages conflicts within your
venv module to create a lightweight virtual environment.
Now, within our project folder, let’s run the following.
python3 -m venv .venv
This will create the target directory (
.venv). This will house everything related to our virtual environment in the project, like the
Scripts in Windows), and
Lib in Windows).
Notice that while my target directory is named
.venv, you can name it as you wish.
The next step before we work on our project is to activate the corresponding environment.
# GNU/Linx || MacOS . venv/bin/activate # Windows venv\Scripts\activate
Once you run the command above, your shell prompt will change to show the name of the activated environment; in our case,
- If you want to quit the virtual environment, you need to type
- If running scripts is disabled on Windows due to the execution policy, open
Powershellas administrator and run this command:
Y) when it asks for confirmation.
Last but not least, you can now install our framework within the activated environment. To do so, use the following command.
pip install Flask
That is how you set up a working environment to work with Flask.
Let’s now learn how to say “Hello, world!” in Flask.
Make sure you’re in the activated environment and create a file called
Within the file, add the following code.
from flask import Flask app = Flask(__name__) @app.route("/") def hello_world(): return "<p>Hello, World!</p>"
This code snippet is what a minimal Flask app looks like. We first import the
Flask class from the
flask module and then create an
__name__, a convenient shortcut for the name of the application’s module or package.
Next, we set a route (
/) to tell Flask what URL should trigger our function (
hello_world()). Our function returns the message we want to display in the user’s browser.
Warning: Do not call your application
flask.py. This will conflict with Flask itself.
It is not difficult to run the app. You can use either
flask or the
python -m flask command, like this:
Running this command will throw an error. To fix it, we need to export the
FLASK_APP environment variable to tell our terminal which application to work with.
# Bash export FLASK_APP=hello # CMD set FLASK_APP=hello
Now, we can run our app and enjoy the result on the browser:
flask run * Running on http://127.0.0.1:5000/
Our application responds on the port
Flask is a popular framework used for web development in Python. To get started with it, make sure you have a convenient isolated environment for your project; you can use the
venv module for this. After you create a minimal Flask app by importing the Flask class, you have to export the
FLASK_APP environment variable before running the app with
View all Courses