• python之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/")
    def name(name):
        return "你好 %s" %name
    
    # 通过访问路径,获取用户的字符串参数          此外还有float类型
    @app.route("/user/")
    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 在templates下新建html文件

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
        今天是{{ var}},欢迎你!<br/>
        今天值班的有:<br/>
        {% for data in list %}      
            <li>{{ data }}li>
        {% endfor %}
    
        任务:<br/>            
            <table border="1">
                {% for key,value in task.items() %}      #
                <tr>
                    <td>{{ key }}td>
                    <td>{{ value }}td>
                tr>
                {% endfor %}
            table>
    
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    表单提交

    • 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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    • register.html
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • result.html
    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
        <table border="1">
                {% for key,value in result.items() %}      #
                <tr>
                    <th>{{ key }}th>
                    <th>{{ value }}th>
                tr>
                {% endfor %}
            table>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    Redis集群
    快速排序(Quick Sort)
    【社保—五险一金科普】
    使用 Docker Compose 快速搭建监控网站 uptime-kuma
    首次做CMMI,如何选择适合的评估级别
    读书笔记:《你拿什么定义自己》
    常识——手机改直供电+usb调试
    【车载开发系列】GIT教程---如何使用GUI来提交变更
    【C++ 程序】函数积分(使用 std::function)
    91. 解码方法
  • 原文地址:https://blog.csdn.net/qq_51670115/article/details/126532756