Week 9: Flask
A web app comes alive on a server: routes answering requests, sessions remembering who you are, and one push shipping it to the world.
7 topics · 26 questions
Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.
Routes and View Functions
- How it worksHow does a Flask route map a URL like slash login to one specific Python function that handles it?
- Worked exampleWalk me through a Flask route decorated with slash greet slash name that returns Hello followed by that name.
- Common mistakeWhy isn't a Flask route just a filename that the server happens to look up on disk?
Your turn1 of 2
A Flask app defines 22 separate routes, and 3 of them share the exact same view function while every other route has its own unique function. How many distinct view functions does the app have in total?Handling GET and POST Requests
- Worked exampleWalk me through a Flask route that checks whether the request method is POST before reading a submitted username.
- Common mistakeWhy isn't a GET request a safe place to send a password?
- Going furtherWhat happens to a browser's URL bar when a form is submitted with GET instead of POST?
Your turn1 of 2
A form is submitted via POST with 15 separate fields, and the server-side view function reads 2 of those fields to process the request. How many fields does the view function ignore?Templates and Jinja
- How it worksHow do double curly braces in a Jinja template get replaced with real Python values before the page is sent?
- Common mistakeWhy isn't a Jinja template just an HTML file with some extra symbols sprinkled into it?
- Worked exampleWalk me through a Jinja template that displays Hello, name inside double curly braces, given name equals David.
- Going furtherWhat happens inside a Jinja template when it loops over a list of ten items with a for tag?
Your turn1 of 2
A Jinja template has 15 double-curly-brace placeholders, and 3 of them reference the same repeated variable while the rest are all distinct from each other and from it. How many distinct variable names appear across the placeholders?Building an API with Flask
- How it worksHow does a Flask route return JSON instead of HTML so another program can read the response directly?
- Worked exampleWalk me through a Flask API route that returns a JSON object with a price field set to 142.50 for a stock quote.
- Going furtherWhat happens when an API is called far more often than the server running it can handle?
- Ties togetherHow does a Flask API returning JSON connect back to JavaScript fetching data and updating the DOM without a reload?
Your turn1 of 2
An API is called 3512 times per minute, and the server can comfortably handle 68 calls per minute before slowing down. How many calls per minute exceed that comfortable capacity?Environment Variables and API Keys
- How it worksHow does an environment variable let a running program read a secret value that never appears in the code itself?
- How it worksHow does a Flask app look up a stock price by calling an external API using a key stored as an environment variable?
- Worked exampleWalk me through setting an environment variable named API_KEY and reading it inside a Flask app with os.environ.get.
- Common mistakeWhy isn't committing an API key to a public repository a mistake that just fixes itself once you delete the line later?
Your turn1 of 2
A Flask app reads 11 separate secret values from environment variables. If 2 of those variables are missing at startup, how many secret values does the app fail to load?Deploying a Web App
- How it worksHow does deploying a web app move it from a local development server onto a public server with its own address?
- Worked exampleWalk me through the steps of pushing a finished Flask app to a hosting platform so it gets a public URL.
- Going furtherWhat happens to a deployed web app's reliability once real traffic starts hitting it instead of just you testing it?
Your turn1 of 2
Deploying a Flask app to a hosting platform takes 11 minutes for the build step and 7 minutes for the platform to start serving traffic afterward. How many total minutes pass from starting the deploy to serving live traffic?