From 256ad44d08395521ba9ce67c4e1817b98ec9ce17 Mon Sep 17 00:00:00 2001 From: Michael Hartl Date: Sun, 13 Nov 2022 10:39:28 -0800 Subject: [PATCH] Add listings --- Listing_1.11.py | 1 + Listing_10.10.txt | 7 ++++++ Listing_10.11.py | 26 +++++++++++++++++++++ Listing_10.12.html | 21 ++++++++++------- Listing_10.13.html | 14 +++++++++--- Listing_10.14.html | 24 ++++++++++++++++++++ Listing_10.15.py | 10 +++++++++ Listing_10.16.html | 20 +++++++++++++++++ Listing_10.17.txt | 1 + Listing_10.18.py | 12 ++++++++++ Listing_10.19.py | 6 ----- Listing_10.21.py | 17 ++++++++++++++ Listing_10.22.txt | 7 ++++++ Listing_10.23.html | 36 ++++++++++++++++------------- Listing_10.24.html | 18 +++++++++++++-- Listing_10.25.html | 16 +++++++++++++ Listing_10.26.html | 7 ++++++ Listing_10.28.py | 6 ++--- Listing_10.29.txt | 13 ++--------- Listing_10.3.py | 2 ++ Listing_10.30.py | 55 +++++++++++++++++---------------------------- Listing_10.31.txt | 16 +++++++++++++ Listing_10.32.py | 38 +++++++++++++++++++++++++++++++ Listing_10.33.html | 27 +++------------------- Listing_10.34.txt | 7 ++++++ Listing_10.35.html | 29 ++++++++++++++++++++++++ Listing_10.36.py | 24 ++++++++++++++++++++ Listing_10.37.txt | 7 ++++++ Listing_10.38.html | 22 ++++++++++++++++++ Listing_10.39.html | 32 ++++++++------------------ Listing_10.40.txt | 11 +++++++-- Listing_10.41.html | 21 +++++++++++++++++ Listing_10.42.txt | 7 ++++++ Listing_10.43.py | 23 +++++++++++++++++++ Listing_10.44.py | 9 ++++++++ Listing_10.45.txt | 7 ++++++ Listing_10.46.html | 11 +++++++++ Listing_10.47.py | 31 +++++++++++++++++++++++++ Listing_10.48.py | 4 ++++ Listing_10.49.html | 5 +++++ Listing_10.50.html | 15 +++++++++++++ Listing_10.51.py | 56 +++++++++++++++++++++++++++++++++++----------- Listing_10.52.py | 6 +++++ Listing_10.53.py | 3 +-- Listing_10.54.txt | 12 ++-------- Listing_10.55.py | 17 ++++++++++++++ Listing_10.56.txt | 12 ++++++++-- Listing_10.57.html | 26 ++++++++++++++++----- Listing_10.58.txt | 8 +++++++ Listing_10.59.html | 12 +++++----- Listing_10.6.txt | 8 ++++++- Listing_10.60.html | 19 ++++++++++++++++ Listing_10.61.html | 7 ++++++ Listing_10.62.py | 48 +++++++++++++++++++++++++++++++++++++++ Listing_10.63.html | 19 ++++++++++++++++ Listing_10.64.txt | 8 +++++++ Listing_10.65.txt | 2 ++ Listing_10.7.txt | 2 +- Listing_10.8.txt | 1 + Listing_10.9.py | 1 + Listing_11.10.py | 26 +++++++++++++++------ Listing_11.11.py | 16 ++++++------- Listing_11.12.py | 12 +++++++--- Listing_11.13.py | 10 +++------ Listing_11.14.py | 15 ++++++------- Listing_11.15.py | 11 ++++++--- Listing_11.16.py | 18 +++------------ Listing_11.17.py | 4 ++-- Listing_11.18.py | 19 ++++++++++++---- Listing_11.19.py | 3 ++- Listing_11.2.txt | 8 +++---- Listing_11.20.py | 9 ++++---- Listing_11.21.py | 8 +------ Listing_11.22.py | 5 +++++ Listing_11.23.py | 7 ++++++ Listing_11.5.py | 1 + Listing_11.7.py | 4 +++- Listing_2.10.py | 8 +++---- Listing_2.12.py | 6 ++--- Listing_2.14.py | 10 ++++----- Listing_2.16.py | 6 ++--- Listing_2.17.py | 6 ++--- Listing_2.18.py | 6 ++--- Listing_2.19.py | 10 ++++----- Listing_2.20.py | 12 +++------- Listing_2.21.py | 11 +++++++-- Listing_2.22.py | 10 ++------- Listing_2.23.py | 16 ++++++------- Listing_2.24.py | 8 +++++++ Listing_2.25.js | 8 +++++++ Listing_2.26.py | 4 ++-- Listing_2.27.py | 21 +++++++++-------- Listing_2.28.py | 4 ++-- Listing_2.29.py | 13 +++++++++++ Listing_2.7.py | 4 ++-- Listing_2.8.py | 4 ++-- Listing_2.9.py | 6 ++--- Listing_3.1.py | 4 ++-- Listing_3.10.py | 7 ++++++ Listing_3.11.py | 11 +++++++++ Listing_3.12.py | 4 ++++ Listing_3.4.py | 18 ++++++++------- Listing_3.5.py | 7 ++---- Listing_3.6.py | 16 ++++++------- Listing_3.7.py | 12 +++++----- Listing_3.8.py | 13 ++++++----- Listing_3.9.py | 9 +++++--- Listing_4.10.py | 1 + Listing_4.11.py | 1 + Listing_4.13.py | 2 ++ Listing_4.3.py | 5 ++++- Listing_4.4.py | 7 ++++-- Listing_4.5.py | 4 +++- Listing_5.1.py | 8 +++---- Listing_5.10.py | 5 +---- Listing_5.11.py | 7 +++--- Listing_5.12.py | 21 +++++++++-------- Listing_5.13.py | 15 ++++++++----- Listing_5.14.py | 17 +++++++++----- Listing_5.15.py | 17 +++++++------- Listing_5.16.py | 12 +++++----- Listing_5.17.py | 12 +++++----- Listing_5.18.py | 13 ++++++----- Listing_5.19.py | 11 ++++++--- Listing_5.2.py | 12 +++++----- Listing_5.20.py | 5 +++++ Listing_5.21.py | 5 +++++ Listing_5.22.py | 3 +++ Listing_5.3.py | 12 +++++----- Listing_5.4.py | 16 +++++-------- Listing_5.5.py | 22 +++++------------- Listing_5.6.py | 19 +++++----------- Listing_5.7.py | 16 +++++++++---- Listing_5.8.py | 14 ++++++++++-- Listing_5.9.py | 12 +++++++--- Listing_6.3.py | 2 +- Listing_6.6.py | 16 ++++++++----- Listing_6.7.py | 6 +++++ Listing_6.8.py | 21 +++++------------ Listing_7.10.py | 3 ++- Listing_7.11.py | 2 +- Listing_7.8.py | 2 +- Listing_7.9.py | 5 +++++ Listing_8.1.txt | 3 +-- Listing_8.10.txt | 17 ++++++++++++++ Listing_8.11.py | 2 ++ Listing_8.12.txt | 7 ++++++ Listing_8.13.py | 2 ++ Listing_8.14.txt | 7 ++++++ Listing_8.15.py | 17 ++++++++++++++ Listing_8.16.py | 18 +-------------- Listing_8.17.txt | 20 +++++++++++++++++ Listing_8.18.txt | 21 +---------------- Listing_8.19.py | 8 +++++++ Listing_8.2.txt | 8 +++---- Listing_8.20.txt | 9 ++++++++ Listing_8.21.py | 16 +++++++++++++ Listing_8.22.txt | 7 ++++++ Listing_8.23.py | 16 +++++++++++++ Listing_8.24.py | 23 ++++++++++--------- Listing_8.25.py | 25 ++++++++++----------- Listing_8.26.txt | 17 ++++++++++++++ Listing_8.27.py | 19 ++++++++++++++++ Listing_8.28.py | 28 +++++++++++++---------- Listing_8.29.txt | 24 ++++++++++++++++++++ Listing_8.3.toml | 22 ++++++++++++++++++ Listing_8.30.py | 28 +++++++++++++++++++++++ Listing_8.31.txt | 19 ++++++++++++++++ Listing_8.32.py | 28 +++++++++++++++++++++++ Listing_8.33.txt | 7 ++++++ Listing_8.34.py | 24 ++++++++++++++++++++ Listing_8.35.txt | 7 ++++++ Listing_8.36.py | 24 ++++++++++++++++++++ Listing_8.37.txt | 7 ++++++ Listing_8.38.py | 25 +++++++++++++++++++++ Listing_8.39.py | 24 ++++++++++++++++++++ Listing_8.40.txt | 2 ++ Listing_8.6.txt | 5 +++++ Listing_8.7.bash | 16 +++++++++++++ Listing_8.8.txt | 3 +++ Listing_8.9.py | 2 ++ Listing_9.1.py | 1 + Listing_9.10.py | 14 +++++++++--- Listing_9.11.py | 16 ++++++++----- Listing_9.12.py | 8 +++---- Listing_9.13.py | 16 +++++-------- Listing_9.14.py | 17 +++++--------- Listing_9.15.py | 10 ++++----- Listing_9.16.py | 13 +++++------ Listing_9.17.py | 18 +++++++++++++++ Listing_9.18.py | 21 +++++++++++++++++ Listing_9.2.py | 5 +++-- Listing_9.3.py | 5 +++-- Listing_9.4.py | 5 +++-- Listing_9.5.py | 7 +++--- Listing_9.6.py | 5 +++-- Listing_9.7.py | 7 ++++-- Listing_9.8.py | 7 ++++-- Listing_9.9.py | 26 +++++++++++++++------ 199 files changed, 1834 insertions(+), 657 deletions(-) create mode 100644 Listing_10.10.txt create mode 100644 Listing_10.11.py create mode 100644 Listing_10.14.html create mode 100644 Listing_10.15.py create mode 100644 Listing_10.16.html create mode 100644 Listing_10.17.txt create mode 100644 Listing_10.18.py create mode 100644 Listing_10.21.py create mode 100644 Listing_10.22.txt create mode 100644 Listing_10.25.html create mode 100644 Listing_10.26.html create mode 100644 Listing_10.31.txt create mode 100644 Listing_10.32.py create mode 100644 Listing_10.34.txt create mode 100644 Listing_10.35.html create mode 100644 Listing_10.36.py create mode 100644 Listing_10.37.txt create mode 100644 Listing_10.38.html create mode 100644 Listing_10.41.html create mode 100644 Listing_10.42.txt create mode 100644 Listing_10.43.py create mode 100644 Listing_10.44.py create mode 100644 Listing_10.45.txt create mode 100644 Listing_10.46.html create mode 100644 Listing_10.47.py create mode 100644 Listing_10.48.py create mode 100644 Listing_10.49.html create mode 100644 Listing_10.50.html create mode 100644 Listing_10.52.py create mode 100644 Listing_10.55.py create mode 100644 Listing_10.58.txt create mode 100644 Listing_10.60.html create mode 100644 Listing_10.61.html create mode 100644 Listing_10.62.py create mode 100644 Listing_10.63.html create mode 100644 Listing_10.64.txt create mode 100644 Listing_10.65.txt create mode 100644 Listing_10.8.txt create mode 100644 Listing_10.9.py create mode 100644 Listing_11.22.py create mode 100644 Listing_11.23.py create mode 100644 Listing_2.24.py create mode 100644 Listing_2.25.js create mode 100644 Listing_2.29.py create mode 100644 Listing_3.10.py create mode 100644 Listing_3.11.py create mode 100644 Listing_3.12.py create mode 100644 Listing_5.20.py create mode 100644 Listing_5.21.py create mode 100644 Listing_5.22.py create mode 100644 Listing_7.9.py create mode 100644 Listing_8.10.txt create mode 100644 Listing_8.11.py create mode 100644 Listing_8.12.txt create mode 100644 Listing_8.13.py create mode 100644 Listing_8.14.txt create mode 100644 Listing_8.15.py create mode 100644 Listing_8.17.txt create mode 100644 Listing_8.19.py create mode 100644 Listing_8.20.txt create mode 100644 Listing_8.21.py create mode 100644 Listing_8.22.txt create mode 100644 Listing_8.23.py create mode 100644 Listing_8.26.txt create mode 100644 Listing_8.27.py create mode 100644 Listing_8.29.txt create mode 100644 Listing_8.3.toml create mode 100644 Listing_8.30.py create mode 100644 Listing_8.31.txt create mode 100644 Listing_8.32.py create mode 100644 Listing_8.33.txt create mode 100644 Listing_8.34.py create mode 100644 Listing_8.35.txt create mode 100644 Listing_8.36.py create mode 100644 Listing_8.37.txt create mode 100644 Listing_8.38.py create mode 100644 Listing_8.39.py create mode 100644 Listing_8.40.txt create mode 100644 Listing_8.6.txt create mode 100644 Listing_8.7.bash create mode 100644 Listing_8.8.txt create mode 100644 Listing_8.9.py create mode 100644 Listing_9.17.py create mode 100644 Listing_9.18.py diff --git a/Listing_1.11.py b/Listing_1.11.py index 6da89e0..289f9ff 100644 --- a/Listing_1.11.py +++ b/Listing_1.11.py @@ -1,5 +1,6 @@ from flask import Flask + app = Flask(__name__) @app.route("/") diff --git a/Listing_10.10.txt b/Listing_10.10.txt new file mode 100644 index 0000000..f562008 --- /dev/null +++ b/Listing_10.10.txt @@ -0,0 +1,7 @@ +(venv) $ deactivate +$ rm -rf venv/ +$ python3 -m venv venv +$ source venv/bin/activate +(venv) $ pip install -r requirements.txt +(venv) $ flask --app palindrome_detector --debug run + * Running on http://127.0.0.1:5000/ diff --git a/Listing_10.11.py b/Listing_10.11.py new file mode 100644 index 0000000..5099793 --- /dev/null +++ b/Listing_10.11.py @@ -0,0 +1,26 @@ +import os + +from flask import Flask, render_template + + +def create_app(test_config=None): + """Create and configure the app.""" + app = Flask(__name__, instance_relative_config=True) + . + . + . + @app.route("/") + def index(): + return render_template("index.html") + + @app.route("/about") + def about(): + return render_template("about.html") + + @app.route("/palindrome") + def palindrome(): + return render_template("palindrome.html") + + return app + +app = create_app() diff --git a/Listing_10.12.html b/Listing_10.12.html index 052eb08..449b64c 100644 --- a/Listing_10.12.html +++ b/Listing_10.12.html @@ -14,18 +14,23 @@
-

About

+

Sample Flask App

- This site is the final application in + This is the sample Flask app for Learn Enough Python - to Be Dangerous - by Michael Hartl, - a tutorial introduction to the - Python programming language that - is part of - LearnEnough.com. + to Be Dangerous. Learn more on the About page.

+ +

+ Click the Sator + Square below to run the custom Palindrome + Detector. +

+ + + Sator Square +
diff --git a/Listing_10.13.html b/Listing_10.13.html index 4eede9f..052eb08 100644 --- a/Listing_10.13.html +++ b/Listing_10.13.html @@ -14,10 +14,18 @@
-

Palindrome Detector

- -

This will be the palindrome detector.

+

About

+

+ This site is the final application in + Learn Enough Python + to Be Dangerous + by Michael Hartl, + a tutorial introduction to the + Python programming language that + is part of + LearnEnough.com. +

diff --git a/Listing_10.14.html b/Listing_10.14.html new file mode 100644 index 0000000..4eede9f --- /dev/null +++ b/Listing_10.14.html @@ -0,0 +1,24 @@ + + + + + Learn Enough Python Sample App + + + + + +
+
+ +

Palindrome Detector

+ +

This will be the palindrome detector.

+ +
+
+ + diff --git a/Listing_10.15.py b/Listing_10.15.py new file mode 100644 index 0000000..e808d40 --- /dev/null +++ b/Listing_10.15.py @@ -0,0 +1,10 @@ +% import os +% from flask import Flask + +% def create_app(test_config=None): +% """Create and configure the app.""" +% app = Flask(__name__, instance_relative_config=True, +% static_url_path="/static") +% . +% . +% . diff --git a/Listing_10.16.html b/Listing_10.16.html new file mode 100644 index 0000000..26c826e --- /dev/null +++ b/Listing_10.16.html @@ -0,0 +1,20 @@ + + + + + Learn Enough Python Sample App + + + + + +
+
+ +
+
+ + diff --git a/Listing_10.17.txt b/Listing_10.17.txt new file mode 100644 index 0000000..922fb8f --- /dev/null +++ b/Listing_10.17.txt @@ -0,0 +1 @@ +$ pip install -e . diff --git a/Listing_10.18.py b/Listing_10.18.py new file mode 100644 index 0000000..39165a4 --- /dev/null +++ b/Listing_10.18.py @@ -0,0 +1,12 @@ +import pytest + +from palindrome_detector import create_app + + +@pytest.fixture +def app(): + return create_app() + +@pytest.fixture +def client(app): + return app.test_client() diff --git a/Listing_10.19.py b/Listing_10.19.py index 2a665a8..32e7b02 100644 --- a/Listing_10.19.py +++ b/Listing_10.19.py @@ -1,17 +1,11 @@ def test_index(client): response = client.get("/") assert response.status_code == 200 - assert "" in response.text - assert "<h1>" in response.text def test_about(client): response = client.get("/about") assert response.status_code == 200 - assert "<title>" in response.text - assert "<h1>" in response.text def test_palindrome(client): response = client.get("/palindrome") assert response.status_code == 200 - assert "<title>" in response.text - assert "<h1>" in response.text diff --git a/Listing_10.21.py b/Listing_10.21.py new file mode 100644 index 0000000..2a665a8 --- /dev/null +++ b/Listing_10.21.py @@ -0,0 +1,17 @@ +def test_index(client): + response = client.get("/") + assert response.status_code == 200 + assert "<title>" in response.text + assert "<h1>" in response.text + +def test_about(client): + response = client.get("/about") + assert response.status_code == 200 + assert "<title>" in response.text + assert "<h1>" in response.text + +def test_palindrome(client): + response = client.get("/palindrome") + assert response.status_code == 200 + assert "<title>" in response.text + assert "<h1>" in response.text diff --git a/Listing_10.22.txt b/Listing_10.22.txt new file mode 100644 index 0000000..c8a9871 --- /dev/null +++ b/Listing_10.22.txt @@ -0,0 +1,7 @@ +(venv) $ pytest +============================= test session starts ============================== +collected 3 items + +tests/test_site_pages.py ... [100%] + +============================== 3 passed in 0.01s =============================== diff --git a/Listing_10.23.html b/Listing_10.23.html index 1966020..3e73ccd 100644 --- a/Listing_10.23.html +++ b/Listing_10.23.html @@ -1,16 +1,20 @@ -{% extends "layout.html" %} - -{% block content %} - <h1>About</h1> - - <p> - This site is the final application in - <a href="https://www.learnenough.com/python-tutorial"><em>Learn Enough Python - to Be Dangerous</em></a> - by <a href="https://www.michaelhartl.com/">Michael Hartl</a>, - a tutorial introduction to the - <a href="https://www.python.org/">Python programming language</a> that - is part of - <a href="https://www.learnenough.com/">LearnEnough.com</a>. - </p> -{% endblock %} +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <title>Learn Enough Python Sample App + + + + + +
+
+ {% block content %}{% endblock %} +
+
+ + diff --git a/Listing_10.24.html b/Listing_10.24.html index ce855ea..c7039e8 100644 --- a/Listing_10.24.html +++ b/Listing_10.24.html @@ -1,7 +1,21 @@ {% extends "layout.html" %} {% block content %} -

Palindrome Detector

+

Sample Flask App

-

This will be the palindrome detector.

+

+ This is the sample Flask app for + Learn Enough Python + to Be Dangerous. Learn more on the About page. +

+ +

+ Click the Sator + Square below to run the custom Palindrome + Detector. +

+ + + Sator Square + {% endblock %} diff --git a/Listing_10.25.html b/Listing_10.25.html new file mode 100644 index 0000000..1966020 --- /dev/null +++ b/Listing_10.25.html @@ -0,0 +1,16 @@ +{% extends "layout.html" %} + +{% block content %} +

About

+ +

+ This site is the final application in + Learn Enough Python + to Be Dangerous + by Michael Hartl, + a tutorial introduction to the + Python programming language that + is part of + LearnEnough.com. +

+{% endblock %} diff --git a/Listing_10.26.html b/Listing_10.26.html new file mode 100644 index 0000000..ce855ea --- /dev/null +++ b/Listing_10.26.html @@ -0,0 +1,7 @@ +{% extends "layout.html" %} + +{% block content %} +

Palindrome Detector

+ +

This will be the palindrome detector.

+{% endblock %} diff --git a/Listing_10.28.py b/Listing_10.28.py index aaf26fa..6a720a5 100644 --- a/Listing_10.28.py +++ b/Listing_10.28.py @@ -2,7 +2,7 @@ def test_index(client): response = client.get("/") assert response.status_code == 200 base_title = "Learn Enough Python Sample App" - title = f"{base_title} | Home" + title = f"{base_title}" assert title in response.text assert "

" in response.text @@ -10,7 +10,7 @@ def test_about(client): response = client.get("/about") assert response.status_code == 200 base_title = "Learn Enough Python Sample App" - title = f"{base_title} | About" + title = f"{base_title}" assert title in response.text assert "

" in response.text @@ -18,6 +18,6 @@ def test_palindrome(client): response = client.get("/palindrome") assert response.status_code == 200 base_title = "Learn Enough Python Sample App" - title = f"{base_title} | Palindrome Detector" + title = f"{base_title}" assert title in response.text assert "

" in response.text diff --git a/Listing_10.29.txt b/Listing_10.29.txt index 5c79f54..c8a9871 100644 --- a/Listing_10.29.txt +++ b/Listing_10.29.txt @@ -2,15 +2,6 @@ ============================= test session starts ============================== collected 3 items -tests/test_site_pages.py FFF [100%] +tests/test_site_pages.py ... [100%] -=================================== FAILURES =================================== -__________________________________ test_index __________________________________ -. -. -. -=========================== short test summary info ============================ -FAILED tests/test_site_pages.py::test_index - assert 'Learn Enough Pyt... -FAILED tests/test_site_pages.py::test_about - assert '<title>Learn Enough Pyt... -FAILED tests/test_site_pages.py::test_palindrome - assert '<title>Learn Enoug... -============================== 3 failed in 0.03s =============================== +============================== 3 passed in 0.01s =============================== diff --git a/Listing_10.3.py b/Listing_10.3.py index d6107f4..386c2f5 100644 --- a/Listing_10.3.py +++ b/Listing_10.3.py @@ -1,6 +1,8 @@ import os + from flask import Flask + def create_app(test_config=None): """Create and configure the app.""" app = Flask(__name__, instance_relative_config=True) diff --git a/Listing_10.30.py b/Listing_10.30.py index 0fa858f..aaf26fa 100644 --- a/Listing_10.30.py +++ b/Listing_10.30.py @@ -1,36 +1,23 @@ -import os -from flask import Flask, render_template +def test_index(client): + response = client.get("/") + assert response.status_code == 200 + base_title = "Learn Enough Python Sample App" + title = f"<title>{base_title} | Home" + assert title in response.text + assert "

" in response.text -def create_app(test_config=None): - """Create and configure the app.""" - app = Flask(__name__, instance_relative_config=True) +def test_about(client): + response = client.get("/about") + assert response.status_code == 200 + base_title = "Learn Enough Python Sample App" + title = f"{base_title} | About" + assert title in response.text + assert "

" in response.text - if test_config is None: - # Load the instance config, if it exists, when not testing. - app.config.from_pyfile("config.py", silent=True) - else: - # Load the test config if passed in. - app.config.from_mapping(test_config) - - # Ensure the instance folder exists. - try: - os.makedirs(app.instance_path) - except OSError: - pass - - @app.route("/") - def index(): - return render_template("index.html", page_title="Home") - - @app.route("/about") - def about(): - return render_template("about.html", page_title="About") - - @app.route("/palindrome") - def palindrome(): - return render_template("palindrome.html", - page_title="Palindrome Detector") - - return app - -app = create_app() +def test_palindrome(client): + response = client.get("/palindrome") + assert response.status_code == 200 + base_title = "Learn Enough Python Sample App" + title = f"{base_title} | Palindrome Detector" + assert title in response.text + assert "

" in response.text diff --git a/Listing_10.31.txt b/Listing_10.31.txt new file mode 100644 index 0000000..5c79f54 --- /dev/null +++ b/Listing_10.31.txt @@ -0,0 +1,16 @@ +(venv) $ pytest +============================= test session starts ============================== +collected 3 items + +tests/test_site_pages.py FFF [100%] + +=================================== FAILURES =================================== +__________________________________ test_index __________________________________ +. +. +. +=========================== short test summary info ============================ +FAILED tests/test_site_pages.py::test_index - assert 'Learn Enough Pyt... +FAILED tests/test_site_pages.py::test_about - assert '<title>Learn Enough Pyt... +FAILED tests/test_site_pages.py::test_palindrome - assert '<title>Learn Enoug... +============================== 3 failed in 0.03s =============================== diff --git a/Listing_10.32.py b/Listing_10.32.py new file mode 100644 index 0000000..c816a9f --- /dev/null +++ b/Listing_10.32.py @@ -0,0 +1,38 @@ +import os + +from flask import Flask, render_template + + +def create_app(test_config=None): + """Create and configure the app.""" + app = Flask(__name__, instance_relative_config=True) + + if test_config is None: + # Load the instance config, if it exists, when not testing. + app.config.from_pyfile("config.py", silent=True) + else: + # Load the test config if passed in. + app.config.from_mapping(test_config) + + # Ensure the instance folder exists. + try: + os.makedirs(app.instance_path) + except OSError: + pass + + @app.route("/") + def index(): + return render_template("index.html", page_title="Home") + + @app.route("/about") + def about(): + return render_template("about.html", page_title="About") + + @app.route("/palindrome") + def palindrome(): + return render_template("palindrome.html", + page_title="Palindrome Detector") + + return app + +app = create_app() diff --git a/Listing_10.33.html b/Listing_10.33.html index 809c2ec..87e1e77 100644 --- a/Listing_10.33.html +++ b/Listing_10.33.html @@ -3,27 +3,6 @@ <head> <meta charset="utf-8"> <title>Learn Enough Python Sample App | {{ page_title }} - - - - - -
-
- -
-
- {% block content %}{% endblock %} -
-
- - + . + . + . diff --git a/Listing_10.34.txt b/Listing_10.34.txt new file mode 100644 index 0000000..c8a9871 --- /dev/null +++ b/Listing_10.34.txt @@ -0,0 +1,7 @@ +(venv) $ pytest +============================= test session starts ============================== +collected 3 items + +tests/test_site_pages.py ... [100%] + +============================== 3 passed in 0.01s =============================== diff --git a/Listing_10.35.html b/Listing_10.35.html new file mode 100644 index 0000000..809c2ec --- /dev/null +++ b/Listing_10.35.html @@ -0,0 +1,29 @@ + + + + + Learn Enough Python Sample App | {{ page_title }} + + + + + +
+
+ +
+
+ {% block content %}{% endblock %} +
+
+ + diff --git a/Listing_10.36.py b/Listing_10.36.py new file mode 100644 index 0000000..58b21c5 --- /dev/null +++ b/Listing_10.36.py @@ -0,0 +1,24 @@ +def test_index(client): + response = client.get("/") + assert response.status_code == 200 + base_title = "Learn Enough Python Sample App" + title = f"{base_title} | Home" + assert title in response.text + assert "

" in response.text + assert "