YouTip LogoYouTip

Flask Blog Project Init

This chapter takes you from scratch to create your first Web application with Flask, understanding the micro-framework design philosophy. * * * ## Flask vs Django: Positioning Differences Django is a "big and complete" full-stack framework, coming with ORM, template engine, admin backend, user authentication, form processing, etc. Flask is a micro-framework - the core is minimal, only providing routing and template rendering, other features are added on-demand through extensions. | Feature | Django | Flask | | --- | --- | --- | | Design Philosophy | Batteries included (out of the box) | Micro-framework, extend as needed | | ORM | Built-in Django ORM | Flask-SQLAlchemy (extension) | | Database Migration | Built-in makemigrations/migrate | Flask-Migrate (extension) | | User Authentication | Built-in auth module | Flask-Login (extension) | | Admin Backend | Built-in Django Admin | Flask-Admin (extension) | | Template Engine | Django Templates | Jinja2 (syntax almost identical) | | Route Configuration | Centralized in urls.py | Decorator-based declaration | Selection suggestion: Use Django for quick results as a beginner, use Flask if you prefer to have full control and assemble things as needed. * * * ## Environment Setup Confirm Python version (requires 3.8 or higher): $ python3 --version Python 3.12.0 ### Create 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 * * * ## Flask Minimal Application A Flask application only needs a single Python file. ## Example # File path: app.py from flask import Flask # Create Flask application instance # __name__ tells Flask the location of the current module (used to find templates and static files) app = Flask( __name__ ) # @app.route("/") β€” Route decorator: executes the function below when accessing the / path @app.route("/") def index(): return'

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`.
← Flask Blog SqlalchemyDjango Blog Cbv β†’