0
点赞
收藏
分享

微信扫一扫

windows python web flask Hello World实战


文章目录

  • ​​1. hello world项目​​
  • ​​1.1 新建一个项目​​
  • ​​1.2 hello world项目修改无效的原因​​
  • ​​1.3 修改hello world成功的效果​​
  • ​​1.3.1 修改显示内容​​
  • ​​1.3.2 自定义配置​​
  • ​​1.3.3 调试模式​​
  • ​​1.3.4 绑定IP和端口​​
  • ​​1.4 templates的调用​​
  • ​​1.5 templates的传参​​
  • ​​1.6 static的访问​​
  • ​​1.7 html文件调用加载static目录的图片​​

  • ​​linux python web flask Hello World实战​​
  • ​​windows python web flask 模板开放实战​​
  • ​​windows python web flask获取请求参数数据​​

1. hello world项目

1.1 新建一个项目

windows python web flask Hello World实战_flask

在默认的app.py点击运行
windows python web flask Hello World实战_html_02
windows python web flask Hello World实战_html_03

1.2 hello world项目修改无效的原因

修改返回内容,发现仍然是​​“hello world”​​,后来发现是端口占用

C:\Users\XH>netstat -aon|findstr "5000"
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       21380
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       21488
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       19480
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       20848
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       19816
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       6900
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       19568
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       20688
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       12516
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       16360
  TCP    127.0.0.1:5000         0.0.0.0:0              LISTENING       19900
  TCP    127.0.0.1:50000        0.0.0.0:0              LISTENING       4820

C:\Users\XH>netstat -aon|findstr "5000"

通过进程号查询占用进程命令,原来是多余的python.exe

C:\Users\XH>tasklist|findstr  19900
python.exe                   19900 Console                    2     27,332 K

杀死进程

C:\Users\XH>taskkill  /f  /pid  21380
成功: 已终止 PID 为 21380 的进程。

1.3 修改hello world成功的效果

1.3.1 修改显示内容

​app.py​​重新测试修改返回值。

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Welcome to Hello World !'

@app.route('/hello')
def hello_world2():
    return 'Hello World 2!'
    
if __name__ == '__main__':
    app.run()

重新运行
注意:要关闭之前的启动进程。CTRL + C或者点击重新运行。
windows python web flask Hello World实战_html_04
windows python web flask Hello World实战_html_05

app.py设置一个​​html​​内容

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Welcome to Hello World !'

@app.route('/hello')
def hello_world2():
    return '''
    <html>
    <body>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;h1&nbsp;style=&quot;color:#e00;">hello world</h>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/html&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&#39;''
if&nbsp;__name__&nbsp;==&nbsp;&#39;__main__':
&nbsp;&nbsp;&nbsp;&nbsp;app.run()

重新运行:
windows python web flask Hello World实战_python_06

1.3.2 自定义配置

自定义项目名称、​​static​​​静态文件位置、​​templates​​​模板位置(一般不常用)
windows python web flask Hello World实战_重新运行_07

1.3.3 调试模式

在app.py修改

if&nbsp;__name__&nbsp;==&nbsp;&#39;__main__':
&nbsp;&nbsp;&nbsp;&nbsp;app.run(debug=True)

重新运行:
windows python web flask Hello World实战_重新运行_08

1.3.4 绑定IP和端口

这里我设置本机的ip地址192.168.1.4,然后设置不被占用的端口
windows python web flask Hello World实战_flask_09
重新运行:
windows python web flask Hello World实战_python_10

1.4 templates的调用

但是这样的格式,对于维护网页成本很高,当面对复杂的网页时,因此,可以利用我们的templates文件夹进行配置。
在templates目录下创建一个hello.html文件,并复制刚才app.py中的html内容。
windows python web flask Hello World实战_html_11
然后app.py调用templates的文件。

from&nbsp;flask&nbsp;import&nbsp;Flask,&nbsp;render_template&nbsp;&nbsp;#多添加一个方法

app&nbsp;=&nbsp;Flask(__name__)


@app.route(&#39;/')
def&nbsp;hello_world():
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&#39;Welcome to Hello World !'

@app.route(&#39;/hello')
def&nbsp;hello_world2():
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;render_template(&quot;hello.html&quot;)&nbsp;&nbsp;#方法中指定文件

if&nbsp;__name__&nbsp;==&nbsp;&#39;__main__':
&nbsp;&nbsp;&nbsp;&nbsp;app.run()

重新运行:
windows python web flask Hello World实战_html_12
如此看来,app.py非常简约。

1.5 templates的传参

修改app.py设置变量
windows python web flask Hello World实战_重新运行_13
修改hello.py文件调用变量名
windows python web flask Hello World实战_html_14
重新运行:
windows python web flask Hello World实战_flask_15

1.6 static的访问

将一张图片放到static目录下
windows python web flask Hello World实战_flask_16
我们不需要重启,可以直接访访问这张图片。
windows python web flask Hello World实战_重新运行_17

1.7 html文件调用加载static目录的图片

hello.html文件修改:
windows python web flask Hello World实战_html_18
重新运行:
windows python web flask Hello World实战_重新运行_19

举报

相关推荐

0 条评论