Skip to content

Latest commit

 

History

History
376 lines (253 loc) · 7.72 KB

File metadata and controls

376 lines (253 loc) · 7.72 KB
Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0569
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在: 
	- 个人网站: `https://oeasy.org` 
	- 蓝桥云课: `https://www.lanqiao.cn/courses/3584` 
	- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial` 
	- Gitee: `https://gitee.com/overmind1980/oeasypython` 
---

flask 最小应用

回忆上次

  • 上次 了解到 路由
    • 用装饰器 声明url
    • 再 将路由 封装成 函数

图片描述

  • 服务器 都注册了 些路由 呢?

恢复环境

  • 首先 要 恢复 上次的 环境
mkdir myproject
cd myproject
vi app.py
  • 编辑app.py
from flask import Flask

app = Flask(__name__)
@app.route('/')
def index():
    return 'Index Page'

@app.route('/hello')
def hello():
    return 'Hello, World'

if __name__ == "__main__":
    app.run(debug=True,host="0.0.0.0",port=8081)

访问

  • 后台运行app 并浏览
nohup python3 app.py >> flask.log 2>&1 &
firefox http://127.0.0.1:8081/hello http://127.0.0.1:8081/ &
  • 两个路由都能访问

图片描述

  • 可以访问动态的地址吗?

动态地址

图片描述

  • 如果改成3781
    • 就变成了 另一门课程

图片描述

  • 还有什么类似的吗?

用户id

图片描述

  • 每个网站 都有查看用户id的页面
    • 根据用户id 可以生成 页面

图片描述

  • 如何根据url中的动态信息生成网页呢?

提问ai

图片描述

url中的变量

图片描述

  • 具体试试

动态路由

  • url中 的 name
    • 可以是 任意字符串
    • 所以是 动态的
    • 动态路径 的 路由
from flask import Flask
from markupsafe import escape

app = Flask(__name__)

@app.route("/<name>")
def hello(name):
    return f"Hello, {escape(name)}!"

if __name__ == "__main__":
    app.run(debug=True,host="0.0.0.0",port=8081)
  • 由于 服务器是 后台运行的 debug模式
    • 保存文件 会自动加载代码

浏览网页

firefox http://127.0.0.1:8081/oeasy &
  • 访问成功

图片描述

  • 可以根据 name 做点什么吗?

进行判定

  • 根据字符串的长度进行判定
from flask import Flask
from markupsafe import escape

app = Flask(__name__)

@app.route("/<name>")
def hello(name):
    len_of_name = len(name)
    if len_of_name > 5:
        return "name is long"
    elif len_of_name <= 4:
        return "name is short"
    else:
        return f"Hello, {escape(name)}!"

if __name__ == "__main__":
    app.run(debug=True,host="0.0.0.0",port=8081)

观察

firefox http://127.0.0.1:8081/oeasyo
  • 效果访问 太长

图片描述

观察2

firefox http://127.0.0.1:8081/o &
  • 太短

图片描述

观察3

firefox http://127.0.0.1:8081/oeasy &
  • 长短合适

图片描述

  • 为什么要有动态路由呢?

动态路由的意义

  • 我们看 视频的时候
    • 要明确 某个BV号
    • 根据BV号 访问视频相关的 内容

图片描述

  • BV号 就是 动态路由

图片描述

  • 动态路由变量 还设置 类型吗?

多条动态路由

from flask import Flask
from markupsafe import escape

app = Flask(__name__)

@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return f'User {escape(username)}'

@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return f'Post {post_id}'

@app.route('/path/<path:subpath>')
def show_subpath(subpath):
    # show the subpath after /path/
    return f'Subpath {escape(subpath)}'


if __name__ == "__main__":
    app.run(debug=True,host="0.0.0.0",port=8081)
  • 这里有三类路由
路由类型 路由定义语法 匹配规则说明 示例 URL 及对应处理逻辑
字符串地址(无约束) /user/ 匹配任意字符串(除 / 外 ) 如 localhost:5000/user/oeasy ,username 为 oeasy ,返回 User oeasy
整数类型约束 /post/int:post_id 仅匹配整数形式内容 如 localhost:5000/post/123 ,post_id 为 123 ,返回 Post 123
路径类型约束 /path/path:subpath 匹配包含 / 的完整路径片段 如 localhost:5000/path/a/b/c ,subpath 为 a/b/c ,返回 Subpath a/b/c

三类路由

  1. 字符串地址
@app.route('/user/<username>')
def show_user_profile(username):
    # show the user profile for that user
    return f'User {escape(username)}'

图片描述

  1. 整型数字
@app.route('/post/<int:post_id>')
def show_post(post_id):
    # show the post with the given id, the id is an integer
    return f'Post {post_id}'

图片描述

  1. 子路径地址
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
    # show the subpath after /path/
    return f'Subpath {escape(subpath)}'

图片描述

  • 可以看看现在 有哪些路由吗 ?

url映射规则

  • 修改 主程序
    • 输出 路由地图
    • url_map
if __name__ == "__main__":
	print(app.url_map)
	app.run(debug=True,host="0.0.0.0",port=8081)
  • 后台自动更新后
    • 这条输出语句能看到吗?
tail -n 5 flask.log
  • 在if中的这句输出没有运行

图片描述

  • 需要重启Web服务器

找到进程pid

lsof -i:8081
  • 找到占用 8081的进程pid
    • 381
    • 743

图片描述

  • 结束进程
kill -9 381  743
  • 成功结束flask web服务器

图片描述

再启动

  • 重启 服务器
nohup python3 app.py >> flask.log 2>&1 &
  • 观察日志
tail -n 20 flask.log
  • 显示规则

图片描述

总结

  • 这次 了解到 路由
    • 用装饰器 声明url
    • 再 将路由 封装成 函数
  • url 中 可以包含变量
    • 变量作为参数 传入处理函数
    • 进行处理
路由类型 路由定义语法 匹配规则说明 示例 URL 及对应处理逻辑
字符串地址(无约束) /user/ 匹配任意字符串(除 / 外 ) 如 localhost:5000/user/oeasy ,username 为 oeasy ,返回 User oeasy
整数类型约束 /post/int:post_id 仅匹配整数形式内容 如 localhost:5000/post/123 ,post_id 为 123 ,返回 Post 123
路径类型约束 /path/path:subpath 匹配包含 / 的完整路径片段 如 localhost:5000/path/a/b/c ,subpath 为 a/b/c ,返回 Subpath a/b/c
  • 大型应用 有各种 要处理的url
    • 都要 放 app.py 里
    • 能分门别类 放到 不同py文件 中吗??
  • 下次再说👋🏻

  • 本文来自 oeasy Python 系统教程。
  • 想完整、扎实学 Python,
  • 搜索 oeasy 即可。