Skip to content

Commit 0b29ad7

Browse files
authored
Merge pull request #505 from realpython/mvc-legos
Add materials for Lego MVC resurfacing
2 parents 109a1d3 + 06110e4 commit 0b29ad7

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

mvc-legos/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Model-View-Controller (MVC) in Python Web Apps: Explained With Legos
2+
3+
This folder contains a minimal Flask example app that supports the explanation of the MVC pattern in [Model-View-Controller (MVC) Explained – With Legos](https://realpython.com/the-model-view-controller-mvc-paradigm-summarized-with-legos/).
4+
5+
To run the app, you'll need to first install [Flask](https://flask.palletsprojects.com/), preferably into a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/):
6+
7+
```sh
8+
(venv) $ python -m pip install Flask
9+
```
10+
11+
To create and see the SQLite database file, you can then run `create_db.py`:
12+
13+
```sh
14+
(venv) $ python create_db.py
15+
```
16+
17+
Finally, you can start the Flask app by executing `app.py`:
18+
19+
```sh
20+
(venv) $ python app.py
21+
```
22+
23+
When you make a request in your browser by typing the URL of your localhost and port 8000, you'll see how Flask renders the single view that this web app defines.

mvc-legos/app.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sqlite3
2+
3+
from flask import Flask, g, render_template
4+
5+
DATABASE = "./database.db"
6+
7+
app = Flask(__name__)
8+
9+
10+
def get_db():
11+
db = getattr(g, "_database", None)
12+
if db is None:
13+
db = g._database = sqlite3.connect(DATABASE)
14+
return db
15+
16+
17+
@app.route("/")
18+
def home():
19+
"""Searches the database for entries, then displays them."""
20+
db = get_db()
21+
cur = db.execute("SELECT * FROM entries ORDER BY id DESC;")
22+
entries = cur.fetchall()
23+
print(entries)
24+
return render_template("index.html", entries=entries)
25+
26+
27+
if __name__ == "__main__":
28+
app.run(host="0.0.0.0", port=8000, debug=True)

mvc-legos/create_db.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Creates and seeds a SQLite example database."""
2+
import sqlite3
3+
4+
DATABASE = "./database.db"
5+
6+
with sqlite3.connect(DATABASE) as db:
7+
try:
8+
# Create a table in the database
9+
cursor = db.cursor()
10+
cursor.execute(
11+
"""
12+
CREATE TABLE IF NOT EXISTS entries (
13+
id INTEGER PRIMARY KEY AUTOINCREMENT,
14+
title TEXT NOT NULL,
15+
text TEXT
16+
);
17+
"""
18+
)
19+
20+
# Insert a new entry to the database
21+
cursor.execute(
22+
"""
23+
INSERT INTO entries (title, text) VALUES (?, ?)
24+
""",
25+
("spaceship", "My MVP MVC Lego spaceship 🚀"),
26+
)
27+
28+
db.commit()
29+
except sqlite3.Error as e:
30+
print(f"An error occurred: {e}")

mvc-legos/database.db

12 KB
Binary file not shown.

mvc-legos/requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
blinker==1.7.0
2+
click==8.1.7
3+
flask==3.0.2
4+
itsdangerous==2.1.2
5+
jinja2==3.1.3
6+
markupsafe==2.1.5
7+
werkzeug==3.0.1

mvc-legos/templates/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<html lang="en">
2+
<head>
3+
<meta charset="UTF-8">
4+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5+
<title>Lego Builds</title>
6+
</head>
7+
<body>
8+
<h1>My Favorite Lego Builds</h1>
9+
<p>Here's a list of all my favorite Lego builds:</p>
10+
<ul>
11+
{% for entry in entries %}
12+
<li>
13+
<strong>{{ entry[1] }}:</strong> <!-- title -->
14+
{{ entry[2] }} <!-- text -->
15+
</li>
16+
{% else %}
17+
<li>No entries yet. Add some!</li>
18+
{% endfor %}
19+
</ul>
20+
</body>
21+
</html>

0 commit comments

Comments
 (0)