Due Mon, 11/02 at 11:59pm
This lab is a guided introduction to MVC. You will learn what the Model-View-Controller (MVC) pattern is for by working in a course search web app (the same idea as Project 1), and make one small change in each of the Model, View, and Controller.
1. The Big Idea of MVC: Separation of Concerns
A concern is one job a program has to do. A course search has at least three:
- Knowing about courses: what a course is, and the rules for it (does this course match “data”?)
- Showing courses: laying out a page, a table, colors, and buttons
- Handling requests: noticing that the user searched for something, getting the courses, and deciding what to show
You could write all of this in one big function that downloads the courses, filters them, and builds the HTML as it goes. It would work. The trouble starts when you need to change something. If you want to restyle the page, you are editing the same code that decides which courses match, and one slip can break the search. If two teammates work on the same file, they step on each other. If you want to test the matching rule, you can’t, because it is tangled up with the downloading and the HTML.
Separation of concerns is the idea that fixes this: give each concern its own place, so that a change to one concern doesn’t force changes to the others.
Model-View-Controller (MVC) is a popular way to separate concerns in an app that shows data to a user:
| Part | Concern | Question it answers |
|---|---|---|
| Model | The data and the rules for it | ”What is a course, and what can it tell me about itself?” |
| View | How the data is shown | ”How should this list of courses look?” |
| Controller | Connecting the other two | ”The user asked for X. What do I get, and what do I show?” |
Two rules make the pattern work:
- The Model knows nothing about how it is displayed, and the View knows nothing about how the data is stored or filtered. They never talk to each other directly.
- The Controller sits in the middle. It takes the request, uses the Model to get what is needed, and hands the result to the View.
You have already seen this idea in Project 1. A filter like matches_title knew nothing about the Textual screen, and the screen knew nothing about how titles were matched. Textual’s app.py connected them.
A test for separation: to add a “room number” to the course search, you should only have to change the Model (so a course has a room) and the View (so the page shows it). If the change leaks into places that have nothing to do with it, the concerns were mixed together.
2. Get Situated
Note: If you were here on Tuesday (when we went over processes, threads, and asyncio), you already pulled down the code so you should be good to go.
On GitHub: sync your fork of the class-exercises-fall2026 repo.
On your local machine: make sure all previous work is committed, then:
git checkout main
git pull
git checkout -b lab07-b
You should see a new lab07 folder containing an app directory.
Before moving on
git branch shows * lab07-blab07/app/ with models.py, server.py, unca_api.py, and a templates folder3. MVC and the Course Search App
Now let’s find the three parts in the course search app:
| MVC part | Its job | In the course search app |
|---|---|---|
| Model | Represents the data: one thing (here, one course) and what it knows about itself | app/models.py defines a Course and decides whether that course matches a search |
| View | Shows the data | app/templates/index.html turns courses into a web page |
| Controller | Connects the two | app/server.py receives the request, gets the courses, asks each Course whether it matches, and hands the results to the View |
The app has one more file, app/unca_api.py. It is not part of MVC: it just downloads the raw course data from UNCA and turns each record into a Course.
If you worked on Project 1, this will look familiar. Course.matches_title is the same idea as the filters you wrote there. The difference is that here the rule lives on the Model (the course itself), which is separate from the screen (the View), and a Controller connects them. In Project 1’s Textual app, app.py plays the Controller role.
You will see
async defandawaitinunca_api.pyandserver.py. They let the server keep working while it waits for UNCA to respond. You do not need to change them in this lab.
3.1 Run the app
cd lab07
poetry install
cd app
poetry run uvicorn server:app --reload
What does uvicorn do?
- uvicorn is the web server that runs the FastAPI app. It listens for browser requests and passes them to your code. The web server runs as a service that is always listening on its assigned port (in this case, port 8080).
server:appmeans “theappobject insideserver.py”. Theappobject is an instance of the FastAPI class, and it holds everything about your web app.--reloadrestarts the server automatically whenever you save a file, so you don’t have to stop and start it by hand.
Visit http://127.0.0.1:8000, then search for a title such as data.
Before moving on
poetry install finished without errorsdata shows a table of courses3.2 Trace a request
Open all three files in app/ and follow one search from start to finish:
- The form in
index.htmlsends the browser to/?title=data. home()inserver.pyreceives thetitleand gets the list of courses (unca_api.pydownloads them and builds aCoursefor each).filter_courses()inserver.pyasks eachCoursewhethermatches_title()is true, and keeps the ones that match.home()hands the matching courses toindex.html, which loops over them and builds the table.
You can also see the Controller’s results without the View at http://127.0.0.1:8000/api?title=data. It returns the same results as JSON instead of HTML.
Before moving on
unca_api.py is not one of the three MVC parts4. Make a Change in Each Part
Add a department filter (for example, CSCI) to the search. It takes one small change in each MVC part.
4.1 Model: app/models.py
Add a matches_department(self, value) method to Course, next to matches_title. Follow the same rules the Project 1 filters use:
- Ignore capitalization and spaces around
value. - A blank
valuereturnsTrue(no restriction). - Otherwise, return
Trueonly when this course’sdepartmentequalsvalue.
Before moving on
matches_department is a method inside Course, indented under the classTrueserver.py or index.html yet4.2 Controller: app/server.py
In both endpoints (/ and /api):
- Accept a
department: str = Query("")parameter. - Add a
departmentparameter tofilter_courses(), keep a course only if it matches the title and the department, and passdepartmentalong from both endpoints. - In
/, also passdepartmentto the View in the dictionary that goes to the template.
Before moving on
CSCI coursestitle=data alone/ page still loads without errorsThe Controller doesn’t know how to match a department. It just asks each
Course. If the rule for matching changes later, onlymodels.pyhas to change.
4.3 View: app/templates/index.html
- Add a text input named
departmentto the form (copy the title input and keep itsvalue="{{ department }}"so the box remembers what you typed). - Add a Dept column to the table that shows
{{ course.department }}.
Reload the page and search for the department csci. Then try the title data and the department csci together. You can test the Model and Controller before touching the View at http://127.0.0.1:8000/api?department=csci.
Before moving on
department box that keeps what I typed after I searchdata and department csci together shows only CSCI courses5. Reflect
Answer these in an answers.md file in your lab07 folder (a few sentences each):
- For each of the three changes in Part 4, which file did you edit, and why that one?
- Why does a
Courseknow whether it matches a search but never decide how it looks? - If you wanted to show each course’s room number, which files would change? Which would not?
- Name one thing that would have been harder if the downloading, matching, and page building were all in one function.
Extra Tasks
- Add a
creditsfilter (for example, only 3-credit courses), following the same Model, Controller, View steps. - Show a “Full” label instead of
0in the seats open column. - Add a term drop-down in the View, and have the Controller download only the chosen term.
Hints for the extra tasks
- Credits filter:
- Copy
matches_departmentinmodels.pyand compare credits instead. Credits are probably a number, so convert the search value withint()and decide what a blank or invalid value should do. - A form value always arrives as a string, so compare like with like.
- Then repeat the steps from Part 4: Model, then Controller (both endpoints), then View. Test with
/api?credits=3before touching the template.
- Copy
- “Full” label:
- Do this in
index.htmlwith a Jinja{% if %}: if the seats open value is0, printFull, otherwise print the number. - Jinja has
{% if ... %}{% else %}{% endif %}, just like Python’sif/else.
- Do this in
- Term drop-down:
- Use a
<select name="term">with an<option value="...">for each term. Mark the current one withselectedso the box remembers it. - Add a
termquery parameter in the Controller and pass it to the function inunca_api.pythat downloads courses. Look at how that function builds its request to see where the term goes. - Pass
termback to the template, like you did withdepartment.
- Use a
Design Tasks
These only change the View (index.html and CSS). The Model and Controller stay the same, which is the point of MVC.
- Replace the table with a grid of cards, one per course. Each card should show the course title, department and number, and seats open. Use CSS Grid or Flexbox so the cards wrap to fit the window.
- Make the cards respond to screen size: one column on a phone, two on a tablet, three or more on a desktop.
- Style the seats open count so it is easy to scan, for example green when seats are open and red or gray when the course is full.
- Add a hover effect to the cards (a shadow or a slight lift), and make sure the text still has enough contrast to read.
Hints for the design tasks
- Cards:
- Replace the
<table>with a container<div class="cards">and loop inside it:{% for course in courses %}<div class="card">...</div>{% endfor %}. - Reuse the same
{{ course.title }}-style expressions that the table cells use. - Make the container a grid with
display: grid; gap: 1rem;.
- Replace the
- Responsive layout:
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));makes the cards wrap on their own, with no media queries.- If you want exact column counts, use
@media (min-width: 600px) { ... }to changegrid-template-columns. - Resize your browser window, or use the browser’s device toolbar, to test.
- Seats styling:
- Add a CSS class in the template based on the value:
<span class="{% if course.seats_open > 0 %}open{% else %}full{% endif %}">. Check the name of the seats attribute inmodels.pyfirst. - Then style
.openand.fullwith different colors. Don’t rely on color alone: keep the number or the “Full” label too.
- Add a CSS class in the template based on the value:
- Hover effect:
- Use
.card:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); }, and addtransition: transform 0.15s, box-shadow 0.15s;to.cardso it animates. - Check text contrast with your browser’s dev tools (the color picker shows a contrast ratio).
- Use
Submission
Before you submit, make sure that:
- All three changes (Model, Controller, View) work together in the browser
lab07/answers.mdhas your answers to the four Reflect questions
- Commit and push your
lab07-bbranch. - Create a pull request to
main. - Submit the PR link in Canvas.
Resources: