from flask import Flask
from flask import request
from flask import render_template
app = Flask(__name__)
@app.route("/")
@app.route("/hello")
def hello():
return """
<!DOCTYPE html>
<html>
<body>
<h2>输入两个数求和</h2>
<form method='post' action='/add'>
<table>
<p>使用表单计算两个数的和</p>
<tr><td>被加数</td><td><input type='text' name='n1' /></td></tr>
<tr><td>加数</td><td><input type='text' name='n2' /></td></tr>
</table>
<button name="sum" type="submit" value="sum">求和</button>
</form>
</body>
</html>
"""
@app.route("/add", methods=['GET', 'POST'])
def add():
n1 = int(request.form['n1'])
n2 = int(request.form['n2'])
n3 = n1 + n2
return """
<!DOCTYPE html>
<html>
<body>
<h2>输入两个数求和</h2>
<form method='get' action='/hello'>
<table>
<p>使用表单计算两个数的和</p>
<tr><td>被加数</td><td><input type='text' name='n1' value = """+str(n1)+""" /></td></tr>
<tr><td>加数</td><td><input type='text' name='n2' value = """+str(n2)+""" /></td></tr>
<tr><td>和</td><td><input type='text' name='n3' value= """+str(n3)+""" /></td></tr>
</table>
<button name="return" type="submit" value="return">home</button>
</form>
</body>
</html>
"""
if __name__ == "__main__":
app.run(port=5001)
* Running on http://127.0.0.1:5001/ (Press CTRL+C to quit)
127.0.0.1 - - [24/Dec/2020 11:05:37] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [24/Dec/2020 11:05:37] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [24/Dec/2020 11:07:55] "POST /add HTTP/1.1" 200 -
127.0.0.1 - - [24/Dec/2020 11:07:59] "GET /hello?n1=20&n2=63&n3=83&return=return HTTP/1.1" 200 -