Project 2: Habit Tracker: Adding Flask blueprints (+67, -56)

app.py (+6, -52)

From: curriculum/section10/lectures/07_flask_blueprints/start/app.py

To: curriculum/section10/lectures/07_flask_blueprints/end/app.py

            
            index ad5b4c3..8c8ddb0 100644
--- a/curriculum/section10/lectures/07_flask_blueprints/start/app.py
+++ b/curriculum/section10/lectures/07_flask_blueprints/end/app.py
@@ -1,55 +1,9 @@
-import datetime
-from collections import defaultdict
-from flask import Flask, render_template, request, redirect, url_for
+from flask import Flask
+from routes import pages
 
-app = Flask(__name__)
-habits = ["Test habit"]
-completions = defaultdict(list)
 
+def create_app():
+    app = Flask(__name__)
+    app.register_blueprint(pages)
 
-@app.context_processor
-def add_calc_date_range():
-    def date_range(start: datetime.date):
-        dates = [start + datetime.timedelta(days=diff) for diff in range(-3, 4)]
-        return dates
-
-    return {"date_range": date_range}
-
-
-@app.route("/")
-def index():
-    date_str = request.args.get("date")
-    if date_str:
-        selected_date = datetime.date.fromisoformat(date_str)
-    else:
-        selected_date = datetime.date.today()
-
-    return render_template(
-        "index.html",
-        habits=habits,
-        selected_date=selected_date,
-        completions=completions[selected_date],
-        title="Habit Tracker - Home",
-    )
-
-
-@app.route("/complete", methods=["POST"])
-def complete():
-    date_string = request.form.get("date")
-    date = datetime.date.fromisoformat(date_string)
-    habit = request.form.get("habitName")
-    completions[date].append(habit)
-
-    return redirect(url_for("index", date=date_string))
-
-
-@app.route("/add", methods=["GET", "POST"])
-def add_habit():
-    if request.form:
-        habits.append(request.form.get("habit"))
-
-    return render_template(
-        "add_habit.html",
-        title="Habit Tracker - Add Habit",
-        selected_date=datetime.date.today(),
-    )
+    return app
        

routes.py (+57, -0)

From: curriculum/section10/lectures/07_flask_blueprints/end/routes.py

To: curriculum/section10/lectures/07_flask_blueprints/end/routes.py

            
            new file mode 100644
index 0000000..be8e7c6
--- /dev/null
+++ b/curriculum/section10/lectures/07_flask_blueprints/end/routes.py
@@ -0,0 +1,57 @@
+import datetime
+from collections import defaultdict
+from flask import Blueprint, render_template, request, redirect, url_for
+
+pages = Blueprint(
+    "habits", __name__, template_folder="templates", static_folder="static"
+)
+habits = ["Test habit"]
+completions = defaultdict(list)
+
+
+@pages.context_processor
+def add_calc_date_range():
+    def date_range(start: datetime.date):
+        dates = [start + datetime.timedelta(days=diff) for diff in range(-3, 4)]
+        return dates
+
+    return {"date_range": date_range}
+
+
+@pages.route("/")
+def index():
+    date_str = request.args.get("date")
+    if date_str:
+        selected_date = datetime.date.fromisoformat(date_str)
+    else:
+        selected_date = datetime.date.today()
+
+    return render_template(
+        "index.html",
+        habits=habits,
+        selected_date=selected_date,
+        completions=completions[selected_date],
+        title="Habit Tracker - Home",
+    )
+
+
+@pages.route("/complete", methods=["POST"])
+def complete():
+    date_string = request.form.get("date")
+    date = datetime.date.fromisoformat(date_string)
+    habit = request.form.get("habitName")
+    completions[date].append(habit)
+
+    return redirect(url_for(".index", date=date_string))
+
+
+@pages.route("/add", methods=["GET", "POST"])
+def add_habit():
+    if request.form:
+        habits.append(request.form.get("habit"))
+
+    return render_template(
+        "add_habit.html",
+        title="Habit Tracker - Add Habit",
+        selected_date=datetime.date.today(),
+    )
        

index.html (+1, -1)

From: curriculum/section10/lectures/07_flask_blueprints/start/templates/index.html

To: curriculum/section10/lectures/07_flask_blueprints/end/templates/index.html

            
            index c58b354..81e1478 100644
--- a/curriculum/section10/lectures/07_flask_blueprints/start/templates/index.html
+++ b/curriculum/section10/lectures/07_flask_blueprints/end/templates/index.html
@@ -15,7 +15,7 @@
             </div>
         {% else %}
             <div class="habit">
-                <form method="POST" class="habit__form" action="{{ url_for('complete') }}">
+                <form method="POST" class="habit__form" action="{{ url_for('habits.complete') }}">
                 <input type="hidden" id="habitName" name="habitName" value="{{ habit }}" />
                 <input type="hidden" id="date" name="date" value="{{ selected_date }}" />
                 <button type="submit" class="habit__button">
        

layout.html (+3, -3)

From: curriculum/section10/lectures/07_flask_blueprints/start/templates/layout.html

To: curriculum/section10/lectures/07_flask_blueprints/end/templates/layout.html

            
            index e2e0788..d865b0b 100644
--- a/curriculum/section10/lectures/07_flask_blueprints/start/templates/layout.html
+++ b/curriculum/section10/lectures/07_flask_blueprints/end/templates/layout.html
@@ -13,9 +13,9 @@
     
     <body>
         <header class="header">
-            <h1 class="header__logo"><a href="{{ url_for('index') }}" class="header__link">Habits</a></h1>
+            <h1 class="header__logo"><a href="{{ url_for('habits.index') }}" class="header__link">Habits</a></h1>
             {% if "add" not in request.endpoint %}
-                <a href="{{ url_for('add_habit') }}" class="header__link">+ Add new</a>
+                <a href="{{url_for('habits.add_habit')}}" class="header__link">+ Add new</a>
             {% endif %}
         </header>
 
@@ -24,7 +24,7 @@
                 {% for date in date_range(selected_date) %}
                     <a 
                         class="dates__link {{ 'dates__link--current' if loop.index0 == 3 else ''}}"
-                        href="{{ url_for('index', date=date) }}"
+                        href="{{ url_for('habits.index', date=date) }}"
                     >
                         <time class="date" datetime="{{ date }}">
                             <span>{{ date.strftime("%a") }}</span>