数据可视化
flask框架补充
#路由解析,通过用户访问的路径,匹配相应的函数
# @app.route('/')
# def hello_world(): # put application's code here
# return 'Hello World!'
#通过访问路径,获取用户的字符串参数或整形参数,此外还有float类型
# @app.route('/user/<name>')
# def hello(name):
# return 'Hello %s'%name
#
# @app.route('/user/<int:id>')
# def hello1(id):
# return 'Hello the %d '%id
#路由路径不能重复,用户通过唯一的路径访问特定的函数
#返回给用户渲染后的网页文件
# @app.route('/')
# def index():
# return render_template("index.html")
#向页面传递一个变量
# @app.route('/')
# def index2():
# time=datetime.date.today() #普通变量
# name=["校长","班主任","数学老师"]
# task={"任务":"打扫卫生","时间":"仨小时"}
# return render_template("index.html",var=time,list=name,task=task)
对应的HTML语言——index.html
**<body>
今天是{{ var }} <br/>
今天值班的有: <br/>
{% for data in list %} <!--用大括号和百分号括起来的是控制结构,还有if结构-->
<li>{{ data }}</li>
{% endfor %}<br/>
任务:<br/>
<table border="1">
{% for key,value in task.items() %}<!--[(key,value),(key,value),(key,value)]-->
<tr>
<td>{{ key }}</td>
<td>{{ value }}</td>
</tr>
{% endfor %}<br/>
</table>
</body>**
#表单提交
# @app.route('/test/register')
# def register():
# return render_template("test/register.html")
#接受表单提交的路由,需要指定method为post
# @app.route('/result',methods=['POST','GET'])
# def result():
# if request.method=='POST':
# result=request.form
# return render_template("test/result.html",result=result)
对应的HTML语言——register.html与result.html
<body>
<form action="{{ url_for('result')}}" method="post"> #这里的action传入地址更加安全,避免页面源码泄露目录路径
<p>姓名:<input type="text" name="姓名"></p>
<p>年龄:<input type="text" name="年龄"></p>
<p>性别:<input type="text" name="性别"></p>
<p>地址:<input type="text" name="地址"></p>
<p>姓名:<input type="submit" value="提交"></p>
</form>
<body>
<table border="1">
{% for key,value in result.items() %}<!--[(key,value),(key,value),(key,value)]-->
<tr>
<th>{{ key }}</th>
<td>{{ value }}</td>
</tr>
{% endfor %}<br/>
</table>
</body>