Skip to content

Commit 3ba922f

Browse files
Merge pull request #170 from realpython/python-web-applications
Python web applications
2 parents ad2a0bc + a5b36fd commit 3ba922f

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

python-web-applications/app.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runtime: python38

python-web-applications/main.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from flask import Flask
2+
from flask import request
3+
4+
app = Flask(__name__)
5+
6+
7+
def fahrenheit_from(celsius):
8+
"""Convert Celsius to Fahrenheit degrees."""
9+
try:
10+
fahrenheit = float(celsius) * 9 / 5 + 32
11+
fahrenheit = round(fahrenheit, 3) # Round to three decimal places
12+
return str(fahrenheit)
13+
except ValueError:
14+
return "invalid input"
15+
16+
17+
@app.route("/")
18+
def index():
19+
celsius = request.args.get("celsius", "")
20+
if celsius:
21+
fahrenheit = fahrenheit_from(celsius)
22+
else:
23+
fahrenheit = ""
24+
return (
25+
"""<form action="" method="get">
26+
Celsius temperature: <input type="text" name="celsius">
27+
<input type="submit" value="Convert to Fahrenheit">
28+
</form>"""
29+
+ "Fahrenheit: "
30+
+ fahrenheit
31+
)
32+
33+
34+
if __name__ == "__main__":
35+
app.run(host="127.0.0.1", port=8080, debug=True)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask==1.1.2

0 commit comments

Comments
 (0)