未验证 提交 3ba922fb 编写于 作者: M Martin Breuss 提交者: GitHub

Merge pull request #170 from realpython/python-web-applications

Python web applications
runtime: python38
\ No newline at end of file
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)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册