Module 1: Web Application Development with Flask

Module Overview

What is a web application? It's a pretty overloaded term, but in general, refers to any sort of useful interactive tool you can load in a web browser. In this module, we'll break down the specific components of a web application, and learn how to develop our own basic web application using the Flask Python framework.

Learning Objectives

Objective 01 - Distinguish between front-end, back-end, database, and what tasks are appropriate for which

Overview

Web applications are built on a relatively thick stack - from the client (user with their browser) over a network to the back-end (server) and database (for data persistence). As a data scientist you won't master any particular piece, but you should understand the whole picture.

A web application is a user-facing piece of software implemented with a client-server architecture. The client (the user's browser) makes requests - specifically, HTTP (HyperText Transfer Protocol) requests over a network (the Internet, or a local network for e.g. internal corporate web applications).

The server receives this request, and then builds and sends a response - generally, a mixture of HTML/CSS and JavaScript, a.k.a. “a web page.” The client receives the response, and the web browser renders it so the user can see whatever it was they asked for.

Follow Along

From the above summary, it's fairly clear what the front-end of a web application is - specifically, it's that HTML/CSS and JavaScript returned in response to user requests. But what's the difference between the back-end and the database?

The back-end is the direct server receiving requests, and handling the routing/business logic to determine and send the appropriate response. The database is generally a separate server where data is actually persisted - saved in a durable fashion, for later retrieval by the web application. In fact, many web applications are essentially highly usable database querying machines - they let a user interact with a front-end, send requests to a back-end which gets or changes data in a database.

Why separate the back-end and the database? A useful model is to have a back-end server application that is stateless (or close to it) - that is, it doesn't persist or save or depend on persisting or saving things. It just has the appropriate logic to respond as desired in a given situation - and as such, it's much easier to scale, spin up or down extra instances, and so forth.

Now that you know a bit about the pieces, consider this example of a simple full-stack web application:

Challenge

Think of your own example of a full-stack web application, and list what specific responsibilities you think fall to which piece of the stack. You may pick an existing application or one you imagine - in either case, share your analysis with a classmate and offer them feedback on theirs.

Additional Resources

Objective 02 - Create a simple Python web application that exposes an API to URL endpoints

Required Resources

Prepare Videos

Overview

Now that you know the basics of what a web application is, it's time to build our own! We'll be focusing on the back-end, building a simple Python web application with Flask (a microframework) to handle and return requests to URL endpoints.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

“Hello World!” with Flask is as simple as that - 5 lines of code. Let's understand each one by one.

Follow Along

Let's break down each of the lines of the above example.

Challenge

Make a new file hello.py with the contents of the above basic “Hello World!” Flask web application. Then, get to a terminal and try to run it:

$ pip install Flask
$ FLASK_APP=hello.py flask run
 * Running on http://localhost:5000/

This assumes you have pip ready to go - which you should! You'll only have to install Flask once (globally or per environment), and to run an application you specify what it is with an environment variable (FLASK_APP=hello.py) and then just flask run - and you can do it all in one line!

Once you get it running and see output as above, try to open the URL it gives in your browser. You should see “Hello World!” in all its Python Flask glory.

Additional Resources

Guided Project

Due to recent changes in the accessibility of the Twitter API you will no longer need to request Developer Access. We will be working with a mock version for the time being. For this Module, you may follow the instructions in the Guided Project video.

In this guided project, we'll build a web application using Flask. Open guided-project.md in the GitHub repository below to follow along with the guided project.

Module Assignment

For this assignment, you'll reproduce the steps from lecture to write and run a basic local Flask web application, and create models for Tweet and User data.

Assignment Solution Video

Additional Resources

Flask Development

Web Development Concepts