Your First Flask App
What is Flask?
Flask is a lightweight Python web framework. It lets you build websites and APIs quickly with minimal setup.
Installation
Make sure you have Python installed, then run the following in your terminal.
pip install flask
Creating the App
Create a file called app.py and add the following code.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)
Running the App
In your terminal, run the following command.
python app.py
Then open your browser and visit http://127.0.0.1:5000 to see your app running.
Adding More Routes
You can add as many routes as you need.
@app.route("/about")
def about():
return "This is the about page."
Using Templates
Instead of returning plain text, render HTML templates using render_template.
from flask import render_template
@app.route("/")
def home():
return render_template("index.html")
Next Steps
Learn how to pass data to templates, handle forms, and connect a database.