Lecture 36

Using Flask

MCS 275 Spring 2021
Emily Dumas

Lecture 36: Using Flask

Course bulletins:

  • Please install Flask, e.g. with
    python3 -m pip install Flask
    in preparation for using it in upcoming assignments.
  • Project 4 description likely to be posted tonight.

Today's goal

Build the database and Flask routes for our web apps.

Get a minimal working version of at least one of them running.

Chat app todo

  • ✓ HTML mockup
  • ✓ Stylesheet
  • ✓ (Learn a bit about Flask)
  • Database schema & test data
  • Python code to generate message view HTML from a database query
  • Add form to post a message to HTML
  • Create route for new message submission

Vote app todo

Everything from previous list and:

  • Second view (chrono vs score)
  • Routes for the + and - buttons

Chat DB schema

Table messages has columns:

  • id - integer uniquely identifying a message
  • sender - name of the sender
  • content - message content
  • ts - float giving time message received by server in seconds since unix epoch (0:00 Jan 1 1970 UTC)

Generating chat feed

  • SELECT query retrieves messages
  • Convert each row to a <div> using string formatting.
  • Add static parts of the HTML page (header and footer).
  • When a Flask route returns a string, Flask assumes it is HTML and delivers it as the response (with HTTP response code 200, and adding necessary headers).

References

Revision history

  • 2021-04-13 Move unused slides forward to Lecture 37
  • 2021-04-12 Initial publication