Before creating a Flask app, ensure Python is installed.
Open a terminal and run:
pip install flask
To verify the installation, check the Flask version:
python -m flask --version
Create a new folder for your Flask app and navigate into it:
mkdir flask_app
cd flask_app
app.py
Inside flask_app
, create a Python file named app.py
and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, World! Welcome to Flask."
if __name__ == "__main__":
app.run(debug=True)
Run the following command in your terminal:
python app.py
You should see an output similar to this:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
You will see the message:
Hello, World! Welcome to Flask.
Flask(__name__)
: Creates a Flask application instance.@app.route("/")
: Defines a route for the home page (/
).def home()
: A function that returns "Hello, World!" when the home page is accessed.app.run(debug=True)
: Runs the Flask development server in debug mode.Modify app.py
to include additional pages:
@app.route("/about")
def about():
return "This is the About page."
@app.route("/contact")
def contact():
return "Contact us at contact@example.com."
In this guide, you learned how to:
✅ Install Flask
✅ Set up a basic Flask project
✅ Create and run a simple "Hello, World!" web application
✅ Add additional routes
This is just the beginning—now you can start building dynamic Flask applications! 🚀