HOWTO · Flask
How to Run a Flask App
Set up a Python virtual environment, install Flask, create a small app.py application, and run it locally.
On this page
To run a Flask app, create a Python virtual environment, install Flask inside it, save a route in app.py, and start the development server with Flask’s command-line interface. Open the local address it prints in a browser to see the route’s response. This walkthrough uses Visual Studio Code, though the same files and commands work in another editor.
Create a Basic Flask Application
Flask is a Python web framework for building web applications and APIs. A small project needs Python, Flask installed in the project environment, and a Python file that defines the application and its routes.
First, open a project folder in Visual Studio Code and check that Python is available from its integrated terminal by running python --version. The command should print an installed Python version. On some systems, the Python command is named python3; use that name consistently for the version check, environment creation, and later run command if needed.
Create a virtual environment in the project folder with python -m venv .venv. It keeps this project’s packages separate from other Python projects and from the system installation. Activate it using the command for your terminal:
- Windows Command Prompt:
.venv\Scripts\activate.bat - Windows PowerShell:
.venv\Scripts\Activate.ps1 - macOS or Linux:
source .venv/bin/activate
After activation, install Flask with python -m pip install Flask. Using python -m pip ties the installation to the Python interpreter selected for the active environment. If the terminal says that python is unavailable, repeat the commands with python3 where that command is installed. In Visual Studio Code, select the .venv interpreter if the editor does not detect it automatically.
Create a file named app.py in the project folder and add this minimal application. Its test client requests the root route and prints the response when you run python app.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hi there, this is our basic Flask app."
if __name__ == "__main__":
response = app.test_client().get("/")
print(response.get_data(as_text=True))
Hi there, this is our basic Flask app.
The Flask class creates the application object. The @app.route("/") decorator maps the site’s root path to index, and the returned string becomes the browser response. The test client exercises that route without opening a network port. The if __name__ == "__main__": block runs the check only when you execute this file directly; importing it from another module does not print the response.
To view the app in a browser, keep the environment active and the terminal in the folder containing app.py, then start Flask’s development server with flask --app app run. If your system uses python3, activate the environment created with that interpreter and use its corresponding Flask command. Flask prints a local URL, commonly http://127.0.0.1:5000; open the exact address shown in your terminal. Visiting the root path displays Hi there, this is our basic Flask app. Keep the terminal process running while you use the page, and press Ctrl+C in that terminal to stop the server. Running python app.py performs the route check shown above and exits.
Flask’s built-in server is intended for local development. Do not expose it as a production server, and do not enable debug mode in a production deployment; use a production WSGI server and deployment configuration appropriate to your host instead.