Welcome to part one of the the Flask 101 Series.
- Flask 101: Create a Basic Python Web App – This tutorial
- Flask 101: Add JSON to your Python Web App
- Flask 101: Use HTML templates & send variables
- Flask 101: Serve Images
- 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__)
@app.route('/')
def myapp():
message = "To use this app: %s/add?x=value&y=value" % request.base_url
return message
@app.route('/add')
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
try:
x = float(request.args['x'])
y = float(request.args['y'])
except:
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 http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 319-407-766
4 – Test your app

