Flask
What It Is
Flask is a Python library that helps you turn Python code into a website. Instead of only printing text in the terminal, you can use Flask to build pages that open in a browser.
Why We Use It
Flask is a good fit for this course because it lets beginners build real web apps without too much setup. It is simple enough to learn step by step, but powerful enough to support a real Demo Day project.
With Flask, you can:
- Show pages in a browser
- Collect input from forms
- Move between different pages in your app
- Build your project one feature at a time
The Basic Idea
A Flask app usually starts with one file called app.py.
Inside that file, you:
- Import Flask
- Create the app
- Make routes
- Run the app
Example:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "<h1>Hello!</h1><p>My app is working.</p>"
if __name__ == "__main__":
app.run(debug=True)
What The Important Parts Mean
from flask import Flaskloads the Flask tool into your file.app = Flask(__name__)creates your app.@app.route("/")tells Flask which URL should open this page.def home():is the Python function Flask runs for that page.return ...is what the browser shows.app.run(debug=True)starts the app and reloads it when you save changes.
Think of a route like an address in your app:
/= homepage/about= an about page/stats= a stats page
How To Install Flask
Flask does not come with Python automatically, so you need to install it once per coding environment.
Open the terminal and run:
pip install flask
If that does not work, try:
pip3 install flask
When you see Successfully installed or your prompt comes back, Flask is ready.
How To Run A Flask App
- Save
app.py - Make sure
app.pyis the active file - Click the play button in the top-right of the editor
- Look for output like this:
* Serving Flask app 'app'
* Debug mode: on
* Running on http://127.0.0.1:5000
Then open that address in your browser.
To stop the app, click in the terminal and press Ctrl + C.
Routes And Pages
Each route is one page or action in your app.
Example:
@app.route("/")
def home():
return "<h1>Home</h1>"
@app.route("/about")
def about():
return "<h1>About</h1>"
If you visit /, Flask runs home().
If you visit /about, Flask runs about().
This is how you build an app one page at a time.
Forms And User Input
Flask can also read what a user types into a form.
Example:
from flask import Flask, request
app = Flask(__name__)
@app.route("/hello", methods=["POST"])
def hello():
name = request.form.get("name", "friend")
return f"<h1>Hello, {name}!</h1>"
Important parts:
requestlets Flask read data from a formmethods=["POST"]means the route accepts form datarequest.form.get("name")reads what the user typed into a field namedname
A Good Beginner Build Order
For this course, a good way to build a Flask project is:
- Get one route working
- Add a second page
- Add a form
- Make the app respond to user input
- Improve the design
- Move repeated HTML into templates later
Do not try to build the whole app at once. Small wins are better.
Flask And Templates
At first, it is okay to return HTML strings right inside app.py. That helps you see the connection between Python and the page.
Later, you can move your HTML into a templates/ folder. This is called using templates, and it keeps your code cleaner once your app gets bigger.
In this course, the usual path is:
- Early build weeks: routes in
app.pywith string-based HTML - Later build weeks: Jinja templates in
templates/
Common Problems
| Problem | What it usually means | Fix |
|---|---|---|
No module named flask | Flask is not installed in this environment | Run pip install flask or pip3 install flask |
| Blank page or error page | There is a mistake in your Python or HTML | Save the file, read the terminal error, and check the line it names |
| "Not Found" | Flask does not have that route yet | Check @app.route(...) and make sure the function is above app.run(...) |
| "Method Not Allowed" | The form and route are using different request methods | Check method="post" in the form and methods=["POST"] in Flask |
| App will not stop | The server is still running | Click in the terminal and press Ctrl + C |
Quick Reference
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def home():
return "<h1>Hello!</h1>"
if __name__ == "__main__":
app.run(debug=True)
pip install flask