flask介绍、快速使用、配置文件、路由系统

前言:

Flask框架和Django框架的区别:

 

  • Django框架:
    • 大而全,内置的app的很多,第三方app也很多
  • Flask框架:
    • 小而精,没有过多的内置app,只能完成web框架的基本功能,很多功能都需要借助第三方

拓展

  • python异步框架:

    • fastapi:FastAPI
    • sanic
    • tornado(目前很少人在使用)
    • django:3.x以后版本支持异步
  • 同步框架和异步框架的区别

    • 同步框架: 一个线程只会处理一个请求
    • 异步框架: 一个线程可以处理多个请求
    • 异步框架可以显著的提高并发量

一、flask介绍

1、介绍

Flask是一个基于Python开发并且依赖于jinja2模板和Werkzeug WSGI服务的一个微型框架

jinja2:

模板语法,和django的dtl非常像

Werkzeug WSGI:

符合wsgi协议的web服务器,django使用的是wsgiref

2、使用两种协议编写web

使用wsgiref编写web

<span style="color:#393a34"><span style="background-color:white"><code class="language-py"><span style="color:#0000ff">from</span> wsgiref<span style="color:#393a34">.</span>simple_server <span style="color:#0000ff">import</span> make_server


<span style="color:#0000ff">def</span> <span style="color:#393a34">mya</span><span style="color:#393a34">(</span>environ<span style="color:#393a34">,</span> start_response<span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># request就是environ包装后的对象</em></span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>environ<span style="color:#393a34">)</span>
    start_response<span style="color:#393a34">(</span><span style="color:#a31515">'200 OK'</span><span style="color:#393a34">,</span> <span style="color:#393a34">[</span><span style="color:#393a34">(</span><span style="color:#a31515">'Content-Type'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text/html'</span><span style="color:#393a34">)</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span>
    <span style="color:#008000"><em># 分发路由</em></span>
    <span style="color:#008000"><em># 根据用户访问的路由,打开对应的html文件,读取并返回给用户</em></span>
    <span style="color:#0000ff">if</span> environ<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'PATH_INFO'</span><span style="color:#393a34">)</span> <span style="color:#393a34">==</span> <span style="color:#a31515">'/index'</span><span style="color:#393a34">:</span>
        <span style="color:#0000ff">with</span> open<span style="color:#393a34">(</span><span style="color:#a31515">'index.html'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'rb'</span><span style="color:#393a34">)</span> <span style="color:#0000ff">as</span> f<span style="color:#393a34">:</span>
            data <span style="color:#393a34">=</span> f<span style="color:#393a34">.</span>read<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
    <span style="color:#0000ff">elif</span> environ<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'PATH_INFO'</span><span style="color:#393a34">)</span> <span style="color:#393a34">==</span> <span style="color:#a31515">'/login'</span><span style="color:#393a34">:</span>
        <span style="color:#0000ff">with</span> open<span style="color:#393a34">(</span><span style="color:#a31515">'login.html'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'rb'</span><span style="color:#393a34">)</span> <span style="color:#0000ff">as</span> f<span style="color:#393a34">:</span>
            data <span style="color:#393a34">=</span> f<span style="color:#393a34">.</span>read<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
    <span style="color:#0000ff">else</span><span style="color:#393a34">:</span>
        data <span style="color:#393a34">=</span> <span style="color:#a31515">b'<h1>Hello Web!</h1>'</span>
    <span style="color:#0000ff">return</span> <span style="color:#393a34">[</span>data<span style="color:#393a34">]</span>


<span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 第一个参数是服务的IP(不写默认为127.0.0.1),第二个是监听的端口,第三个是编写的web函数</em></span>
    my_server <span style="color:#393a34">=</span> make_server<span style="color:#393a34">(</span><span style="color:#a31515">'0.0.0.0'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">8008</span><span style="color:#393a34">,</span> mya<span style="color:#393a34">)</span>
    <span style="color:#008000"><em># 启动服务</em></span>
    my_server<span style="color:#393a34">.</span>serve_forever<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>

werkzeug WSGI编写服务:

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># pip 安装werkzeug</em></span>
<span style="color:#008000"><em># 导入</em></span>
<span style="color:#0000ff">from</span> werkzeug<span style="color:#393a34">.</span>wrappers <span style="color:#0000ff">import</span> Request<span style="color:#393a34">,</span> Response


<span style="color:#393a34">@Request<span style="color:#393a34">.</span>application</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">my_server</span><span style="color:#393a34">(</span>request<span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>request<span style="color:#393a34">)</span>
    <span style="color:#0000ff">return</span> Response<span style="color:#393a34">(</span><span style="color:#a31515">'Hello Web!'</span><span style="color:#393a34">)</span>


<span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 导入启动服务的模块</em></span>
    <span style="color:#0000ff">from</span> werkzeug<span style="color:#393a34">.</span>serving <span style="color:#0000ff">import</span> run_simple

    run_simple<span style="color:#393a34">(</span><span style="color:#a31515">'127.0.0.1'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">4000</span><span style="color:#393a34">,</span> my_server<span style="color:#393a34">)</span>
</code></span></span>

二、flask快速使用

安装:

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 安装flask会一并安装其依赖:jinja2、Werkzeug、MarkupSafe</em></span>
	pip install flask 

<span style="color:#008000"><em># 版本问题:</em></span>
    <span style="color:#393a34">-</span><span style="color:#36acaa">1</span><span style="color:#393a34">.</span>x 没有本质区别
    <span style="color:#393a34">-</span><span style="color:#36acaa">2</span><span style="color:#393a34">.</span>x 没有本质区别,源码上动了,用起来一样
</code></span></span>

1、快速使用:

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 导入模块</em></span>
<span style="color:#0000ff">from</span> flask <span style="color:#0000ff">import</span> Flask

<span style="color:#008000"><em># 实例化对象,参数内是服务的名字,填入任意都可以</em></span>

app <span style="color:#393a34">=</span> Flask<span style="color:#393a34">(</span>__name__<span style="color:#393a34">)</span>


<span style="color:#008000"><em># 编写函数、注册路由(装饰器方法注册)</em></span>
<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">index</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#0000ff">return</span> <span style="color:#a31515">'hello web!'</span>


<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/home'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">home</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#0000ff">return</span> <span style="color:#a31515">'hello home!'</span>


<span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># app.run('127.0.0.1', 5000)</em></span>
    <span style="color:#008000"><em># 默认监听本地127.0.0.1的5000端口</em></span>
    app<span style="color:#393a34">.</span>run<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>

2、使用flask编写登录小案例

2.1 login.html

<span style="color:#393a34"><span style="background-color:white"><code class="language-html"><span style="color:#008000"><em><span style="color:#393a34"><!</span>DOCTYPE html<span style="color:#393a34">></span></em></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>html</span> <span style="color:#ff0000">lang</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>en<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>head</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>meta</span> <span style="color:#ff0000">charset</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>UTF-8<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>title</span><span style="color:#393a34">></span></span>Title<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>title</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>head</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>body</span><span style="color:#393a34">></span></span>

<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>form</span> <span style="color:#ff0000">method</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>post<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>用户名:<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>input</span> <span style="color:#ff0000">type</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>text<span style="color:#393a34">"</span></span> <span style="color:#ff0000">name</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>username<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>密码:<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>input</span> <span style="color:#ff0000">type</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>password<span style="color:#393a34">"</span></span> <span style="color:#ff0000">name</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>password<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>input</span> <span style="color:#ff0000">type</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>submit<span style="color:#393a34">"</span></span> <span style="color:#ff0000">value</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>登录<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span> {{error}}
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>form</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>html</span><span style="color:#393a34">></span></span>
</code></span></span>

2.2 home.html

<span style="color:#393a34"><span style="background-color:white"><code class="language-html"><span style="color:#008000"><em><span style="color:#393a34"><!</span>DOCTYPE html<span style="color:#393a34">></span></em></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>html</span> <span style="color:#ff0000">lang</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>en<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>head</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>meta</span> <span style="color:#ff0000">charset</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>UTF-8<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>title</span><span style="color:#393a34">></span></span>Title<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>title</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>head</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>h1</span><span style="color:#393a34">></span></span>用户列表<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>h1</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>table</span><span style="color:#393a34">></span></span>
    {% for k,v in user_dict.items() %}
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>tr</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{k}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{v.name}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{v['name']}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{v.get('name')}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>a</span> <span style="color:#ff0000">href</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>/detail/{{k}}<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>查看详细<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>a</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>tr</span><span style="color:#393a34">></span></span>
    {% endfor %}
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>table</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>html</span><span style="color:#393a34">></span></span>
</code></span></span>

2.3 detail.html

<span style="color:#393a34"><span style="background-color:white"><code class="language-html"><span style="color:#008000"><em><span style="color:#393a34"><!</span>DOCTYPE html<span style="color:#393a34">></span></em></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>html</span> <span style="color:#ff0000">lang</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>en<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>head</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>meta</span> <span style="color:#ff0000">charset</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>UTF-8<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>title</span><span style="color:#393a34">></span></span>Title<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>title</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>head</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>名字是:{{user.name}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>年龄是:{{user['age']}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>性别是:{{user.get('gender')}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>{{user.text}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>html</span><span style="color:#393a34">></span></span>
</code></span></span>

2.4 py文件

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#0000ff">from</span> flask <span style="color:#0000ff">import</span> Flask<span style="color:#393a34">,</span> request<span style="color:#393a34">,</span> render_template<span style="color:#393a34">,</span> session<span style="color:#393a34">,</span> redirect

app <span style="color:#393a34">=</span> Flask<span style="color:#393a34">(</span>__name__<span style="color:#393a34">)</span>

<span style="color:#008000"><em># 使用session需要指定key</em></span>
app<span style="color:#393a34">.</span>secret_key <span style="color:#393a34">=</span> <span style="color:#a31515">'abc123'</span>

USERS <span style="color:#393a34">=</span> <span style="color:#393a34">{</span>
    <span style="color:#36acaa">1</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'张三'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">18</span><span style="color:#393a34">,</span> <span style="color:#a31515">'gender'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'男'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text'</span><span style="color:#393a34">:</span> <span style="color:#a31515">"道路千万条"</span><span style="color:#393a34">}</span><span style="color:#393a34">,</span>
    <span style="color:#36acaa">2</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'李四'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">28</span><span style="color:#393a34">,</span> <span style="color:#a31515">'gender'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'男'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text'</span><span style="color:#393a34">:</span> <span style="color:#a31515">"安全第一条"</span><span style="color:#393a34">}</span><span style="color:#393a34">,</span>
    <span style="color:#36acaa">3</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'王五'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">18</span><span style="color:#393a34">,</span> <span style="color:#a31515">'gender'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'女'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text'</span><span style="color:#393a34">:</span> <span style="color:#a31515">"行车不规范"</span><span style="color:#393a34">}</span><span style="color:#393a34">,</span>
<span style="color:#393a34">}</span>


<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">,</span> methods<span style="color:#393a34">=</span><span style="color:#393a34">[</span><span style="color:#a31515">'GET'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'POST'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">index</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 判断路由的方式</em></span>
    <span style="color:#0000ff">if</span> request<span style="color:#393a34">.</span>method <span style="color:#393a34">==</span> <span style="color:#a31515">'GET'</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 返回登陆页面给用户</em></span>
        <span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Login.html'</span><span style="color:#393a34">)</span>
    <span style="color:#008000"><em># post请求判断用户名密码</em></span>
    <span style="color:#0000ff">if</span> request<span style="color:#393a34">.</span>method <span style="color:#393a34">==</span> <span style="color:#a31515">'POST'</span><span style="color:#393a34">:</span>
        username <span style="color:#393a34">=</span> request<span style="color:#393a34">.</span>form<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'username'</span><span style="color:#393a34">)</span>
        password <span style="color:#393a34">=</span> request<span style="color:#393a34">.</span>form<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'password'</span><span style="color:#393a34">)</span>
        <span style="color:#008000"><em># 校验用户名或密码</em></span>
        <span style="color:#0000ff">if</span> username <span style="color:#393a34">==</span> <span style="color:#a31515">'kangkang'</span> <span style="color:#0000ff">and</span> password <span style="color:#393a34">==</span> <span style="color:#a31515">'123'</span><span style="color:#393a34">:</span>
            <span style="color:#008000"><em># 校验成功,保存session(导入、全局使用)</em></span>
            session<span style="color:#393a34">[</span><span style="color:#a31515">'name'</span><span style="color:#393a34">]</span> <span style="color:#393a34">=</span> username
            <span style="color:#008000"><em># 重定向到home页面(导入redirect)</em></span>
            <span style="color:#0000ff">return</span> redirect<span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">)</span>
        <span style="color:#0000ff">else</span><span style="color:#393a34">:</span>
            <span style="color:#008000"><em># 用户名或密码错误</em></span>
            <span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Login.html'</span><span style="color:#393a34">,</span> error<span style="color:#393a34">=</span><span style="color:#a31515">'用户名或密码错误'</span><span style="color:#393a34">)</span>


<span style="color:#008000"><em># 编写首页</em></span>
<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">home</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 先校验用户是否登录</em></span>
    <span style="color:#0000ff">if</span> session<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 校验登录通过,展示首页</em></span>
        <span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Home.html'</span><span style="color:#393a34">,</span> user_dict<span style="color:#393a34">=</span>USERS<span style="color:#393a34">)</span>
    <span style="color:#0000ff">else</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 没有登陆跳转到登陆页面</em></span>
        <span style="color:#0000ff">return</span> redirect<span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">)</span>


<span style="color:#008000"><em># 编写用户详情页</em></span>
<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/detail/<int:pk>'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">detail</span><span style="color:#393a34">(</span>pk<span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 先校验用户是否登录</em></span>
    <span style="color:#0000ff">if</span> session<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 校验登录通过,展示详情页面</em></span>
        user_detail <span style="color:#393a34">=</span> USERS<span style="color:#393a34">[</span>pk<span style="color:#393a34">]</span>
        <span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Detail.html'</span><span style="color:#393a34">,</span> user<span style="color:#393a34">=</span>user_detail<span style="color:#393a34">)</span>
    <span style="color:#0000ff">else</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 没有登陆跳转到登陆页面</em></span>
        <span style="color:#0000ff">return</span> redirect<span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">)</span>


<span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>
    app<span style="color:#393a34">.</span>run<span style="color:#393a34">(</span><span style="color:#393a34">)</span>

</code></span></span>

三、flask配置文件

1、配置文件的几种方式

flask不同于django可以在settings文件编写配置,flask配置文件的方式有多种,相较于django更加灵活

方式一:直接编写

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 在编写app的我呢见中直接编写配置(用于测试)</em></span>
    app<span style="color:#393a34">.</span>debug<span style="color:#393a34">=</span><span style="color:#36acaa">True</span>  
    <span style="color:#008000"><em># 调试模式,提示信息更详细,修改代码不需要重启,自动重启</em></span>
    
    app<span style="color:#393a34">.</span>secret_key<span style="color:#393a34">=</span><span style="color:#a31515">'dasdfasdfasd'</span>  
    <span style="color:#008000"><em># 秘钥,只能 放debug和secret_key</em></span>
</code></span></span>

方式二:使用app.config

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 直接使用flask实例化的对象点出config的方式添加</em></span>
    app<span style="color:#393a34">.</span>config<span style="color:#393a34">[</span><span style="color:#a31515">'DEBUG'</span><span style="color:#393a34">]</span><span style="color:#393a34">=</span><span style="color:#36acaa">True</span>
    app<span style="color:#393a34">.</span>config<span style="color:#393a34">[</span><span style="color:#a31515">'SECRET_KEY'</span><span style="color:#393a34">]</span><span style="color:#393a34">=</span><span style="color:#a31515">'sdfasdfasd'</span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">)</span>
</code></span></span>

方式三:使用py文件,然后载入

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 将配置编写在py文件中,然后使用方法导入(不常用)</em></span>

    app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_pyfile<span style="color:#393a34">(</span><span style="color:#a31515">"settings.py"</span><span style="color:#393a34">)</span>  <span style="color:#008000"><em># 变量必须大写</em></span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">)</span>
</code></span></span>

方式四:使用类导入

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 同样是创建py文件,区别是写在类中,可以上线时候可以指定使用哪套</em></span>

    app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_object<span style="color:#393a34">(</span><span style="color:#a31515">'settings.DevelopmentConfig'</span><span style="color:#393a34">)</span>
    app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_object<span style="color:#393a34">(</span><span style="color:#a31515">'settings.ProductionConfig'</span><span style="color:#393a34">)</span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">)</span> 
</code></span></span>

方式五:其他方式

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 1、通过环境变量导入</em></span>
	 app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_envvar<span style="color:#393a34">(</span><span style="color:#a31515">"环境变量名称"</span><span style="color:#393a34">)</span>
    
<span style="color:#008000"><em># 2、通过json文件载入</em></span>
    app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_json<span style="color:#393a34">(</span><span style="color:#a31515">"json文件名称"</span><span style="color:#393a34">)</span>
    <span style="color:#008000"><em># JSON文件名称,必须是json格式,因为内部会执行json.loads</em></span>
    
<span style="color:#008000"><em># 3、字典格式、配置中心</em></span>
	app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_mapping<span style="color:#393a34">(</span><span style="color:#393a34">{</span><span style="color:#a31515">'DEBUG'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">True</span><span style="color:#393a34">}</span><span style="color:#393a34">)</span>
</code></span></span>

2、常用的配置字段

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#393a34">-</span>DEBUG  <span style="color:#008000"><em># debug模式</em></span>
<span style="color:#393a34">-</span>SECRET_KEY  <span style="color:#008000"><em># session的key值 (密钥)</em></span>
<span style="color:#393a34">-</span>SESSION_COOKIE_NAME  <span style="color:#008000"><em># 用户浏览器上cokie会变成设置的名字</em></span>
<span style="color:#393a34">-</span>PERMANENT_SESSION_LIFETIME  <span style="color:#008000"><em># session过期时间</em></span>

<span style="color:#008000"><em># 内置的配置字段,其他可以写自己的,比如 redis的连接地址,mysql的连接地址</em></span>
</code></span></span>

四、路由系统

1、路由的本质

在django中,路由写在urls.py文件下的path列表中

flask是基于装饰器的,大部分都是使用装饰器来做,少量的可以抽取到urls.py中

路由装饰器源码分析:

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 咱们这样写</em></span>
    <span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">)</span>
    <span style="color:#0000ff">def</span> <span style="color:#393a34">index</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
        <span style="color:#0000ff">pass</span>
    
    <span style="color:#008000"><em>#本质是---》index=app.route('/login')(index)</em></span>
    
    <span style="color:#008000"><em># app.route('/login')的执行结果 decorator 函数</em></span>
    	<span style="color:#393a34">-</span>rule是路径
        <span style="color:#393a34">-</span>其他参数都给了options
    <span style="color:#008000"><em># 然后 decorator(index)--->在执行</em></span>
    		<span style="color:#008000"><em># f是index</em></span>
    		endpoint <span style="color:#393a34">=</span> options<span style="color:#393a34">.</span>pop<span style="color:#393a34">(</span><span style="color:#a31515">"endpoint"</span><span style="color:#393a34">,</span> <span style="color:#36acaa">None</span><span style="color:#393a34">)</span> <span style="color:#008000"><em># 目前没有endpoint,是None</em></span>
            <span style="color:#008000"><em># 核心,本质--》self就是实例化得到的app对象,flask对象</em></span>
            <span style="color:#008000"><em># app对象中有个方法add_url_rule,这是在添加路由</em></span>
            <span style="color:#008000"><em># 不使用装饰器,自己注册路由</em></span>
            self<span style="color:#393a34">.</span>add_url_rule<span style="color:#393a34">(</span>rule<span style="color:#393a34">,</span> endpoint<span style="color:#393a34">,</span> f<span style="color:#393a34">,</span> <span style="color:#393a34">**</span>options<span style="color:#393a34">)</span>
            <span style="color:#0000ff">return</span> f
        
        
    <span style="color:#0000ff">def</span> <span style="color:#393a34">route</span><span style="color:#393a34">(</span>self<span style="color:#393a34">,</span> rule<span style="color:#393a34">:</span> str<span style="color:#393a34">,</span> <span style="color:#393a34">**</span>options<span style="color:#393a34">:</span> t<span style="color:#393a34">.</span>Any<span style="color:#393a34">)</span> <span style="color:#393a34">-</span><span style="color:#393a34">></span> t<span style="color:#393a34">.</span>Callable<span style="color:#393a34">[</span><span style="color:#393a34">[</span>T_route<span style="color:#393a34">]</span><span style="color:#393a34">,</span> T_route<span style="color:#393a34">]</span><span style="color:#393a34">:</span>
        <span style="color:#0000ff">def</span> <span style="color:#393a34">decorator</span><span style="color:#393a34">(</span>f<span style="color:#393a34">:</span> T_route<span style="color:#393a34">)</span> <span style="color:#393a34">-</span><span style="color:#393a34">></span> T_route<span style="color:#393a34">:</span>
            endpoint <span style="color:#393a34">=</span> options<span style="color:#393a34">.</span>pop<span style="color:#393a34">(</span><span style="color:#a31515">"endpoint"</span><span style="color:#393a34">,</span> <span style="color:#36acaa">None</span><span style="color:#393a34">)</span>
            self<span style="color:#393a34">.</span>add_url_rule<span style="color:#393a34">(</span>rule<span style="color:#393a34">,</span> endpoint<span style="color:#393a34">,</span> f<span style="color:#393a34">,</span> <span style="color:#393a34">**</span>options<span style="color:#393a34">)</span>
            <span style="color:#0000ff">return</span> f

        <span style="color:#0000ff">return</span> decorator
    
    
<span style="color:#008000"><em># 可以不使用装饰器的方式,注册路由</em></span>
	app<span style="color:#393a34">.</span>add_url_rule<span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">,</span> endpoint<span style="color:#393a34">=</span><span style="color:#36acaa">None</span><span style="color:#393a34">,</span> view_func<span style="color:#393a34">=</span>home<span style="color:#393a34">,</span> methods<span style="color:#393a34">=</span><span style="color:#393a34">[</span><span style="color:#a31515">'GET'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span>
    
    
<span style="color:#008000"><em># flask路由的本质是app对象的add_url_rule完成路由的注册</em></span>
</code></span></span>

2、add_url_rule参数

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># rule             URL规则</em></span>
<span style="color:#008000"><em># view_func        视图函数名称</em></span>
<span style="color:#008000"><em># defaults = None  默认值, 当URL中无参数,函数需要参数时,使用defaults = {'k': 'v'}为函数提供参数</em></span>
<span style="color:#008000"><em># endpoint = None, 路径的别名,名称,用于反向解析URL,即: url_for('名称')</em></span>
<span style="color:#008000"><em># methods = None, 允许的请求方式,如:["GET", "POST"]</em></span>


<span style="color:#008000"><em>#对URL最后的 / 符号是否严格要求</em></span>
strict_slashes <span style="color:#393a34">=</span> <span style="color:#36acaa">None</span>
    <span style="color:#a31515">'''
        @app.route('/index', strict_slashes=False)
        #访问http://www.xx.com/index/ 或http://www.xx.com/index均可
        @app.route('/index', strict_slashes=True)
        #仅访问http://www.xx.com/index
    '''</span>

<span style="color:#008000"><em>#重定向到指定地址</em></span>
redirect_to <span style="color:#393a34">=</span> <span style="color:#36acaa">None</span><span style="color:#393a34">,</span> 
    <span style="color:#a31515">'''
        @app.route('/index/<int:nid>', redirect_to='/home/<nid>')
    '''</span>
    
    
 <span style="color:#008000"><em># 需要记住的</em></span>
    <span style="color:#008000"><em># rule  </em></span>
    <span style="color:#008000"><em># view_func   </em></span>
    <span style="color:#008000"><em># defaults</em></span>
    <span style="color:#008000"><em># endpoint</em></span>
    <span style="color:#008000"><em># methods</em></span>
</code></span></span>

3、转换器

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"> <span style="color:#a31515">'default'</span><span style="color:#393a34">:</span>          UnicodeConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'string'</span><span style="color:#393a34">:</span>           UnicodeConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'any'</span><span style="color:#393a34">:</span>              AnyConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'path'</span><span style="color:#393a34">:</span>             PathConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'int'</span><span style="color:#393a34">:</span>              IntegerConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'float'</span><span style="color:#393a34">:</span>            FloatConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'uuid'</span><span style="color:#393a34">:</span>             UUIDConverter<span style="color:#393a34">,</span>
    
 <span style="color:#008000"><em># 了解:让路由支持正则(忽略掉)</em></span></code></span></span>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/43836.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【python】在matlab中调用python

参考 Matlab调用Python - 知乎 (zhihu.com) 说一下我犯的错误&#xff1a; 1、电脑上有没有python都可以&#xff0c;我以为anaconda里的python不行&#xff0c;又重新下了一个python3.8 实际上导入的时候可以用 pyversion(D:\myDownloads\anaconda\envs\pytorch38\pytho…

C++笔记之STL的sort使用第三个参数来自定义排序

C笔记之STL的sort使用第三个参数来自定义排序 code review! 文章目录 C笔记之STL的sort使用第三个参数来自定义排序1.方法一&#xff1a;使用比较函数(其实是使用函数指针)作为std::sort()的第三个参数来排序2.方法二&#xff1a;使用lambda表达式作为std::sort()的第三个参数…

C++OpenCV(2):图像处理基础概念与操作

&#x1f506; 文章首发于我的个人博客&#xff1a;欢迎大佬们来逛逛 &#x1f506; OpenCV项目地址及源代码&#xff1a;点击这里 文章目录 图形读取与显示加载图片显示图片打印图片信息保存图片 色彩模型转换RGB颜色模型HSV颜色模型HLS模型LAB模型 图像像素读写操作像素算数运…

MySQL数据库第十一课---------SQl语句的拔高-------水平提升

作者前言 个人主页::小小页面 gitee页面:秦大大 一个爱分享的小博主 欢迎小可爱们前来借鉴 ______________________________________________________ 目录 SQL提高 日期函数 length round reverse substring ifnull case when cast grouping sets 排序函数 开窗函…

计算机视觉(二)图像特征提取

文章目录 颜色特征量化颜色直方图适用颜色空间&#xff1a;RGB、HSV等颜色空间操作 几何特征边缘 Edge边缘定义边缘提取 基于关键点的特征描述子引入几何特征&#xff1a;关键点几何特征&#xff1a;Harris角点FAST角点检测几何特征&#xff1a;斑点局部特征&#xff1a;SIFT预…

【Linux从入门到精通】进程的控制(进程替换)

本篇文章会对进程替换进行讲解。希望本篇文章会对你有所帮助 文章目录 一、进程替换概念 二、进程替换函数 2、1 execl 2、2 execlp 2、3 execv 2、3 execle 2、4 execve 三、总结 &#x1f64b;‍♂️ 作者&#xff1a;Ggggggtm &#x1f64b;‍♂️ &#x1f440; 专栏&…

视频讲解Codeforces Round 887 (Div. 2)(A--C)

文章目录 A. Desorting1、板书2、代码 B. Fibonaccharsis1、板书2、代码 C. Ntarsis Set1、板书2、代码 视频讲解Codeforces Round 887 (Div. 2)&#xff08;A–C&#xff09; A. Desorting 1、板书 2、代码 #include<bits/stdc.h> #define endl \n #define INF 0x3f…

区间预测 | MATLAB实现基于QRF随机森林分位数回归时间序列区间预测模型

区间预测 | MATLAB实现基于QRF随机森林分位数回归时间序列区间预测模型 目录 区间预测 | MATLAB实现基于QRF随机森林分位数回归时间序列区间预测模型效果一览基本介绍程序设计参考资料 效果一览 基本介绍 1.Matlab实现基于QRF随机森林分位数回归时间序列区间预测模型&#xff1…

从零到一nvm、npm、cnpm、yarn、vue全套安装和环境配置以及创建新项目和如何运行人家的项目大全,最详细,保姆级

NVM 1.下载 下载地址&#xff1a;Releases coreybutler/nvm-windows GitHub 可能需要开启魔法 划到下面。找到如图所示的文件下载即可2. 2.安装&#xff08;要记住安装的路径&#xff09; 基本一键默认即可&#xff0c;你要修改路径也可以&#xff0c;但不建议 下载好…

【开发问题】flink-cdc不用数据库之间的,不同类型的转化

不同的数据库之期间数据类型转化 问题来源与原因解决过程&#xff0c;思路错误&#xff0c;导致各种错误错误思路是什么 正确解决方式&#xff0c;找官网对应的链接器&#xff0c;数据转化 问题来源与原因 我一开始是flink-cdc&#xff0c;oracle2Mysql&#xff0c;sql 我一开…

删除Windows顽固启动项Program

卸载软件之后可能会遇到任务管理器中启动项显示有Program这种无效项目的情况 无效项目如图所示: 解决方法 打开注册表 Win R 打开注册表后定位到 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run 可以直接复制粘贴到地址栏进行跳…

Orangepi docker Home Assistant部署

手册上多数应用在树莓派用过&#xff0c;发现一个Home Assistant之前没搞过 按照手册先安装docker&#xff0c;使用enable_docker.sh 然后使用 docker run hello-world 来测试docker是否成功&#xff0c;如果hello from docker&#xff0c;即正常 通过以下docker命令搜索hom…

微服务探索之路06篇k8s配置文件Yaml部署Redis使用Helm部署MongoDB和kafka

1 安装Redis 1.1创建配置文件redis.conf 切换到自己的目录下如本文是放在/home/ubuntu下 cd /home/ubuntuvim redis.conf bind 0.0.0.0 protected-mode yes port 6379 requirepass qwe123456 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no pidfile /var/run/r…

【Spring】ApplicationEventPublisher 发布订阅模式

概念 关于发布订阅这个词&#xff0c;其实不仅仅出现在Spring框架当中&#xff0c;其实在Redis中也有存在&#xff08;其对应的是convertAndSend()方法&#xff09;&#xff0c;还有在MQ消息队列里也是有的&#xff0c;但这里就主要介绍的是关于Spring框架的ApplicationEventPu…

怎么把PDF转为word?1分钟解决难题

PDF文件在我们的电脑上应用非常广泛&#xff0c;由于其较高的安全性和兼容性&#xff0c;得到了广泛的认可。然而&#xff0c;对于一些人来说&#xff0c;PDF文件不能直接进行编辑和修改可能是一个问题。因此&#xff0c;通常我们需要将其转换为Word格式&#xff0c;以便在Word…

成为“AI+的UGC社交平台”,亚马逊云科技助力博宇盖乐向“3D UGC社交门户”迈进

随着元宇宙浪潮逐渐升温&#xff0c;以玩家为主导的UGC游戏平台获得空前关注。多元化的视觉呈现方式&#xff0c;人人可参与、交互的玩法生态&#xff0c;具有UGC属性的游戏平台在极大提升玩家参与度、增加游戏趣味性的同时&#xff0c;也为游戏行业的内容创作带来了新的想象空…

5分钟开发一个AI论文抓取和ChatGPT提炼应用

5分钟开发一个AI论文抓取和ChatGPT提炼应用 第一步 点击“即刻开始” -选择模板 python -修改标题 “AIPaper”&#xff0c;项目标识“AIPaper”&#xff0c;点击“创建项目” 第二步 在编程区域右侧AI区域&#xff0c;输入框输入以下内容&#xff1a; 请根据下面的内容&…

MacBook外接键盘修改键位

众所周知&#xff0c;MacBook的键盘和Windows差别很大&#xff0c;比如我们最常用的ctrlcv在Mac下是commandcv…而外接键盘往往是Windows布局&#xff0c;因此如何修改外接键盘键位就是一件很重要的事情&#xff01; 首先&#xff0c;我们要知道Win键在Mac系统中是多余的&…

客户案例 | 思腾合力服务器助力西安电子科技大学人工智能实验室建设

客户介绍 西安电子科技大学是以信息与电子学科为主&#xff0c;工、理、管、文多学科协调发展的全国重点大学&#xff0c;直属教育部&#xff0c;是国家“优势学科创新平台”项目和“211工程”项目重点建设高校之一、国家双创示范基地之一、首批35所示范性软件学院、首批9所示范…

机器学习术语解析与应用(一)

文章目录 &#x1f340;引言&#x1f340;数据集&#x1f340;特征工程&#xff08;Feature Engineering&#xff09;&#x1f340;模型&#xff08;Model&#xff09;&#x1f340;算法&#xff08;Algorithm&#xff09;&#x1f340;训练&#xff08;Training&#xff09;&a…