Flask 101: Create a Basic Python Web App

Welcome to part one of the the Flask 101 Series. 


  1. Flask 101: Create a Basic Python Web App – This tutorial
  2. Flask 101: Add JSON to your Python Web App
  3. Flask 101: Use HTML templates & send variables 
  4. Flask 101: Serve Images 
  5. Flask 101: Serve CSV Files


This is probably one of the simplest applications you can generate with Flask: we will create an app that adds two numbers and returns the answer. As usual, I will assume that you run Linux.


1 – Setup

If you want to use a Python 3 virtual environment, you can follow this post for example. But all you really need is to install Flask

# Install flask
pip install flask


2 – Create your basic flask application

We are going to create two routes to test our API

  • /
    Which is going to tell users how to use the api
  • add
    takes two numbers x and y and returns the value x+y

Create a file named myapp.py with the following contents.

from flask import Flask, request

app = Flask(__name__)

def myapp():
    message = "To use this app: %s/add?x=value&y=value" % request.base_url
    return message

def add():
    # Checking that both parameters have been supplied
    if not 'x' in request.args:
        return "x value is missing"
    if not 'y' in request.args:
        return "y value is missing"
    # Make sure they are numbers too
        x = float(request.args['x'])
        y = float(request.args['y'])
        return "x and y should be numbers"
    return str(x+y)


3 – Run your app

In the same directory as myapp.py, create a file named runapp.sh with the following contents.

export FLASK_APP=myapp
export FLASK_ENV=development
flask run

Close the file, then in the command line, run it with

# Execute this in the command line inside the same directory 
bash runapp.sh

You should see the following output

* Serving Flask app "myapp" (lazy loading)
* Environment: development
* Debug mode: on
* Running on (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 319-407-766


4 – Test your app