Server-Side Scripting/Requests/Python (Flask)

From Wikiversity
Jump to navigation Jump to search

app.py[edit | edit source]

# Converts a Fahrenheit temperature to Celsius using a GET request and
# converts a Celsius temperature to Fahrenheit using a POST request.
#
# References:
#   https://www.mathsisfun.com/temperature-conversion.html
#   https://en.wikibooks.org/wiki/Python_Programming
#   https://www.digitalocean.com/community/tutorials/processing-incoming-request-data-in-flask

import flask

app = flask.Flask(__name__)

GET_FORM = """
<form method="GET">
<label for="fahrenheit">Enter Fahrenheit temperature:</label>
<input type="text" id="fahrenheit" name="fahrenheit">
<input type="submit" value="Submit">
</form>
"""

EMPTY_PARAGRAPH = """
<p>&nbsp;</p>
"""

POST_FORM = """
<form method="POST">
<label for="celsius">Enter Celsius temperature:</label>
<input type="text" id="celsius" name="celsius">
<input type="submit" value="Submit">
</form>
"""

@app.route('/', methods=["GET"])
def root_get():
    result = GET_FORM
    
    if "fahrenheit" in flask.request.args:
        fahrenheit = float(flask.request.args["fahrenheit"])
        celsius = (fahrenheit - 32) * 5 / 9
        result += "<p>" + str(fahrenheit) + "° Fahrenheit is " + \
            str(celsius) + "° Celsius</p>"
    else:
        result += EMPTY_PARAGRAPH

    result += POST_FORM + EMPTY_PARAGRAPH

    return result

@app.route('/', methods=["POST"])
def root_post():
    result = GET_FORM + EMPTY_PARAGRAPH + POST_FORM
    
    if "celsius" in flask.request.form:
        celsius = float(flask.request.form["celsius"])
        fahrenheit = celsius * 9 / 5 + 32
        result += "<p>" + str(celsius) + "° Celisus is " + \
            str(fahrenheit) + "° Fahrenheit</p>"
    else:
        result += EMPTY_PARAGRAPH

    return result

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

Try It[edit | edit source]

Copy and paste the code above into the following free online development environment or use your own Python (Flask) compiler / interpreter / IDE.

See Also[edit | edit source]