From b32cf367ab2b5f472834ebdc2b7c025e6d8cc908 Mon Sep 17 00:00:00 2001 From: martin-martin Date: Wed, 28 Feb 2024 15:52:46 +0100 Subject: [PATCH 1/2] Add materials for Lego MVC resurfacing --- mvc-legos/README.md | 23 +++++++++++++++++++++++ mvc-legos/app.py | 28 ++++++++++++++++++++++++++++ mvc-legos/create_db.py | 30 ++++++++++++++++++++++++++++++ mvc-legos/database.db | Bin 0 -> 12288 bytes mvc-legos/requirements.txt | 7 +++++++ mvc-legos/templates/index.html | 21 +++++++++++++++++++++ 6 files changed, 109 insertions(+) create mode 100644 mvc-legos/README.md create mode 100644 mvc-legos/app.py create mode 100644 mvc-legos/create_db.py create mode 100644 mvc-legos/database.db create mode 100644 mvc-legos/requirements.txt create mode 100644 mvc-legos/templates/index.html diff --git a/mvc-legos/README.md b/mvc-legos/README.md new file mode 100644 index 0000000000..e1445b9b00 --- /dev/null +++ b/mvc-legos/README.md @@ -0,0 +1,23 @@ +# Model-View-Controller (MVC) Explained – With Legos + +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/). + +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/): + +```sh +(venv) $ python -m pip install Flask +``` + +To create and see the SQLite database file, you can then run `create_db.py`: + +```sh +(venv) $ python create_db.py +``` + +Finally, you can start the Flask app by executing `app.py`: + +```sh +(venv) $ python app.py +``` + +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. diff --git a/mvc-legos/app.py b/mvc-legos/app.py new file mode 100644 index 0000000000..f8146c74ca --- /dev/null +++ b/mvc-legos/app.py @@ -0,0 +1,28 @@ +import sqlite3 + +from flask import Flask, g, render_template + +DATABASE = "./database.db" + +app = Flask(__name__) + + +def get_db(): + db = getattr(g, "_database", None) + if db is None: + db = g._database = sqlite3.connect(DATABASE) + return db + + +@app.route("/") +def home(): + """Searches the database for entries, then displays them.""" + db = get_db() + cur = db.execute("SELECT * FROM entries ORDER BY id DESC;") + entries = cur.fetchall() + print(entries) + return render_template("index.html", entries=entries) + + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8000, debug=True) diff --git a/mvc-legos/create_db.py b/mvc-legos/create_db.py new file mode 100644 index 0000000000..7deed809e6 --- /dev/null +++ b/mvc-legos/create_db.py @@ -0,0 +1,30 @@ +"""Creates and seeds a SQLite example database.""" +import sqlite3 + +DATABASE = "./database.db" + +with sqlite3.connect(DATABASE) as db: + try: + # Create a table in the database + cursor = db.cursor() + cursor.execute( + """ + CREATE TABLE IF NOT EXISTS entries ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + title TEXT NOT NULL, + text TEXT + ); + """ + ) + + # Insert a new entry to the database + cursor.execute( + """ + INSERT INTO entries (title, text) VALUES (?, ?) + """, + ("spaceship", "My MVP MVC Lego spaceship 🚀"), + ) + + db.commit() + except sqlite3.Error as e: + print(f"An error occurred: {e}") diff --git a/mvc-legos/database.db b/mvc-legos/database.db new file mode 100644 index 0000000000000000000000000000000000000000..e749e18a4739a9aa34e11adb030873140cc25200 GIT binary patch literal 12288 zcmeI&&r8EF6bJC68;S$1w;guy9XB?-=w0gwQf#|fGjS)W8nv)>KN>-A>Yw4of5ZR7 zf5gMUtJ&OyRbe+_@O|_(P5Khr&n=}V$6l-zUH69FNYf76WSp~oBF30yGG}rY++5^m z9n+5ci^5{t$=kZAvNk@M?V1w;0uX=z1Rwwb2tWV=5P$##An<3oCt@5^xE;=e5 zbj%+3JXM1`m0V`Kxq2uZDM&g8o}f%^If=TeW@c8NcKK?tRN~KjlQmVMhp|ehjWxH% zbdbsg%0zrcZXm^>2&oymz7w|TNVLgm$%Y%4HGC1s+OJtU)*VGsoJk5ACR?8ON0GYM zlbX5dmGR!3+waUA2nav`0uX=z1Rwwb2tWV=5P$##mPeq<^K8pa`;qySaU1vj2l7vw zChO#>n;!k#ppVy=$M60Bh}om%c|klW1Rwwb2tWV=5P$##AOHafKmY=Z1qwV@TATia F@CD*2XzKs~ literal 0 HcmV?d00001 diff --git a/mvc-legos/requirements.txt b/mvc-legos/requirements.txt new file mode 100644 index 0000000000..ba62ec0679 --- /dev/null +++ b/mvc-legos/requirements.txt @@ -0,0 +1,7 @@ +blinker==1.7.0 +click==8.1.7 +flask==3.0.2 +itsdangerous==2.1.2 +jinja2==3.1.3 +markupsafe==2.1.5 +werkzeug==3.0.1 diff --git a/mvc-legos/templates/index.html b/mvc-legos/templates/index.html new file mode 100644 index 0000000000..cbc703ff07 --- /dev/null +++ b/mvc-legos/templates/index.html @@ -0,0 +1,21 @@ + + + + + Lego Builds + + +

My Favorite Lego Builds

+

Here's a list of all my favorite Lego builds:

+ + + \ No newline at end of file From 06110e4d4c529ff9a6b7b9198872ce283c949c02 Mon Sep 17 00:00:00 2001 From: brendaweles <160772586+brendaweles@users.noreply.github.com> Date: Mon, 11 Mar 2024 12:42:11 -0600 Subject: [PATCH 2/2] Update README.md --- mvc-legos/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mvc-legos/README.md b/mvc-legos/README.md index e1445b9b00..fa9c0c191d 100644 --- a/mvc-legos/README.md +++ b/mvc-legos/README.md @@ -1,4 +1,4 @@ -# Model-View-Controller (MVC) Explained – With Legos +# Model-View-Controller (MVC) in Python Web Apps: Explained With Legos 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/).