A simple Flask hello world application.
Create a project folder and create a virtual environment and activate it.
mkdir flask-hello-world && cd $_
python3 -m venv .venv
source .venv/bin/activate
First create a file to save dependencies (only Flask dependency in this case).
The must common name for this file is requirements.txt
, so:
touch requirements.txt
Now you need to define your dependencies, and the file looks like this:
flask==1.1.2
You can check Flask releases in: https://github.com/pallets/flask/releases.
To install it, you just to need run:
pip install -r requirements.txt
Now, you can check installed dependencies (Flask and its dependencies).
pip freeze
And the output should be like this:
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1
WOW Flask has very few dependencies to be a web application framework,
it is because Flask is a microframework
Create a main file call main.py
(The name does not matter, it could be another).
touch main.py
And then put the next code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello world with Flask'
To run your app, first you need to set a environment variable called FLASK_APP
and its value is the name of your file where your app is (main.py
in this case).
Optionally you can set the debug mode for developing suing FLASK_DEBUG
env var to 1.
export FLASK_APP=main.py
export FLASK_DEBUG=1
Check the env var:
echo $FLASK_APP
$ main.py
And then you can run your app:
flask run
And it outputs something like:
* Serving Flask app "main.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
By default Flask app run a server in the 5000 port in localhost.
Now you can send a request to your app:
curl http://127.0.0.1:5000/
The output should be like:
Hello world with Flask
To stop your new app, just press CTRL + c
MIT. Copyright (c)