MIL-002 Lecture steps: URL paths, HTML, decorators, authentication #24

Merged
Tirsvad merged 3 commits from mil-002-lecture-steps into main 2026-11-06 05:47:50 +01:00
4 changed files with 56 additions and 33 deletions
Showing only changes of commit 34e376bea2 - Show all commits

No files matched your search

@@ -4,30 +4,28 @@
converter. converter.
""" """
from flask import Flask from flask import Blueprint, Flask
from markupsafe import escape from markupsafe import escape
from higher_lower.constants import HOST, PORT from higher_lower.constants import HOST, PORT
## Blueprint that holds the views of this lecture step.
_blueprint = Blueprint("step_02_url_paths", __name__)
def create_app() -> Flask:
"""Build the Flask application of this lecture step.
@return The configured Flask application. @_blueprint.route("/")
"""
app = Flask(__name__)
@app.route("/")
def home() -> str: def home() -> str:
"""Answer the home route.""" """Answer the home route."""
return "Hello, World!" return "Hello, World!"
@app.route("/bye")
@_blueprint.route("/bye")
def say_bye() -> str: def say_bye() -> str:
"""Answer the fixed route /bye.""" """Answer the fixed route /bye."""
return "Bye" return "Bye"
@app.route("/username/<name>")
@_blueprint.route("/username/<name>")
def greet(name: str) -> str: def greet(name: str) -> str:
"""Greet the user whose name is the variable section of the URL. """Greet the user whose name is the variable section of the URL.
@@ -36,7 +34,8 @@ def create_app() -> Flask:
""" """
return f"Hello there {escape(name)}!" return f"Hello there {escape(name)}!"
@app.route("/username/<name>/<int:number>")
@_blueprint.route("/username/<name>/<int:number>")
def greet_with_number(name: str, number: int) -> str: def greet_with_number(name: str, number: int) -> str:
"""Greet the user and use the converter <int:number> for a number. """Greet the user and use the converter <int:number> for a number.
@@ -46,6 +45,14 @@ def create_app() -> Flask:
""" """
return f"Hello there {escape(name)}, you are {number} years old!" return f"Hello there {escape(name)}, you are {number} years old!"
def create_app() -> Flask:
"""Build the Flask application of this lecture step.
@return The configured Flask application.
"""
app = Flask(__name__)
app.register_blueprint(_blueprint)
return app return app
@@ -3,7 +3,7 @@
@brief A route that returns a heading, a paragraph and two images as HTML. @brief A route that returns a heading, a paragraph and two images as HTML.
""" """
from flask import Flask from flask import Blueprint, Flask
from higher_lower.constants import HOST, LECTURE_IMAGE_WIDTH, PORT, STILL_IMAGE from higher_lower.constants import HOST, LECTURE_IMAGE_WIDTH, PORT, STILL_IMAGE
@@ -11,14 +11,11 @@ from higher_lower.constants import HOST, LECTURE_IMAGE_WIDTH, PORT, STILL_IMAGE
ANIMATED_IMAGE: str = STILL_IMAGE.replace("_s.gif", ".gif") ANIMATED_IMAGE: str = STILL_IMAGE.replace("_s.gif", ".gif")
def create_app() -> Flask: ## Blueprint that holds the views of this lecture step.
"""Build the Flask application of this lecture step. _blueprint = Blueprint("step_03_html", __name__)
@return The configured Flask application.
"""
app = Flask(__name__)
@app.route("/") @_blueprint.route("/")
def home() -> str: def home() -> str:
"""Return structured HTML instead of a plain string. """Return structured HTML instead of a plain string.
@@ -34,6 +31,14 @@ def create_app() -> Flask:
f"<img src='{ANIMATED_IMAGE}' width={LECTURE_IMAGE_WIDTH}>" f"<img src='{ANIMATED_IMAGE}' width={LECTURE_IMAGE_WIDTH}>"
) )
def create_app() -> Flask:
"""Build the Flask application of this lecture step.
@return The configured Flask application.
"""
app = Flask(__name__)
app.register_blueprint(_blueprint)
return app return app
@@ -7,7 +7,7 @@ the /bye route.
from collections.abc import Callable from collections.abc import Callable
from functools import wraps from functools import wraps
from flask import Flask from flask import Blueprint, Flask
from higher_lower.constants import HOST, PORT from higher_lower.constants import HOST, PORT
@@ -54,19 +54,17 @@ def make_underlined(function: Callable[..., str]) -> Callable[..., str]:
return _wrap_in_tag(function, "u") return _wrap_in_tag(function, "u")
def create_app() -> Flask: ## Blueprint that holds the views of this lecture step.
"""Build the Flask application of this lecture step. _blueprint = Blueprint("step_04_decorators", __name__)
@return The configured Flask application.
"""
app = Flask(__name__)
@app.route("/") @_blueprint.route("/")
def home() -> str: def home() -> str:
"""Answer the home route.""" """Answer the home route."""
return "Hello, World!" return "Hello, World!"
@app.route("/bye")
@_blueprint.route("/bye")
@make_bold @make_bold
@make_emphasis @make_emphasis
@make_underlined @make_underlined
@@ -74,6 +72,14 @@ def create_app() -> Flask:
"""Answer /bye with bold, emphasised and underlined text.""" """Answer /bye with bold, emphasised and underlined text."""
return "Bye" return "Bye"
def create_app() -> Flask:
"""Build the Flask application of this lecture step.
@return The configured Flask application.
"""
app = Flask(__name__)
app.register_blueprint(_blueprint)
return app return app
@@ -7,7 +7,7 @@ only for a logged-in user.
from collections.abc import Callable from collections.abc import Callable
from functools import wraps from functools import wraps
from flask import Flask from flask import Blueprint, Flask
from markupsafe import escape from markupsafe import escape
from higher_lower.constants import HOST, PORT from higher_lower.constants import HOST, PORT
@@ -57,14 +57,11 @@ def create_blog_post(user: User) -> str:
return f"This is {user.name}'s new blog post." return f"This is {user.name}'s new blog post."
def create_app() -> Flask: ## Blueprint that holds the views of this lecture step.
"""Build the Flask application of this lecture step. _blueprint = Blueprint("step_05_authenticated", __name__)
@return The configured Flask application.
"""
app = Flask(__name__)
@app.route("/") @_blueprint.route("/")
def home() -> str: def home() -> str:
"""Show the result of create_blog_post for a user in and out.""" """Show the result of create_blog_post for a user in and out."""
lines = [] lines = []
@@ -79,6 +76,14 @@ def create_app() -> Flask:
) )
return "<h1>is_authenticated</h1>" + "".join(lines) return "<h1>is_authenticated</h1>" + "".join(lines)
def create_app() -> Flask:
"""Build the Flask application of this lecture step.
@return The configured Flask application.
"""
app = Flask(__name__)
app.register_blueprint(_blueprint)
return app return app