Lecture 25

Using Flask

MCS 275 Spring 2023
David Dumas

Lecture 32: Using Flask

Reminders and announcements:

  • Please install Flask, e.g. with
    python3 -m pip install Flask
    in preparation for using it in upcoming assignments.
  • Start work on Project 4
  • Homework 12 posted

Today's goal

Build the worker view template

Create the database and link it to the Flask application

DB schema

Single table orders with columns:

  • woid - integer uniquely identifying a WO
  • description - string
  • assigned_user - either
    • username this WO is assigned to
    • NULL if not assigned
  • created_ts - "created timestamp" as time.time()
  • assigned_ts - "assigned timestamp", either:
    • time.time() when assigned
    • NULL if not assigned
  • completed_ts - "completed timestamp"
    • time.time() when completed
    • NULL if not completed

Constraint

A row only makes sense if created_ts is not NULL, and if assigned_ts and assigned_user are either both NULL or both non-NULL.

Generating worker view

Route /worker/ddumas/ results in:

  • SELECT query to get ddumas assigned WOs
  • SELECT query to get unassigned WOs
  • Rearrange/format query data for use in template
  • Render a page template to format the data as in mockup

Note part of the URL is an argument. The pattern is /worker/<username>/

Actions

Buttons on the worker view form need to perform actions.

We'll make Flask routes for this, e.g. /wo/58/assign_to/ddumas/.

Requesting that URL should perform an action. But what content will it serve?

Redirects

Every HTTP request results in a numerical status.

So far, we've seen 404 (NOT FOUND) and 200 (OK, generated by Flask when we return HTML).

There are also codes that instruct the browser to load another resource, e.g. 302 (FOUND).

Return flask.redirect("destination") to make this happen.

References

Revision history

  • 2022-04-11 Initial publication
  • 2023-04-07 Revised for 2023