Example 2 - Flask Math

Follow the same steps as the first example, but instead use this code:

from flask import Flask

app = Flask(__name__)

def hello_world():
    return "<p>Flask Math</p>"

def display_number(number):
    """ Displays a number back to the user as-is """
    return str(number)

def add(x, y):
    """ Adds x and y, returns result """
    return str(x + y)

def subtract(x, y):
    """ Subtracts y from x, returns result """
    return str(x - y)

Here, we're using routing within flask, and variable rules to take in input from the user, ensure we get it as a number, do some math, and return it back as a string.

Uses of this app include:

http://localhost:5000/10 , displays 10 in the browser

http://localhost:5000/10/add/20, returns 30

http://localhost:5000/10/subtract/20, returns -10

Try and take this code, and add some more math functions, such as multiplication and division, or go nuts and make it solve arbitrary equations, its up to you!