Skip to content

Latest commit

 

History

History
107 lines (75 loc) · 2.13 KB

File metadata and controls

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

回忆

  • 上次我们研究了fastapi的展示效果
  • 为什么fastapi被叫做fastapi?
  • 因为他可以生成一个openapi.json
  • 然后用不同的ui可以渲染成页面
  • 除了Get之外我们可以别的提交方法吗?

停止服务器

图片描述

  • ctrl + c
  • 结束服务
  • 回到vim进行修改

修改main.py

from typing import Union

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()


class Item(BaseModel):
    name: str
    price: float
    is_offer: Union[bool, None] = None


@app.get("/")
async def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
async def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}
  • 然后再重启服务器
  • :w|!uvicorn main:app --reload

启动浏览器

图片描述

  • 出现了一个新的url
  • 提交方法不是原来的Get
  • 而是Put
  • 可以执行这个api吗?

Try it out

  • 可以设置参数
  • 然后执行

图片描述

  • 可以看到相应的执行结果
  • 总共有什么样的请求方式呢?

请求方式

  • 总共四种
    • POST: to create data.
    • GET: to read data.
    • PUT: to update data.
    • DELETE: to delete data.

总结

  • 跑是能跑起来
  • 但是有些不清楚的地方
@app.get("/")
async def read_root():
    return {"Hello": "World"}
  • 这个函数到底什么意思?🤔
  • 下次再说👋

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