TodayTools

Flask flashcards

A small Python web framework you grow as you go.

Donate
Beginner friendly, starts from zero
Section
How to study
0
FlaskStart from zero · 1 of 12

What is Flask, in one plain sentence?

Flask is a small Python toolkit for building websites and web services: you write a Python function, and Flask runs it whenever someone visits a certain web address.

An address comes in, your function answers.

All topics

Flask at a glance

Flask is a small Python toolkit that runs your functions when someone visits a web address.

Flask is a waiter: it takes each order, hands it to the right cook (your function) and brings the dish back.

Words you will meet

Route
A link between a web address and one of your functions
View
The Python function that answers a route
Request
What the browser or app sends to your server
Response
What your server sends back, with a status code
Template
An HTML file with gaps that Flask fills with data
JSON
A plain-text way to write data that apps can read
Virtual environment
A private folder of packages for one project
Production
The live server that real users visit

The steps

  1. 1. Start from zero (12 cards): What Flask is, how a web request works, and the words you need before your first route.
  2. 2. Core basics (16 cards): Your first app, routes, requests, JSON, templates and error output.
  3. 3. Next steps (16 cards): Blueprints, the app factory, config, sessions, forms, tests and extensions.
  4. 4. Go deeper (16 cards): Contexts, async views, deployment, proxies, security, streaming and resilience.
All 12 cards in Start from zero
What is Flask, in one plain sentence?
Flask is a small Python toolkit for building websites and web services: you write a Python function, and Flask runs it whenever someone visits a certain web address.
What problem does Flask solve? What was it like without it?
Without it you would read raw web traffic yourself, work out which page was asked for and format every reply by hand. Flask does that plumbing so you only write what each page should do.
Where do you already meet something like Flask every day?
When you tap Track order in an app, a program on another computer looks up your order and sends it back. Small services like that are often written with Flask.
What everyday thing is Flask like?
A waiter in a restaurant. He takes each order from a table, passes it to the right cook, and brings the dish back. Flask takes each request, calls the right function and returns its answer.
What are a server, a client and production?
A server is a program that waits for requests and answers them. A client is whatever asks: a browser or a phone app. While you learn, your laptop is the server; the live one for real users is called production.
What are a URL, a route and a view?
A URL is a web address, like /orders/42. A route links a URL to one of your Python functions, and that function is called a view.
What are a request, a response and a status code?
The request is what the client sends: the address, a method like GET (read) or POST (send data), and any data. The response is your reply plus a status code: 200 means OK, 404 not found, 500 server error.
What are JSON and an API?
JSON is a plain-text way to write data, like {"status": "ok"}. An API is a set of web addresses that apps call to get or send data, usually as JSON, instead of web pages.
What are HTML and a template?
HTML is the text that describes a web page. A template is an HTML file with gaps, like {{ name }}, that Flask fills with real data before sending it. Flask's template language is called Jinja.
What are the terminal, a package and a virtual environment?
The terminal is a window where you type commands. A package is ready-made code you install with pip; Flask is one. A virtual environment is a private folder of packages for one project.
What do you need, and what is your first tiny win?
Python and pip install flask. Save this as app.py, run flask --app app run and open localhost:5000 (your own computer). The route / runs the view home, which returns the text.
What will you be able to do after this topic?
Build a small website or JSON API in Python, read form and URL data safely, show pages from templates, and run it properly in production with logging and error pages.
Flask cheatsheet

Routing

  • @app.get("/orders/<int:order_id>") Typed URL variable
  • @app.route("/x", methods=["GET", "POST"]) One view, two verbs
  • <path:subpath> / <uuid:token> Slash-capturing / UUID converters
  • url_for("orders.detail", order_id=7) Build URL by endpoint
  • Blueprint("orders", __name__, url_prefix="/orders") Feature module
  • app.add_url_rule(rule, view_func=V.as_view("v")) Register a class view

Request

  • request.args.get("page", 1, type=int) Typed query value
  • request.args.getlist("tag") Repeated query keys
  • request.form["email"] Form field (400 if missing)
  • request.get_json(silent=True) JSON body or None
  • request.files["photo"] Uploaded file
  • request.headers.get("X-Request-Id") Header value
  • request.remote_addr Client IP (use ProxyFix)

Response

  • return {"ok": True}, 201 JSON with status
  • return body, 200, {"Cache-Control": "no-store"} Add headers
  • redirect(url_for("home"), code=303) Redirect after POST
  • abort(404) Stop with an error
  • send_file(path, as_attachment=True) Download a file
  • Response(gen(), mimetype="text/csv") Stream output
  • make_response(...).set_cookie(...) Edit response before returning

CLI

  • flask --app orders run --debug Dev server with reload
  • flask --app "orders:create_app()" run Run a factory
  • flask routes List every URL rule
  • flask shell Python shell with app context
  • flask db migrate -m "add sku" && flask db upgrade Flask-Migrate schema change
  • gunicorn "orders:create_app()" -w 4 -b 127.0.0.1:8000 Production server

Config and state

  • app.config.from_prefixed_env() Read FLASK_* env vars
  • SECRET_KEY / SECRET_KEY_FALLBACKS Sign sessions / rotate keys (3.1+)
  • MAX_CONTENT_LENGTH Max request body, 413 above
  • SESSION_COOKIE_SECURE / _SAMESITE Cookie hardening
  • g.user Per-request storage
  • current_app.config["KEY"] Config from inside any code
  • with app.app_context(): Context for scripts and jobs
Donate