windows python web flask 编写 Hello World
tags: flask
文章目录
1. hello world项目
1.1 新建一个项目
在默认的app.py点击运行
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或者点击重新运行。
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>
<h1 style="color:#e00;">hello world</h>
</body>
</html>
'''
if __name__ == '__main__':
app.run()
重新运行:
1.3.2 自定义配置
自定义项目名称、static
静态文件位置、templates
模板位置(一般不常用)
1.3.3 调试模式
在app.py修改
if __name__ == '__main__':
app.run(debug=True)
重新运行:
1.3.4 绑定IP和端口
这里我设置本机的ip地址192.168.1.4,然后设置不被占用的端口
重新运行:
1.4 templates的调用
但是这样的格式,对于维护网页成本很高,当面对复杂的网页时,因此,可以利用我们的templates文件夹进行配置。
在templates目录下创建一个hello.html文件,并复制刚才app.py中的html内容。
然后app.py调用templates的文件。
from flask import Flask, render_template #多添加一个方法
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Welcome to Hello World !'
@app.route('/hello')
def hello_world2():
return render_template("hello.html") #方法中指定文件
if __name__ == '__main__':
app.run()
重新运行:
如此看来,app.py非常简约。
1.5 templates的传参
修改app.py设置变量
修改hello.py文件调用变量名
重新运行:
1.6 static的访问
将一张图片放到static目录下
我们不需要重启,可以直接访访问这张图片。
1.7 html文件调用加载static目录的图片
hello.html文件修改:
重新运行:
更多阅读: