Flask Tutorial

CS373 Summer 2018: Victor Chau

Introduction to Flask

Flask is a micro web framework for Python, meaning that it does require any particular tools or libraries, but does support extensions. Flask is used for web development, creating APIs, and hosting servers. Flask is very lightweight and easy to use, being able to host a web page or server with only a few lines of code.

We used Flask for hosting our back-end server that acted as our API by connecting to our SQL database. We also temporarily used Flask to support our front-end as well, before later phasing it out to React.

History of Flask

Flask was initially created by Armin Ronacher in 2010, a member of Pocoo, a group of open source developers working on Python. At first, Flask “came out of an April Fool’s joke but proved popular enough to make into a serious application in its own right.”

Installation

To download Flask, first make sure you have downloaded Python. At the time of writing this, the latest version of Python is 3.7.0.

If you’re on Windows, go to https://www.python.org/downloads/ and hit the download button.

If you’re on Linux, enter these commands into your terminal:

sudo apt-get update
sudo apt-get install python 3.7

After you have downloaded Python, you can download Flask using the command

pip install Flask

in your terminal or Windows Powershell.

To run Flask on Docker, you will need to download the software inside of the container. To do so, first grab any Python Docker image. Docker conveniently provides a Python image in one of their official repos. Assuming you already have Docker set up (if not click here Docker’s guide to installation), you can grab their image using

FROM python:latest

or

FROM python:3

in your Dockerfile.

Now, add this line to your Dockerfile:

RUN pip install -r requirements.txt

This will install all of the required technologies from a file called requirements.txt. But wait, we don’t have any file like that! This is where you will want to create a new file named as such in the working directory specified by your Dockerfile. In your requirements.txt file you can put any technologies and frameworks you will need in your container.

Since we are just using Flask, our requirements.txt file only contains one line

Flask

It’s that simple!

For a concrete example, here is the Dockerfile we used to host our backend server using Flask.

#Use an official Python runtime as a parent image
FROM python:3
# Set the working directory to /index
WORKDIR /index
# Copy the current directory contents into the container at /index
COPY . /index
# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt
# Run app.py when the container launches
CMD [“python”, “index.py”]

The first line grabs an image to use in the container. In our case we will want a Python image since Flask is a Python web framework.

The second line sets the working directory, in our case /index.

The third line copies our directory into our Docker container. You can copy as many or as few files and directories as you please. In our case, all of our backend code is container within the index directory, so we copy the entire directory to our container.

The fourth line makes use of our requirements.txt file and downloads any required packages to our container. For the sake of congruence, here is our requirements.txt file used in our backend.

Flask
Redis
coverage
elasticsearch
elasticsearch-dsl
Flask-Cors
Flask-OAuth
flask-restplus
flask-restless
Flask-SQLAlchemy
Flask-Testing
gunicorn
nose
pymysql
flask-mysql

Note that each package is given its own new line. This makes reading the file easier and lets pip install all of the packages.

Congratulations, you have just installed Flask!

Use

Flask can be used to host front-end or back-end applications.

Here’s a simple Flask Application that returns “Hello World!”. Inside our new file app.py:

from flask import Flask
app = Flask(__name__)

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

You will first want to import Flask. Creating our app as Flask(__name__) lets Python know what we will want to run, our app, in the main method.

@app.route() is used to specify URLs. You can enter any app.route you want as a header above a method. By doing so, you tell Flask that when you visit a specify URL, Python will run whatever methods you specify.

In this example, the URL (“/”) specifies the base URL. When you visit the base URL, the page will return a simple “Hello World!”.

We run our Flask app like we would any other Python file,

python app.py

Alternatively, we can specify our app to run using these commands

FLASK_APP = app.py
flask run

This specifies which Python file Flask will run in the command line. Flask also conveniently includes a development server hosted on localhost:5000. Let’s run our Python app!

If you visit localhost:5000, you should see this:

A simple Flask app

There’s a lot of cool things you can do with Flask. For example, you can use Flask to host your front-end by dynamically loading web pages and retrieving data. I won’t spoil all of the fun, so get to playing around with Flask and enjoy making some web applications! For some more help, here’s Flask’s documentation site: http://flask.pocoo.org/.

Note: when using Flask in production, make sure you expose your port by including the line “host=0.0.0.0” in your main method like so:

if __name__ == “__main__”:
app.run(host = “0.0.0.0”)

Use Cases

Now that you have seen Flask in action you’re probably asking, “Why should I use Flask?” Flask is a very lightweight and easy to use web framework. As you saw, it only takes a few lines of code to get a web application up and running. However, Flask is still very powerful.

Flask can be used to dynamically create web pages, read in data from databases, and more while still being very easy and simple to use. Flask supports dynamic URL creation, making routing to different URLs very simple. Not to mention, the development server that comes bundled when you install Flask is very helpful for creating web apps and debugging. Flask also supports a debug mode (used with the line “debug = True”) that prints error messages to the terminal.

Overall, I’ve had a very enjoyable experience creating our web app on Flask and will continue to use the framework when developing in the future.

Alternatives

As you probably know, there exists another popular Python web framework out there: Django. Django is a very powerful web framework that makes it easy for developers to dive right into creating applications. However, to a new web developer Django may seem a bit overwhelming. Flask is a simple alternative that acts as an introduction to web development, while still offering many powerful tools.

There is also another alternative called Pyramid. Like Django, Pyramid is also an open source framework meant for large applications. However in Pyramid, you will need to mix and match components such as the URL structure, template styles, and the database in order to build the applications. Once again, to an experienced web developer, Pyramid may seem to have everything you will ever need. And once again, Flask presents a simple yet still powerful solution. To newbies and experienced professionals alike, Flask offers a lightweight framework to develop web applications. The basic “Hello World!” application we wrote earlier only requires a mere 7 lines of Code!

In conclusion, each framework has their pros and cons. We chose to use Flask because for most of our group, it was our first time developing web applications. Flask provided exactly what we needed: a simple yet powerful web framework that we used to build our front-end and back-end.