Skip to content

Latest commit

 

History

History
86 lines (64 loc) · 1.9 KB

File metadata and controls

86 lines (64 loc) · 1.9 KB
Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0616
- 这是 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` 
---

fastapi

回忆

  • 上次我们添加了一种请求方法PUT
  • 跑是能跑起来
  • 但是有些不清楚的地方
@app.get("/")
async def read_root():
    return {"Hello": "World"}
  • 这个函数到底什么意思?
  • 我们从头来分析

分析代码

图片描述

  • 从FastAPI这个包中
    • 引入fastapi这个类
  • FastAPI 是 Starlette的派生类
    • 可以使用Starlette中的方法
    • Starlette是我们的web框架

产生一个实例

图片描述

  • main.py中的app
  • 其实对应了shell中的命令
  • uvicorn main:app --reload

装饰器

图片描述

  • 使用的方法是get方法
  • get的路径是/

定义函数

图片描述

  • async 的意思是异步等待
    • 没有响应结果他会去服务下一个请求
    • 有了结果再给过去

返回结果

图片描述

  • 返回的类型
    • 字典
    • 列表
    • 字符串
    • 数字

图片描述

总结🤔

  • 这里理解了整个函数的结构
    • 从导入
    • 到实例化
    • 再到函数修饰器
    • 函数定义
    • 函数返回值
  • 对于根下的访问我们了解了
  • 对于具体路径的访问是如何的呢?🤔
  • 下次再说👋

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