forked from robert-portelli/Celsius-to-Fahrenheit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
35 lines (28 loc) · 896 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from flask import Flask
from flask import request
app = Flask(__name__)
def fahrenheit_from(celsius):
"""Convert Celsius to Fahrenheit degrees."""
try:
fahrenheit = float(celsius) * 9 / 5 + 32
fahrenheit = round(fahrenheit, 3) # Round to three decimal places
return str(fahrenheit)
except ValueError:
return "invalid input"
@app.route("/")
def index():
celsius = request.args.get("celsius", "")
if celsius:
fahrenheit = fahrenheit_from(celsius)
else:
fahrenheit = ""
return (
"""<form action="" method="get">
Celsius temperature: <input type="text" name="celsius">
<input type="submit" value="Convert to Fahrenheit">
</form>"""
+ "Fahrenheit: "
+ fahrenheit
)
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8080, debug=True)