install flask
- Install Flask using pip:
pip install Flask
- Create a directory for your Flask project:
mkdir your_project_name
cd your_project_name
- Create a virtual environment (optional but recommended):
python -m venv venv
- Activate the virtual environment:
- For Windows:
bash venv\Scripts\activate
For macOS/Linux:
bash source venv/bin/activate
Create a Python script for your Flask application, e.g.,
app.py
, and open it in a text editor.Import the Flask module and create a Flask web server instance:
python from flask import Flask app = Flask(__name__)
Define a route and a function to handle the route:
python @app.route('/') def hello(): return 'Hello, World!'
Add the following code at the end of your script to run the Flask application:
python if __name__ == '__main__': app.run(debug=True)
Save your script and go back to the terminal.
Run your Flask application:
bash python app.py
Open a web browser and navigate to
http://127.0.0.1:5000/
orhttp://localhost:5000/
. You should see "Hello, World!" displayed.To stop the Flask application, press
Ctrl+C
in the terminal.
You have successfully installed Flask, created a basic Flask application, and run it locally.