Skip to content

Commit 9a87b6e

Browse files
committed
Add tutorial code
1 parent b731ea7 commit 9a87b6e

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 convert_temp(cel_temp):
8+
"""Converts Celsius temperature to Fahrenheit temperature."""
9+
try:
10+
far_temp = float(cel_temp) * 9 / 5 + 32
11+
far_temp = round(far_temp, 3) # round to three decimal places
12+
return str(far_temp)
13+
except ValueError: # user entered non-numeric temperature
14+
return "invalid input"
15+
16+
17+
@app.route("/")
18+
def index():
19+
cel_temp = request.args.get("cel_temp")
20+
if cel_temp:
21+
far_temp = convert_temp(cel_temp)
22+
else:
23+
far_temp = ""
24+
return (
25+
"""<form action="">
26+
Celsius: <input type="text" name="cel_temp">
27+
<input type="submit" value="Convert">
28+
</form>"""
29+
+ "Fahrenheit: "
30+
+ far_temp
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

0 commit comments

Comments
 (0)