Framework cheatsheet
Flask 3 Cheatsheet
Create, run, test and deploy Flask apps, with routes, templates, blueprints, sessions and database snippets.
Setup and CLI
- Install Flask — Install Flask into the active virtual environment.
- Hello world app — The smallest Flask application, saved as app.py.
- Run the development server — Start the app with the debugger and auto-reload. Never use debug mode in production.
- Run on another host or port — Listen on all interfaces on port 8000.
- List the routes — Show all URL rules and their endpoints.
- Open a shell with the app loaded — Start a Python shell with the application context active.
- Add a CLI command — Add your own command, run with flask --app app seed.
Routes and requests
- Capture part of the URL — Convert the value to an integer with a converter.
- Handle several methods — Accept GET and POST on one route.
- Read query string values — Read ?page=2 with a default.
- Read a JSON body — Parse the JSON request body.
- Return JSON — Return a dict or list and Flask converts it to JSON, with a status code.
- Read form fields — Read submitted form values.
- Redirect to another route — Send the user to another endpoint by name.
- Return an error — Stop the request with an HTTP error code.
- Customize an error page — Return a custom response for 404 errors.
- Run code before every request — Run a function before each request, for example to load the current user.
Templates, sessions and config
- Render a template — Render templates/index.html with variables. Jinja escapes values by default.
- Loop in a template — Use Jinja control structures in the template.
- Share a layout — Define blocks in a base template and fill them in child templates.
- Link to a static file — Build the URL of a file in the static folder.
- Remember a user between requests — Store values in the signed cookie session. It needs a secret key.
- Load configuration — Load settings from an object and the environment.
- Show a one-time message — Queue a message to show on the next page.
Blueprints, database and CORS
- Group routes in a blueprint — Split a large app into modules and register them with the app.
- Use an application factory — Create the app in a function, which makes testing and configuration easier.
- Add SQLAlchemy — Install the extension and bind it to the app.
- Manage migrations — Create and apply database migrations with Flask-Migrate.
- Allow cross-origin requests — Let a browser app on another origin call your API.
Testing and deployment
- Test with the test client — Call routes without running a server.
- Create a pytest fixture — Make a test client available to tests.
- Run with Gunicorn — Serve the app with a production WSGI server.
- Trust a reverse proxy — Read the real client IP and scheme when behind Nginx.
Related cheatsheets
- Angular 22 cheatsheet — Angular CLI commands and standalone component, signals, template, routing, HTTP and forms snippets.
- Astro cheatsheet — Build fast content sites with Astro: components, layouts, routing, content collections, islands, endpoints and deployment.
- Django 6 cheatsheet — Create projects, run migrations, test and deploy Django apps, plus the ORM queries you use every day.
- .NET and ASP.NET Core 10 cheatsheet — The dotnet CLI for creating, building, testing and publishing projects, plus common ASP.NET Core and EF Core code.
- Express 5 cheatsheet — Build HTTP servers and APIs with Express 5 on Node.js.
- FastAPI 0.115 cheatsheet — Install, run, test and deploy FastAPI apps from the command line.