【Python Web】Flask体验

数据可视化

Flask入门

  • Flask作为web框架,它的作用主要是为了开发web应用程序。
  • 开启debug

  • 基本操作
from flask import Flask,render_template
import datetime


app = Flask(__name__)


# @app.route('/')
# def hello_world():  # put application's code here
#     return 'Hello!'

@app.route("/index")
def index():
    return "hello"

# 通过访问路径,获取用户的字符串参数
@app.route("/user/<name>")
def name(name):
    return "你好 %s" %name

# 通过访问路径,获取用户的字符串参数          此外还有float类型
@app.route("/user/<int:id>")
def id(id):
    return "你好 %d 号的会员" %id

# 路由路径不能重复,用户只能通过唯一路径来访问特点的函数

# 返回给用户网页文件
# @app.route("/")
# def index2():
#     return render_template("index.html")

# 向页面传递变量
@app.route("/")
def index2():
    time = datetime.date.today()        # 普通变量
    name = ["张","王","李","赵"]          # 列表类型
    task = {"任务":"睡觉","时间":"24hours"}    # 字典类型
    return render_template("index.html", var = time, list = name, task = task)


if __name__ == '__main__':
    app.run()
  • 在templates下新建html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    今天是{{ var}},欢迎你!<br/>
    今天值班的有:<br/>
    {% for data in list %}      <!--用大括号和百分号扩起来是控制结构-->
        <li>{{ data }}</li>
    {% endfor %}

    任务:<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 %}
        </table>

</body>
</html>

表单提交

  • app.py
from flask import Flask,render_template,request
import datetime


app = Flask(__name__)

# 表单提交,需要指定methods为post
@app.route("/test/register")
def register():
    return render_template("test/register.html")

@app.route("/result",methods=['POST','GET'])
def result():
    if request.method == 'POST':
        result = request.form

        return render_template("test/result.html",result=result)

if __name__ == '__main__':
    app.run()

  • register.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form action="{{ url_for('result') }}" method="post">
    <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>
</html>
  • result.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table border="1">
            {% for key,value in result.items() %}      #<!--[(key,value),(key,value),(key,value)]-->
            <tr>
                <th>{{ key }}</th>
                <th>{{ value }}</th>
            </tr>
            {% endfor %}
        </table>
</body>
</html>
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LibraFree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值