cs304 logo

Flask

Today, we'll work with our first major middleware, namely Flask.

Goal

By the end of today, you will be able to use the Flask module to do development of web applications in your C9 workspace.

Plan

  1. Admin
  2. Python Catchup:
  3. Flask
  4. Routing
  5. Ports and Flask on Cloud 9
  6. Flask and MySQLdb

Python Scripts

We'll conclude the work we did last time, starting with some information about virtualenv.

Flask

In development mode, Flask creates a long-running process that accepts web requests (on a configurable port), figures out which function to call based on the incoming URL, and prints the return value of that function as the response.

In short, it's a web server, except that:

Ports and Flask on Tempest

If we are all running our Flask apps on Tempest, we can't all use port 5000 or 8080 or whatever. Each of you needs a different port. So, if we do this, I suggest using your UID. I'll show you how to determine your UID if we get to that. In any event, we would need to know how to modify the port that Flask runs on.

In Cloud9, we will have to modify the port anyhow, since Cloud9 restricts us to ports 8080, 8081 and 8082.

Thus, we'll change the active part of your app from this (see in thousands of online tutorials and such):

if __name__ == '__main__':
    app.debug = True
    app.run()

to this:

if __name__ == '__main__':
    app.debug = True
    app.run('0.0.0.0',8081)

The '0.0.0.0' means to have the app listen on the external interface of the C9 workspace. The default is to listen on the internal interface, which means only connections from the C9 virtual machine itself. An external interface accepts connections from other machines, including your browser.

First Example

Here's the code for our first example, example1.py:

[an error occurred while processing this directive]

I'll demonstrate how to run it.

First Exercise

  1. Use curl to download flask1.tar from https://cs.wellesley.edu/~cs304/downloads/flask1.tar.
  2. unpack it using tar xf flask1.tar
  3. That will create a directory for today. It will be a sibling of the virtual env we created last time.
  4. activate your virtualenv
  5. use pip to install flask
  6. cd to your flask1 folder
  7. Run the first example:
    python example1.py
  8. Did that work?
  9. Click on the offered URL and open the page
  10. Try different URLs by typing in the location bar of the browser. Try them more than once!
  11. Look in the terminal window where the app is running. You'll see a log of the requests. Error messages might get printed there as well.

Automatic Reloading

A very cool feature of development mode is that the web app monitors the example1.py file and will reload itself if the file changes. Try it!

  1. Edit the file to change the program a little. Say by adding your own name.
  2. Change the name in the file from my name to yours
  3. Save the result
  4. Check the terminal that is running the web app and notice the message about reloading.
  5. Refresh the browser window

Errors

Try putting an error in the code, just to see what happens.

When I first did the example, I omitted the n= in the .format() method. Try that.

In development mode, run-time errors will get printed to the console. You can also put print statements in there.

Certain kinds of errors can be debugged in the browser window, using a security key.

Exiting

When you're done using your Flask application, just kill it:

  1. Go to the terminal window that it's running in
  2. type control c (C-c)
  3. refresh the browser window, and you'll see that it's gone.

Routing

Routing Example

Let's look at the second example, example2.py:

[an error occurred while processing this directive]

I'll demo in my C9 workspace.

  1. Start it, as before
  2. What is the main URL?
  3. Go to a web browser and try the main URL.

Obviously, these functions can easy share code, as they do with the lucky() function.

Nevertheless, your main app file should focus on routing, not on supporting functionality. You can define modules and import them to do the heavy lifting. That keeps your controller file concise.

Variable Rules

Sometimes, our URLs can be patterns that can then be parsed and involved in the computation. Flask makes this easy.

Code from example3.py:

[an error occurred while processing this directive]

Summary

[an error occurred while processing this directive]