T1W9 - tstorrnetnz/teaching2025 GitHub Wiki
Introduction
We are very close to having the technical/code to make a working website. So far we have:
- Revised L2 Python
- Learned about L3 Python including classes and reading/writing to files
- Followed a tutorial on SQLAlchemy that allows you to create and manage a SQL database using only Python code, as well as create objects (of classes from entries in the database). Link to SQL notes here.
To be ready for your assessment we also need to be able to:
- Take input from a web page so that it changes/edits data in the database. This uses Python Flask
- Use more complex web page design
- Improve our project management skills
- Use better (easier and more productive) tools than Pythonanywhere
Better Tools
While Pythonanywhere is great for learning new techniques, other (free) tools are available for use to use for making our project. Note - it is possible to use Pythonanywhere for the project, but other tools will make the process a little easier and certainly more enjoyable. I use Visual Studio Code - and I suggest you do too. VSC can be installed on Windows, Mac, Linux and Chrome. The instructions for the first three can be found here. You will need to install Python, then VSC then the VSC Python extension. This sounds complicated but should be straightforward. Follow the whole of that page. For Chromebooks, try this short tutorial - and then follow the setup in the previous link for windows/macos/linux
Combining Flask and SQLAlchemy
For you to do:
We now need to complete a tutorial that uses Flask and a SQLite database to read data from the database and display it on web pages in Repl.
The Flask tutorial was useful - but used an externally created database. The SQlAlchemy tutorial created a database, but was not connected to website (Graphical User Interface, or GUI). We are going to combine these two together into a mini-project - later we will add the ability to edit the data. For you to do - a bit harder
To do this you must have first completed both the Python Flask tutorial and the SQLAlchemy tutorial.
Rather than giving individual steps, your task is to create a new project in VSC that combines the best of the Flask tutorial and the SQLAlchemy tutorial. The data will be about 5 people and the things they own. The data can be found in this file.
Your Repl needs:
A database created using SQLAlchemy, the tables need to be people and things.
The program needs to check if the database already exists - [this Repl](https://replit.com/@trevorstorr/sqlalchemytestmodified) is an example of how to do that. The comments at the top explain it.
You need to display 4 pages - A title page, a page called people that lists the data about the people (separately), a page called things that lists the things (separately) and a page called 'who owns what' that lists the objects that each person owns
All the data needs to come from database queries, not hard coded!
It would be nice - but not essential - to have the people and object classes as separate files.
This is how I would do this, but you may think otherwise.
Make the basic pages in Flask but with no database entries
Create the people and object class
In the main.py use SQLAlchemy to create the database and an if/else to not throw an error when rerunning the program
Add the code to display the data from the database into the correct pages you created earlier
Good luck!