Flask Web Application
This chapter takes you from zero to creating your first web application with Flask and understanding the design philosophy of a microframework.
Positioning Differences Between Flask and Django
Django is a "batteries-included" full-stack framework, with a built-in ORM, template engine, admin panel, user authentication, form handling, and more.
Flask is a microframework—a minimal core providing only routing and template rendering, with other features added on demand through extensions.
| Feature | Django | Flask |
|---|---|---|
| Design philosophy | Out of the box (batteries included) | Micro-framework, extend as needed |
| ORM | Built-in Django ORM | Flask-SQLAlchemy (extension) |
| Database migrations | Built-in makemigrations/migrate | Flask-Migrate (extension) |
| User Authentication | Built-in auth module | Flask-Login (extension) |
| Admin site | Built-in Django Admin | Flask-Admin (extension) |
| Template engine | Django Templates | Jinja2 (syntax is almost identical) |
| Routing configuration | Centralized configuration in urls.py | Decorators declared in place |
Selection advice: use Django for quick results as a beginner; use Flask if you prefer full control and assembling things as needed.
Environment Preparation
Check the Python version (3.8 or above required):
$ python3 --version
Python 3.12.0
Create a Virtual Environment and Install Flask
$ python3 -m venv venv
$ source venv/bin/activate # Windows:venv\Scripts\activate
(venv) $ pip install flask
Verify installation:
(venv) $ python -c "import flask; print(flask.__version__)"
3.1.0
Minimal Flask Application
A Flask application only needs one Python file.
Example
File path: app.py
from
flask
import
Flask
Create a Flask application instance
name Tell Flask the location of the current module (used to find templates and static files)
app
=
Flask
(
name
)
@app.route("/") — route decorator: execute the function below when the / path is accessed
@
app.
route
(
"/"
)
def
index
(
)
:
return
'
Welcome to the RUNOOB Blog
'If you run python app.py directly (rather than flask run), start the development server
if
name
==
'main'
:
app.
run
(
debug
=
True
)
Start the Dev Server
(venv) $ flask run
* Running on http://127.0.0.1:5000
Visit http://127.0.0.1:5000/ in the browser to see the page content.
Enable hot reload (automatically restart the server after code changes):
(venv) $ flask run --debug
flask runBy default, it looks for anapp.pyTheappinstance. If the filename or instance name differs, set environment variables:FLASK_APP=myapp.py(filename),FLASK_APP=myapp:create_app()(factory function).
Flask Command-Line Tools
Besides flask run, Flask also provides other useful commands.
| Command | Purpose |
|---|---|
| flask run | Start the Dev Server |
| flask run --debug | Start the server + hot reload + debug mode |
| flask routes | List all registered routes |
| flask shell | Enter an interactive Shell (automatically imports the app instance) |
Project directory structure planning
Although Flask supports single-file applications, real projects need clear directory organization.
blog_project/
├── app/
│ ├── __init__.py # 应用工厂 create_app()(第十章引入)
│ ├── blueprints/ # 路由 Blueprint(第四章分离)
│ ├── templates/ # Jinja2 模板(第二章创建)
│ ├── static/ # 静态文件 CSS/JS/图片
│ ├── models.py # SQLAlchemy 模型(第三章创建)
│ └── forms.py # Flask-WTF 表单(第八章创建)
├── config.py # 配置文件
├── app.py # 应用入口(第一章从这里开始)
└── .env # 环境变量
Hands-on: Blog Home Route
Create the project skeleton, configure the home route, and display a welcome message in the browser.
Example
File path: app.py
from
flask
import
Flask
app
=
Flask
(
name
)
@
app.
route
(
"/"
)
def
index
(
)
:
"""Blog homepage"""
return
"""
Welcome to the RUNOOB Blog
This is a personal blog showcase site built with Flask.
More content coming soon, stay tuned.
"""
@
app.
route
(
"/about"
)
def
about
(
)
:
"""About page"""
return
'
About This Site
Recording learning notes on frontend and backend.
'Run:
flask run
Visit the home and about pages to confirm the routes work properly.
Chapter Summary
In this chapter you accomplished three things: creating a virtual environment and installing Flask, understanding the positioning differences between Flask's microframework and Django's batteries-included approach, and writing your first route page.
Key takeaway: a minimal Flask application only needs a Flask(__name__) + @app.route() decorator, and use flask run to start.