flask框架(二)配套代码

简介: flask框架(二)配套代码

1、cookie.py

1"""
 2-  解释: 用来保持服务器和浏览器交互的状态的, 由服务器设置,存储在浏览器
 3
 4- 作用: 用来做广告推送
 5- cookie的设置和获取
 6  - 设置cookie: response.set_cookie(key,value,max_age)
 7    - max_age: 表示cookie在浏览器的存储时间,单位是秒
 8  - 获取cookie: request.cookies.get("key")
 9
10"""
11from flask import Flask, make_response, request
12
13app = Flask(__name__)
14
15#设置cookie
16@app.route('/set_cookie')
17def set_cookie():
18
19    #调用make_response方法获取响应体对象
20    response = make_response("set cookie")
21
22    #设置cookie
23    response.set_cookie("computer","lenovo")
24    response.set_cookie("age","13",10)
25
26    return response
27
28
29#获取cookie
30@app.route('/get_cookie')
31def get_cookie():
32
33    #获取cookie
34    name = request.cookies.get("computer")
35    age = request.cookies.get("age")
36
37    #返回
38    return "name is %s, age is %s"%(name,age)
39
40if __name__ == '__main__':
41    app.run(debug=True)


2、session.py

1"""
 22_session[理解]
 3
 4- 解释: 服务器和用户来做状态保持的,里面存储的是敏感信息(比如身份证,登陆信息),由服务器设置,并存储在服务器
 5- 作用: 用来做用户的登陆状态保持
 6- session的设置和获取
 7  - 设置session: sessioin[key] = value
 8  - 获取session: value =  session.get(key)
 9
10"""
11from flask import Flask, session
12
13app = Flask(__name__)
14
15#设置SECRET_KEY
16app.config["SECRET_KEY"] = "fjdkfhkdfjkdjf"
17
18#设置session
19@app.route('/set_session/<path:name>')
20def set_session(name):
21
22    session["name"] = name
23
24    return "set session!"
25
26
27#获取session
28@app.route('/get_session')
29def get_session():
30
31    value = session.get("name")
32
33    return "set session, name is %s"%value
34
35
36if __name__ == '__main__':
37    app.run(debug=True)


3、context.py

1"""
 23_上下文[了解]
 3
 4- 解释: 就是一个容器
 5- 请求上下文
 6  - request: 封装的是请求相关的数据
 7  - session: 封装的是和用户相关的敏感信息
 8- 应用上下文(在项目中具体应用)
 9  - current_app: 是app的一个代理对象,可以通过他获取app身上设置的各种属性,主要用在模块化开发中
10  - g: 一个局部的全局变量,主要用在装饰器中
11
12"""
13from flask import Flask, current_app
14
15app = Flask(__name__)
16
17@app.route('/')
18def hello_world():
19
20    print(app.config.get("DEBUG"))
21    print(current_app.config.get("DEBUG"))
22
23    return "helloworld"
24
25if __name__ == '__main__':
26    app.run(debug=True)


4、flask_script.py

1"""
 24_Flask_script[掌握]
 3
 4- 解释: 属于flaks的扩展
 5- 作用: 用来动态运行程序,配合flask_migrate做数据库迁移
 6- 使用格式:
 7  - 1.安装
 8    - pip install flask_script
 9  - 2.导入Manager类
10  - 3.创建对象manager,管理app
11  - 4.使用manager启动程序
12    - 启动命令: python xxx.py runserver -h(host是IP地址) -p(端口号) -d(调试模式)
13
14"""
15from flask import Flask
16from flask_script import Manager
17
18app = Flask(__name__)
19app.config["DEBUG"] = True
20
21#3.创建对象manager,管理app
22manager = Manager(app)
23
24@app.route('/')
25def hello_world():
26
27    return "helloworld"
28
29if __name__ == '__main__':
30    manager.run()


5、render_template.py

1"""
 25_render_template[掌握]
 3
 4- 解释: 属于jinja2的模板函数
 5- 好处:
 6  - 1.以后的视图函数,只负责业务逻辑的处理,比如: 数据库的增删改查
 7  - 2.以后数据的展示,全部都有jinja2的模板负责
 8- 使用格式:
 9  - response = render_template('模板文件')
10
11"""
12from flask import Flask,render_template
13
14app = Flask(__name__)
15
16@app.route('/')
17def hello_world():
18
19    response = render_template('file01template.html')
20
21    return response
22
23if __name__ == '__main__':
24    app.run(debug=True)


template.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6    <style>
 7        .box{
 8            width: 300px;
 9            height: 300px;
10            background: red;
11        }
12
13    </style>
14</head>
15<body>
16
17    <div class="box">
18
19    </div>
20
21</body>
22</html>


6、variable.py

1"""
 26_模板语法,获取变量[理解]
 3
 4- 解释: 在模板中获取视图函数的变量
 5- 格式:
 6  - {{ 变量 }}
 7
 8注意点:
 91.如果发现程序被占用端口
102.杀死端口, lsof -i:5000  然后kill 进程
11
12"""
13from flask import Flask,render_template
14
15app = Flask(__name__)
16
17@app.route('/')
18def hello_world():
19
20    #1.定义各种类型的变量
21    number = 10
22    str = "老王"
23    tuple = (1,2,3,4,5)
24    list = [6,7,8,9,10]
25    dict = {
26        "name":"班长",
27        "age":29
28    }
29
30    #2.携带变量到模板中展示
31    return render_template("file02variable.html",number=number,str=str,tuple=tuple,list=list,dict=dict)
32
33if __name__ == '__main__':
34    app.run(debug=True)


variable.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6</head>
 7<body>
 8
 9    <h1>1.获取各种变量的值</h1>
10    <h2>整数: {{number}}</h2>
11    <h2>字符串: {{str}}</h2>
12    <h2>元祖: {{tuple}},分开获取: {{tuple[0]}}, {{ tuple.1 }}</h2>
13    <h2>列表: {{list}},分开获取: {{ list.0 }}, {{ list.1 }}</h2>
14    {# 如果字典使用方括号,获取,需要写成字符串,如果不是字符串,那么则会被当成变量对待   #}
15    <h2>字典: {{dict}}, 分开获取: {{ dict.name }}, {{ dict["age"] }}</h2>
16
17</body>
18</html>


7、otherprogrammer.py

1from flask import Flask,render_template
 2
 3app = Flask(__name__)
 4
 5@app.route('/')
 6def hello_world():
 7
 8    #1.定义各种类型的变量
 9    number = 10
10    str = "老王"
11    tuple = (1,2,3,4,5)
12    list = [6,7,8,9,10]
13    dict = {
14        "name":"班长",
15        "age":29
16    }
17
18    #2.携带变量到模板中展示
19    return render_template("file03other_programer.html",number=number,str=str,tuple=tuple,list=list,dict=dict)
20
21if __name__ == '__main__':
22    app.run(debug=True)


other_programer.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6    <style>
 7        h1{
 8            color:red;
 9        }
10
11    </style>
12</head>
13<body>
14
15    <h1>1.遍历元祖中的偶数</h1>
16    {% for item in tuple %}
17        {% if item %2 == 0 %}
18            <h3>{{ item }}</h3>
19        {% endif %}
20    {% endfor %}
21
22
23    <h1>2.遍历字典</h1>
24    {% for key in dict %}
25        {# dict.key那么这个key会当成字典中的一个键,  dict[key],那么这个key当成一个变量 #}
26        <h3>{{ key }} = {{ dict[key] }}</h3>
27    {% endfor %}
28
29
30</body>
31</html>


8、srting_filter.py

1from flask import Flask,render_template
 2
 3app = Flask(__name__)
 4
 5@app.route('/')
 6def hello_world():
 7
 8    return render_template("file04stringfilter.html")
 9
10if __name__ == '__main__':
11    app.run(debug=True)


stringfilter.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6</head>
 7<body>
 8
 9    {#    使用格式:{{ 字符串 | 字符串过滤器 }}#}
10    1.safe:禁用转义,让标签生效
11    <p>{{ '<em>hello</em>' | safe }}</p>
12
13    2.capitalize:把变量值的首字母转成大写,其余字母转小写
14    <p>{{ 'hello PYTHON' | capitalize }}</p>
15
16    3.lower:把值转成小写
17    <p>{{ 'HELLO PYthON' | lower }}</p>
18
19    4.upper:把值转成大写,中文没有大小写
20    <p>{{ 'hello python 你好' | upper }}</p>
21
22    5.title:把值中的每个单词的首字母都转成大写
23    <p>{{ 'hello world python java' | title }}</p>
24
25    6.reverse:字符串反转
26    <p>{{ 'olleh' | reverse }}</p>
27    <p>{{ '我爱你' | reverse }}</p>
28
29
30    7.format:格式化输出
31    <p>{{ '%s is %d' | format('age',17) }}</p>
32
33    8.striptags:渲染之前把值中所有的HTML标签都删掉
34    <p>{{ '<em>hello</em>' | striptags }}</p>
35
36</body>
37</html>


9、list_filter.py

1from flask import Flask,render_template
 2
 3app = Flask(__name__)
 4
 5@app.route('/')
 6def hello_world():
 7
 8    return render_template("file05list_fliter.html")
 9
10if __name__ == '__main__':
11    app.run(debug=True)


list_filter.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6</head>
 7<body>
 8    {#    * 使用格式:{{ 列表 | 列表过滤器 }}#}
 9
10    1.first:取第一个元素
11    <p>{{ [1,2,3,4,5,6] | first }}</p>
12
13    2. last:取最后一个元素
14    <p>{{ [1,2,3,4,5,6] | last }}</p>
15
16    3. length:获取列表长度
17    <p>{{ [1,2,3,4,5,6] | length }}</p>
18
19    4.sum:列表求和
20    <p>{{ [1,2,3] | sum }}</p>
21
22    5.sort:列表排序,默认升序
23    <p>{{ [6,2,3,1,5,4] | sort }}</p>
24
25
26    6.过滤器的链式调用
27    {# 过滤器的链式调用 #}
28    {{ "hello" | upper | reverse }}
29
30</body>
31</html>


10、custom_filter.py

1"""
 210_自定义过滤器[掌握]
 3
 4- 解释: 当系统提供的过滤器满足不了需求的时候,需要自定义
 5- 自定义过滤器有两种格式:
 6  - 1.先定义好函数,再将函数添加到系统默认的过滤器列表中
 7    - def 函数名: pass
 8    - app.add_template_filter(函数名,'过滤器名字')
 9  - 2.定义函数的时候,直接使用系统过滤器进行装饰
10        @app.template_filter('过滤器名字')
11        def 函数名():
12            pass
13  - 案例:
14  - 1.获取列表偶数和
15  - 2.反转列表
16
17"""
18from flask import Flask,render_template
19
20app = Flask(__name__)
21
22# 1.先定义好函数,再将函数添加到系统默认的过滤器列表中
23def get_oushu(list):
24    print(list)
25    sum = 0
26    for i in list:
27        if i %2 == 0:
28            sum += i
29
30    return sum
31#参数1: 关联的函数名称,  参数2: 在模板中使用的过滤器名字
32app.add_template_filter(get_oushu,"oushu")
33
34
35# 2.定义函数的时候,直接使用系统过滤器进行装饰
36@app.template_filter("reverse")
37def listreverse(list):
38    list.reverse()
39    return list
40
41
42
43@app.route('/')
44def hello_world():
45
46    return render_template("file06custom_filter.html")
47
48if __name__ == '__main__':
49    app.run(debug=True)


custom_filter.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6</head>
 7<body>
 8
 9    <h2>原列表: {{ [1,2,3,4,5,6] }}</h2>
10    <h2>偶数列表: {{ [1,2,3,4,5,6] | oushu }}</h2>
11    <h2>反转列表: {{ [1,2,3,4,5,6] | reverse }}</h2>
12    <h2>降序列表: {{ [1,2,3,4,5,6,10,9,7] | sort | reverse }}</h2>
13
14</body>
15</html>


11、macro.py

1"""
 211_代码复用之宏[了解]
 3
 4- 解释: 相当于python中的函数,定义好一段功能,在需要的时候进行调用即可
 5
 6"""
 7from flask import Flask,render_template
 8
 9app = Flask(__name__)
10
11@app.route('/')
12def hello_world():
13
14    return render_template("file07macro.html")
15
16if __name__ == '__main__':
17    app.run(debug=True)

macro.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6</head>
 7<body>
 8
 9    {# 定义宏 #}
10    {% macro my_macro(name,password) %}
11        用户名: <input type="text" value="{{ name }}"><br>
12        密码: <input type="password" value="{{ password }}"><br>
13    {% endmacro %}
14
15    {# 调用当前文件宏 #}
16    {{ my_macro("zhangsan","111111") }}
17
18
19    {# 使用其他文件的宏 #}
20    {% import 'file08othermacro.html' as other %}
21    {{ other.my_input() }}
22    {{ other.my_div() }}
23
24
25</body>
26</html>


othermacro.html

1{% macro my_macro(name,password) %}
 2    用户名: <input type="text" value="{{ name }}"><br>
 3    密码: <input type="password" value="{{ password }}"><br>
 4{% endmacro %}
 5
 6
 7{% macro my_input() %}
 8
 9    <h1>这是一个其他文件的宏</h1>
10
11{% endmacro %}
12
13
14{% macro my_div() %}
15
16    <div style="color: red;">我是一个孤独的div</div>
17
18{% endmacro %}


12、extends.py

1"""
 212_代码复用之继承[掌握]
 3
 4- 解释: 一个子模板继承自父模板
 5- 作用: 共性抽取,代码复用
 6- 父模板
 7  - 1.所有子类都具有的相同的内容的, 在父模板中直接写死
 8  - 2.每个子类的模板中不一样的内容,使用block模板定义好
 9- 子模板
10  - 1.根据子类自己的需求,去重写父类中的block对应的内容
11  - 2.如果重写之后,还想保留父类的内容,那么使用{{super()}}
12  - 3.继承格式: {% extends '父文件名'%}, 写在页面的顶部
13
14"""
15from flask import Flask,render_template
16
17app = Flask(__name__)
18
19@app.route('/')
20def hello_world():
21
22    # return render_template("file09zi.html")
23    return render_template("file10zi.html")
24
25if __name__ == '__main__':
26    app.run(debug=True)

file09zi.html

1{% extends 'file11fu.html' %}
 2
 3{# 重写正文部分 #}
 4{% block contentBlock %}
 5    <p>
 6        床前一锅汤,<br>
 7        撒了一裤裆, <br>
 8        抬头拿抹布, <br>
 9        低头擦裤裆 <br>
10    </p>
11{% endblock %}
12
13
14{#<!DOCTYPE html>#}
15{#<html lang="en">#}
16{#<head>#}
17{#    <meta charset="UTF-8">#}
18{#    <title>Title</title>#}
19{#</head>#}
20{#<body>#}
21{##}
22{#    <h1>静夜思</h1>#}
23{##}
24{#    <p>#}
25{#        床前一锅汤,<br>#}
26{#        撒了一裤裆, <br>#}
27{#        抬头拿抹布, <br>#}
28{#        低头擦裤裆 <br>#}
29{#    </p>#}
30{##}
31{#    <div>#}
32{#        <a href="#">点我有惊喜</a>#}
33{#    </div>#}
34{##}
35{##}
36{#</body>#}
37{#</html>#}

file10zi.html

1{% extends 'file11fu.html' %}
 2
 3{# 重写父类titleBlock内容 #}
 4{% block titleBlock %}
 5    {{ super() }}
 6    <h1>新静夜思</h1>
 7{% endblock %}
 8
 9{# 重写父类中的contentBlock内容 #}
10{% block contentBlock %}
11    <p>
12        床前明月光,<br>
13        疑似地上霜, <br>
14        举头望明月, <br>
15        低头思故乡 <br>
16    </p>
17{% endblock %}
18
19{#<!DOCTYPE html>#}
20{#<html lang="en">#}
21{#<head>#}
22{#    <meta charset="UTF-8">#}
23{#    <title>Title</title>#}
24{#</head>#}
25{#<body>#}
26{##}
27{#    <h1>静夜思</h1>#}
28{##}
29{#    <p>#}
30{#        床前明月光,<br>#}
31{#        疑似地上霜, <br>#}
32{#        举头望明月, <br>#}
33{#        低头思故乡 <br>#}
34{#    </p>#}
35{##}
36{#    <div>#}
37{#        <a href="#">点我有惊喜</a>#}
38{#    </div>#}
39{##}
40{##}
41{#</body>#}
42{#</html>#}

file11fu.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6</head>
 7<body>
 8
 9    {# 头部部分 #}
10    {% block titleBlock %}
11        <h1>静夜思</h1>
12    {% endblock %}
13
14    {# 正文部分 #}
15    {% block contentBlock %}
16
17    {% endblock %}
18
19
20    {# 底部部分 #}
21    <div>
22        <a href="#">点我有惊喜</a>
23    </div>
24
25</body>
26</html>

13.practice.py

1"""
 2给定如下5条数据,只显示4行数据,背景颜色依次为:黄,绿,红,紫
 3my_list = [
 4    {
 5        "id": 1,
 6        "value": "我爱工作"
 7    },
 8    {
 9        "id": 2,
10        "value": "工作使人快乐"
11    },
12    {
13        "id": 3,
14        "value": "沉迷于工作无法自拔"
15    },
16    {
17        "id": 4,
18        "value": "日渐消瘦"
19    },
20    {
21        "id": 5,
22        "value": "以梦为马,越骑越傻"
23    }
24]
25"""
26from flask import Flask, render_template
27
28app = Flask(__name__)
29
30@app.route('/')
31def hello_world():
32
33    #1.定义5条数据
34    my_list = [
35        {
36            "id": 1,
37            "value": "我爱工作"
38        },
39        {
40            "id": 2,
41            "value": "工作使人快乐"
42        },
43        {
44            "id": 3,
45            "value": "沉迷于工作无法自拔"
46        },
47        {
48            "id": 4,
49            "value": "日渐消瘦"
50        },
51        {
52            "id": 5,
53            "value": "以梦为马,越骑越傻"
54        }
55    ]
56
57    #2.在模板中显示4条
58    return render_template("file13practice.html",list=my_list)
59
60if __name__ == '__main__':
61    app.run(debug=True)

file13practice.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6</head>
 7<body>
 8
 9    <ul>
10        {# 如果dict.id 不等于5才遍历 #}
11        {% for dict in list if dict.id !=5 %}
12
13            {# 方式一 #}
14{#            {% if dict.id == 1 %}#}
15{#                <li style="background: yellow;"> {{ dict.value }} </li>#}
16{#            {% elif dict.id == 2 %}#}
17{#                <li style="background: green;"> {{ dict.value }} </li>#}
18{#            {% elif dict.id == 3 %}#}
19{#                <li style="background: red;"> {{ dict.value }} </li>#}
20{#            {% else %}#}
21{#                <li style="background: purple;"> {{ dict.value }} </li>#}
22{#            {% endif %}#}
23
24            {# 遍历的时候可以获取到从0开始的索引 #}
25{#            <h3>{{ loop.index0  }}</h3>#}
26
27            {# 遍历的时候可以获取到从1开始的索引 #}
28{#            <h3>{{ loop.index  }}</h3>#}
29
30
31            {# 方式二 #}
32            {% if loop.index == 1 %}
33                <li style="background: yellow;"> {{ dict.value }} </li>
34            {% elif loop.index == 2 %}
35                <li style="background: green;"> {{ dict.value }} </li>
36            {% elif loop.index == 3 %}
37                <li style="background: red;"> {{ dict.value }} </li>
38            {% else %}
39                <li style="background: purple;"> {{ dict.value }} </li>
40            {% endif %}
41
42
43
44        {% endfor %}
45    </ul>
46
47
48</body>
49</html>

14.special_variable.py

1from flask import Flask,render_template
 2
 3app = Flask(__name__)
 4app.config["SECRET_KEY"] ="hahaha"
 5@app.route('/')
 6def hello_world():
 7
 8    return render_template("file14special_variable.html")
 9
10@app.route('/test/<int:age>')
11def test(age):
12    return "test..."
13
14
15if __name__ == '__main__':
16    app.run(debug=True)

file14special_variable.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6</head>
 7<body>
 8
 9    <h2>config变量: {{ config }}</h2>
10    <h2>request: {{ request.method }}</h2>
11    <h2>request: {{ request.url }}</h2>
12    <h2>url_for(): {{ url_for("hello_world") }}</h2>
13    <h2>url_for(): {{ url_for("test",age=100) }}</h2>
14
15</body>
16</html>

15.flash.py

1from flask import Flask,render_template,flash
 2
 3app = Flask(__name__)
 4app.config["SECRET_KEY"] = "fdjkfjdk"
 5
 6@app.route('/')
 7def hello_world():
 8
 9
10
11    return render_template("file15flash.html")
12
13@app.route('/test')
14def test():
15    #存储消息
16    flash("登陆成功")
17
18    return "test"
19
20if __name__ == '__main__':
21    app.run(debug=True)

file15flash.html

1<!DOCTYPE html>
 2<html lang="en">
 3<head>
 4    <meta charset="UTF-8">
 5    <title>Title</title>
 6</head>
 7<body>
 8
 9
10    {{ get_flashed_messages() }}
11
12</body>
13</html>
相关文章
|
12天前
|
JSON 测试技术 数据库
Python的Flask框架
Python的Flask框架
|
12天前
|
存储 安全 数据库
Flask框架中,如何实现用户身份验证和会话管理
Flask框架中,如何实现用户身份验证和会话管理
|
16天前
|
数据库 Python
如何开始使用Flask框架
【9月更文挑战第1天】如何开始使用Flask框架
18 3
|
17天前
|
数据库连接 开发者 Python
从 Flask 迁移到 FastAPI,这简直是一场奇妙之旅!性能飙升,代码更优,快来感受这份惊喜!
【8月更文挑战第31天】作为一名开发者,从 Flask 迁移至 FastAPI 是一次充满挑战与收获的旅程。起初,Flask 的简洁性和灵活性吸引了我,但随着项目扩展,其性能局限性和较弱的类型提示支持逐渐显现。FastAPI 结合了 Flask 的简洁性和 Django 的强大功能,具备高性能、类型提示支持及自动文档生成等优势,更适合构建现代化 Web 应用。尽管迁移过程中需对现有代码进行调整,以适应 FastAPI 的类型提示和依赖注入机制,但最终应用性能和代码可维护性均显著提升,整个过程也大大提升了我的开发技能。
25 0
|
17天前
|
前端开发 开发者 Python
Flask框架之谜:如何用Blueprints神技轻松制胜模块化应用?
【8月更文挑战第31天】随着Flask应用规模扩大,代码管理和模块化变得至关重要。Blueprints作为Flask提供的强大工具,支持将应用分割成包含独立路由、视图、模板和静态文件的模块,从而提高代码清晰度与重用性。本文通过创建前端和后台管理两个蓝图的示例,展示了如何在Flask中使用Blueprints实现应用模块化,简化大型应用开发与维护工作,提升代码结构的清晰度及团队协作效率。
10 0
|
19天前
|
运维 Serverless 对象存储
函数计算产品使用问题之如何使用Flask框架支持Stream模式
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
|
24天前
|
Python Windows 内存技术
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?
|
2月前
|
监控 前端开发 API
实战指南:使用Python Flask与WebSocket实现高效的前后端分离实时系统
【7月更文挑战第18天】构建实时Web应用,如聊天室,可借助Python的Flask和WebSocket。安装Flask及Flask-SocketIO库,创建Flask应用,处理WebSocket事件。前端模板通过Socket.IO库连接服务器,发送和接收消息。运行应用,实现实时通信。此示例展现了Flask结合WebSocket实现前后端实时交互的能力。
281 3
|
19天前
|
前端开发 计算机视觉 Python
flask+python 实时视频流输出到前台
该文章介绍了如何使用Flask和Python实现实时视频流输出到网页前端,包括后端通过OpenCV捕获摄像头视频流、处理并编码为JPEG格式,然后以HTTP响应的形式发送给前端显示的完整示例代码和运行效果。
flask+python 实时视频流输出到前台
|
4天前
|
Python
使用Python和Flask构建简单的Web应用
使用Python和Flask构建简单的Web应用
18 6