This worksheet focuses on Flask web applications and anonymous functions.
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.
The Flask application written in lecture was called OrderNova in the 12pm section, and TrackFlow in the 1pm section. The applications are quite similar, and use a SQLite database to manage a queue of work orders that can be created, assigned to workers, and marked as completed.
To prepare for the next problem, download a copy of the Flask application for your section and get it running on your computer.
The application has a number of files, so you can do it in either of these ways:
Note: There was an issue in timefmt.py
that caused the application to return 500 Internal Server Error messages on some versions of Windows. I fixed this in an update on April 18. Thanks to Johnny Joyce for finding the cause of this problem.
trackflow.py
- Main programcreatedb.py
- Utility to reset databasetimefmt.py
- Module for time formattingstatic/
- Subdirectory for static filesstatic/trackflow.css
- Main stylesheettemplates/
- Subdirectory for templatestemplates/tf-showorder.html
- Template for displaying work order statustemplates/tf-workerview.html
- Template for displaying worker viewtemplates/tf-neworder.html
- Template for new work order formordernova.py
- Main programcreatedb.py
- Utility to reset databasetimefmt.py
- Module for time formattingstatic/
- Subdirectory for static filesstatic/ordernova.css
- Main stylesheettemplates/
- Subdirectory for templatestemplates/on-showorder.html
- Template for displaying work order statustemplates/on-workerview.html
- Template for displaying worker viewtemplates/on-neworder.html
- Template for new work order formOpen a terminal and cd
to the location of the main .py
script (ordernova.py
or trackflow.py
). Run that script with Python, e.g. using a command
python3 ordernova.py
or similarly with your interpreter name and possibly replacing ordernova
with trackflow
.
You should see output similar to this:
The database 'ordernova.sqlite' was not found. Creating it.
Making sure the DB contains the necessary tables...Done
Populating DB with sample data, since it was empty...Done
* Serving Flask app 'OrderNova'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
Notice the port number 5000
on the second-to-last line. It might not be 5000
for you, but you need to know it.
Now, open a browser and visit http://localhost:5000/
, replacing 5000
with the port number as needed.
You should see a simple white home page with links to some of the application endpoints. Try them out. Create a work order. Take it on as an assignment. Complete it.
Modify the work order tracking application to add these new features. Test them to make sure they work.
This application doesn't have a table to store workers, but you can generate a list of workers that have appeared as the assigned worker for some task in the table (whether completed or not).
Make a route /workerlist/
that generates a page styled similarly to the work order view page, but which gives a bulleted list of all the workers currently in the database. Each worker's name should only appear once. The worker's username should be a link to their worker view page.
So for example you might see a list like this:
And the first item would link to /worker/ddumas/
, and the next one to /worker/gwashington/
Here's a function that can take a day described by its year, month, and day numbers and return two time stamps ts0,ts1
so that a timestamp x
lies within that day if and only if ts0 <= x < ts1
.
import datetime
def timestamp_range_for_day(year,month,day):
"""
Return the timestamps when a calendar day
begins and ends.
"""
ts0 = datetime.datetime(year,month,day).timestamp()
ts1 = (datetime.datetime(year,month,day)+datetime.timedelta(days=1)).timestamp()
return ts0,ts1
Use this to add the following feature to the work order tracking application: Visiting a URL of the form /reports/day/2023/04/15/
will show a page listing the work orders created on that day, assigned on that day, and completed on that day. These should be in three separate sections, and modeled after the worker view template.
Use anonymous functions (lambda
) with Python's sorted()
, max()
, or min()
functions to answer these questions.
Generate the first 20 powers of 7 (meaning $7^1$ to $7^{20}$) and sort them according to how many different decimal digits they use. That is, 343 would appear near the start of the list since it uses only two distinct digits, while 1628413597910449 would appear near the end, as it uses all 10 digits.
Here's a text file with 10000 English words, one per line:
Let's say the endscore of a word is the number of times its last letter appears in the word. For example, "plasma" has an endscore of 2 because the last letter, a, appears twice. And "associates" has an endscope of 3 because it contains 3 copies of the last letter, s.
Using custom sorting and lambda
, find 20 words with the highest endscores in this list.
Use the same word list as the last problem. Suppose we say the variety of a word is the ratio of the number of distinct letters to the length of the word. For example, "cameras" has 6 distinct letters and has a length of 7, so its variety is $\frac67 \approx 0.85714$.
Among words with at least four letters, find one that has the lowest variety.