Follow the step-by-step instructions on Real Python.
You can run the provided example project on your local machine by following the steps outlined below.
Create a new virtual environment:
python3 -m venv venv/
Activate the virtual environment:
source ./venv/bin/activate
Navigate to the folder for the step that you're currently on.
Install the dependencies for this project if you haven't installed them yet:
(venv) $ python -m pip install -r requirements.txt
Next, you need to install Playwright:
(venv) $ playwright install
Finally, run the Flask development server
(venv) $ python -m flask run
Now you can navigate to the address that's shown in the output when you start the server. Commonly, that's http://localhost:5000/
.
If you want to deploy your Flask app later, then it's a good idea to generate a proper secret key.
If you need to create cryptographically sound data like a Flask secret key, then you can use Python's secrets
module:
>>> import secrets
>>> secrets.token_hex()
'2e9ac41b1e0b66a8d93d66400e2300c4b4c2953f'
The .token_hex()
method returns a hexadecimal string containing random numbers and letters from 0
to 9
and a
to f
. Use the value that secrets.token_hex()
outputs for you and add it to your Flask project's app.py
file:
# app.py
from flask import Flask, render_template
app = Flask(__name__)
app.secret_key = "2e9ac41b1e0b66a8d93d66400e2300c4b4c2953f"
# ...
To avoid saving the secret key directly in your code, it may be a good idea to work with environment variables. You can learn more about that in the Flask documentation on configuration handling.