This worksheet focuses on Flask web applications that use databases.
These things might be helpful while working on the problems. Remember that for worksheets, we don't strictly limit what resources you can consult, so these are only suggestions.
Download and extract this zip file which contains a partially-written Flask web application.
In short, the application provides:
Read below for an explanation of what the app is meant to do, and what part is missing. Then, add the missing code.
SpeakerSlate is an application allowing speakers who have been invited to a conference to choose their speaking times on a first-come first-served basis. Initially, all of the conference's speaking times are open. As speakers visit the site and reserve their times, their choices are recorded in a database.
Specifically, a SQLite database stores rows representing the speaking slots at the conference, containing columns for the datetime (e.g.
"Saturday 9am"), the speaker (
NULL if this slot is available, or a name like
"David Dumas" if it has been assigned), and an integer
slotid that uniquely identifies the row.
In addition to providing a way for speakers to choose a speaking time, the application also lets anyone view a list of lectures that have been scheduled. There is also a feature meant for administrators that will delete all existing lecture time assignments.
/is rendered from
templates/front.html. It is generated by a function
speakerslate.py. It looks like this:
/userinfo/which results in the form shown below, rendered from
templates/userinfo.html. It is generated by a function
/choose/which must be rendered from
templates/choose.htmlafter making a SQL query to determine what speaking slots are available. The HTML template is provided, but you must write the associated function
slotid6 to speaker
David). You must write the associated function
speakerslate.py. This function performs an action, but then redirects the browser to the schedule page (
/schedule/). Thus, while you'll need to write some code, this route will not involve rendering any template.
/schedule/is available in two ways: It is linked directly from the front page, and it is the destination the browser is redirected to after a speaker chooses their time. It is rendered from
templates/schedule.html. It looks like this:
First, try running the application
speakerslate.py in its current form. See what works and what doesn't work.
Then, take a look at the code in
speakerslate.py. Look at the existing routes and try to understand how they work. The function for
/schedule/ will probably be especially helpful, as it is quite similar to
Check the database reset code for information about the table and column names.
templates/choose.html, since this is going to be rendered by a function you write. Determine what variables it uses, and what values they are supposed to contain. Design database queries to retrieve those values.
Add a new feature to SpeakerSlate, where additional speaking times can be created. This should consist of:
/newslot/, asking for the datetime of the new speaking slot. This application treats datetimes as opaque strings; there's no special handling or parsing with Python's
datetimemodule. When submitted, this form should make a POST request to
/createslot/that actually INSERTs a new row into the database.