Welcome to TUTORIAL Blog
' # If running python app.py directly (instead of flask run), start the development server if __name__ =='__main__': app.run(debug=True) ### Start development server (venv) $ flask run * Running on http://127.0.0.1:5000 Access http://127.0.0.1:5000/ in browser to see the page content. Enable hot reload (automatically restart server after code changes): (venv) $ flask run --debug > `flask run` by default looks for the `app` instance in `app.py`. If the file name or instance name is different, you need to set environment variables: `FLASK_APP=myapp.py` (file name), `FLASK_APP=myapp:create_app()` (factory function). * * * ## Flask Command Line Tools In addition to `flask run`, Flask provides other useful commands. | Command | Purpose | | --- | --- | | flask run | Start development server | | flask run --debug | Start server + hot reload + debug mode | | flask routes | List all registered routes | | flask shell | Enter interactive Shell (automatically imports app instance) | * * * ## Project Directory Structure Planning Although Flask supports single-file applications, real projects need a clear directory organization. blog_project/βββ app/β βββ __init__.py # Application factory create_app() (introduced in Chapter 10)β βββ blueprints/ # Route Blueprint (separated in Chapter 4)β βββ templates/ # Jinja2 templates (created in Chapter 2)β βββ static/ # Static files CSS/JS/imagesβ βββ models.py # SQLAlchemy models (created in Chapter 3)β βββ forms.py # Flask-WTF forms (created in Chapter 8)βββ config.py # Configuration fileβββ app.py # Application entry point (starts here in Chapter 1)βββ .env # Environment variables * * * ## Hands-on: Blog Homepage Route Create project skeleton, configure homepage route, and let the browser display welcome message. ## Example # File path: app.py from flask import Flask app = Flask( __name__ ) @app.route("/") def index(): """Blog homepage""" return"""Welcome to TUTORIAL Blog
This is a personal blog showcase 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 Access the homepage and about page to confirm routes work properly. * * * ## Chapter Summary In this chapter, you completed three things: created a virtual environment and installed Flask, understood the positioning difference between Flask's micro-framework and Django's "big and complete" approach, and wrote your first route page. Core takeaway: A Flask minimal application only needs `Flask(__name__)` + `@app.route()` decorator, and is started with `flask run`.
YouTip