CS 257: Software Design

Lab: getting started with Flask

Flask: a web framework

Once you have designed your HTTP-based API, you'll want to write code to implement it, and deploy that code on a publicly accessible server.

One of the biggest hassles of API implementation is the parsing of the URL. For example, if you're implementing queries like "http://wherever.com/author/27", you'll need code to recognize that the caller is requesting the "authors" resource with the author id 27. Once this information is extracted, you'll probably want to call some sort of get_author function with the parameter author_id=27, after which get_author can then do the job of querying the database and assembling the JSON response to be sent back to the browser/client.

In addition to URL parsing, there are other routine and tedious tasks involved in implementing web services. As a result, many people have devloped "web frameworks"—tools to simplify some of these routine tasks. We will be using one such framework called Flask, one of the most popular Python web frameworks. You can run Flask directly on perlman.mathcs.carleton.edu, or you can install it on your own machine. Using Flask will enable you to focus your attention on the core algorithmic content of your API instead of on repetitive details like URL parsing.

Using Flask on perlman, Part 1: Moving files

There are many ways to move files from computer to computer over the internet. Here are a few ways to get your files to perlman.

Using Flask on perlman, Part 2: executing Flask apps

So where are we?

At this point, you can launch a simple Flask web app on perlman and access it via a browser.

Next, you'll want to start implementing your API. To do that, you'll use the functions in flask_sample.py to give you an idea of how to build additional "@app.route" endpoints corresponding to the API you designed in phase 3. You might also find the official Flask documentation helpful.

Here's one approach:

Alternatively, you could create all the endpoint stubs at once, test that they are getting called at the appropriate times, and then start implementing them one at a time.

[Optional] Using Flask on your own computer

Having Flask working on your own machine can help make development of your API more convenient than using the CS department server.