MIL-002 Lecture steps: URL paths, HTML, decorators, authentication #24
No files matched your search
@@ -4,31 +4,29 @@
|
|||||||
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("/")
|
||||||
"""
|
def home() -> str:
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
def home() -> str:
|
|
||||||
"""Answer the home route."""
|
"""Answer the home route."""
|
||||||
return "Hello, World!"
|
return "Hello, World!"
|
||||||
|
|
||||||
@app.route("/bye")
|
|
||||||
def say_bye() -> str:
|
@_blueprint.route("/bye")
|
||||||
|
def say_bye() -> str:
|
||||||
"""Answer the fixed route /bye."""
|
"""Answer the fixed route /bye."""
|
||||||
return "Bye"
|
return "Bye"
|
||||||
|
|
||||||
@app.route("/username/<name>")
|
|
||||||
def greet(name: str) -> str:
|
@_blueprint.route("/username/<name>")
|
||||||
|
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.
|
||||||
|
|
||||||
@param name The text found in the URL after /username/.
|
@param name The text found in the URL after /username/.
|
||||||
@@ -36,8 +34,9 @@ def create_app() -> Flask:
|
|||||||
"""
|
"""
|
||||||
return f"Hello there {escape(name)}!"
|
return f"Hello there {escape(name)}!"
|
||||||
|
|
||||||
@app.route("/username/<name>/<int:number>")
|
|
||||||
def greet_with_number(name: str, number: int) -> str:
|
@_blueprint.route("/username/<name>/<int:number>")
|
||||||
|
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.
|
||||||
|
|
||||||
@param name The text found in the URL after /username/.
|
@param name The text found in the URL after /username/.
|
||||||
@@ -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,15 +11,12 @@ 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.
|
||||||
|
|
||||||
The Python string uses double quotes and the HTML attributes use single
|
The Python string uses double quotes and the HTML attributes use single
|
||||||
@@ -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,26 +54,32 @@ def make_underlined(function: Callable[..., str]) -> Callable[..., str]:
|
|||||||
return _wrap_in_tag(function, "u")
|
return _wrap_in_tag(function, "u")
|
||||||
|
|
||||||
|
|
||||||
|
## Blueprint that holds the views of this lecture step.
|
||||||
|
_blueprint = Blueprint("step_04_decorators", __name__)
|
||||||
|
|
||||||
|
|
||||||
|
@_blueprint.route("/")
|
||||||
|
def home() -> str:
|
||||||
|
"""Answer the home route."""
|
||||||
|
return "Hello, World!"
|
||||||
|
|
||||||
|
|
||||||
|
@_blueprint.route("/bye")
|
||||||
|
@make_bold
|
||||||
|
@make_emphasis
|
||||||
|
@make_underlined
|
||||||
|
def say_bye() -> str:
|
||||||
|
"""Answer /bye with bold, emphasised and underlined text."""
|
||||||
|
return "Bye"
|
||||||
|
|
||||||
|
|
||||||
def create_app() -> Flask:
|
def create_app() -> Flask:
|
||||||
"""Build the Flask application of this lecture step.
|
"""Build the Flask application of this lecture step.
|
||||||
|
|
||||||
@return The configured Flask application.
|
@return The configured Flask application.
|
||||||
"""
|
"""
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.register_blueprint(_blueprint)
|
||||||
@app.route("/")
|
|
||||||
def home() -> str:
|
|
||||||
"""Answer the home route."""
|
|
||||||
return "Hello, World!"
|
|
||||||
|
|
||||||
@app.route("/bye")
|
|
||||||
@make_bold
|
|
||||||
@make_emphasis
|
|
||||||
@make_underlined
|
|
||||||
def say_bye() -> str:
|
|
||||||
"""Answer /bye with bold, emphasised and underlined text."""
|
|
||||||
return "Bye"
|
|
||||||
|
|
||||||
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,15 +57,12 @@ 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 = []
|
||||||
for name, logged_in in (("Angela", True), ("Bob", False)):
|
for name, logged_in in (("Angela", True), ("Bob", False)):
|
||||||
@@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user