Today, we'll work with our first major middleware, namely Flask.
By the end of today, you will be able to use the Flask module to do development of web applications in your C9 workspace.
We'll conclude the work we did last time, starting with some information about virtualenv.
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:
controller, I mean the logic that routes particular requests to particular handler functions. This is sometimes called a
routeror
dispatcher.
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.
Here's the code for our first example, example1.py:
[an error occurred while processing this directive]
I'll demonstrate how to run it.
flask1.tar
from https://cs.wellesley.edu/~cs304/downloads/flask1.tar.
tar xf flask1.tar
flask
flask1
folderpython example1.py
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!
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.
When you're done using your Flask application, just kill it:
control c(C-c)
if
statement is invisible. All
we have are the pairs of URLs and functions.
Let's look at the second example, example2.py
:
[an error occurred while processing this directive]
I'll demo in my C9 workspace.
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.
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]