A document from MCS 275 Spring 2024, instructor Emily Dumas. You can also get the notebook file.

MCS 275 Spring 2024 Homework 12

  • Course Instructor: Emily Dumas

Deadline

This homework assignment must be submitted in Gradescope by Noon central time on Tuesday April 9, 2024.

Collaboration

Collaboration is prohibited, and while working on this you should only consult the resources (books, online, etc.) listed below.

Content

This assignment corresponds to Worksheet 12 and is about Flask web applications.

Resources you may consult

The materials you may refer to for this homework are:

Point distribution

This homework assignment has a single problem. The grading breakdown is:

Points Item
4 Autograder syntax checks (problem 1)
10 Problem 2
14 Total

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

Advisory note

You'll need to have Flask installed, and you will work with SQLite in this assignment.

As a reminder, most people can install Flask using

python3 -m pip install Flask

in the terminal.

The fact that we are using SQLite means it is more important than ever to run your programs in the terminal, after manually changing to the directory containing your script (with cd). Using the "run" button in VS code is likely to cause problems and unexpected behavior.

2. First come, first served

Write a Flask web application that supports the following URLs and behaviors. (When I list a URL as e.g. /asdf/ it is an abbreviation for the full URL that would look something like http://localhost:5000/asdf/.)

  • /<n>/ where n is any positive integer - If this URL has never been visited before (see below for how to determine this), displays a HTML document with a large header reading "Congratulations, you were first!". But if this URL has been visited before, it instead displays a message like "Sorry, this integer was claimed at 10:38AM on April 04, 2024." Of course, the date/time in this example message is replaced with the actual time of the first visit to that URL.

  • / - The front page of the application is an HTML document that has a header reading "First come, first served", followed by an explanation that the user can visit /<n>/ for any integer n to attempt to claim that number. It should then say that the user can also try one of the sample integers below, followed by a bulleted list similar to this:

    • /5782/
    • /1729/
    • /42/
    • /9718/
    • /210/

      but these numbers should be replaced by five random integers less than 10000, chosen again each time the front page is served. Each item in the list should link to the corresponding page in the application.

In other words, this application treats positive integers as things you can 'claim' by visiting a url like /2752024/. The first such visit stakes a claim, and anyone else visiting the URL from then onward will be told it is already taken. The front page just gives you an easy way to visit a few randomly chosen URLs of that form.

Requirements for how the application does this:

  • This application must use a SQLite database table to keep track of which integers have been visited, and when the first visit happened. The application should create the database and table if it doesn't exist when the program is first run. If it does exist, though, the application should use (and add to) the data already in the table. That means the application can tell whether a URL has been visited even if it happened during a previous time the program was run.

  • The only mutable global variable you are allowed to create is the flask.Flask object representing your application. (We usually call it app.) Any data that needs to remain available from one request to the next needs to be stored in the database.

  • Every function that handles HTTP requests should establish, use, and close its own database connection.

Revision history

  • 2024-04-04 Initial publication