惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

冰屋

在TeamSpeak内搭建网易云音乐bot – 冰屋 Linux to go — Rime输入法 – 冰屋 Linux to go — 配置和初体验 – 冰屋 【杂谈】如何快速备考托福 – 冰屋 使用自建N2N加速P2P联机游戏 – 冰屋 FastAPI与SQLModel – 冰屋 SQLModel快速上手 – 冰屋 看不了番剧?自动化你的番剧订阅流程吧! – 冰屋 你好,世界 – 冰屋
FastAPI快速上手 – 冰屋
2024-11-29 · via 冰屋
  • 提供API的框架
  • 小,快,灵
  • 我也不会在这里写大段大段的介绍,非常没有意思w

安装

pip install "fastapi[all]"

噢,对了,你最好使用Python 3.10及以上的版本,这样你就可以使用更多更方便的语法了

第一个API

main.py中:

# main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}

此时,在命令行中运行:

uvicorn main:app --reload

# 参数解释:
# main: main.py
# app: app = FastAPI()
# --reload: 服务器会在更新代码后重新启动,方便调试

就能开启服务了。此时,访问127.0.0.1:8000/docs即可看到API文档

发生了什么?

  • @app.get使用的@代表这行是一个Decorator。关于Decorator,你可以查看Python 函数装饰器 | 菜鸟教程。简单来说,这是一个接受一个函数、返回一个函数的函数
  • 当然,你还可以用@app.post@app.delete什么的
  • /items/{item_id}是这个API对应的URL。这里{item_id}使用花括号扩着,代表你可以在下面的函数内用同名参数获得这个值。当然,这种参数在你的URL里不是必须的
  • async def代表这个函数是一个异步函数,你可以查看Python 异步编程入门 – 阮一峰的网络日志
  • read_item是处理这个API请求的函数,他的参数会自动从URL中获取,或是自动转成Query或Body中的参数
  • 在经过处理后,你可以返回一个字典(其实就是json)作为返回信息。你也可以返回一个Pydantic模型或是一个SQLModel模型(后者是前者的子类)

接下来呢?

我觉得现在知道这些够了,足够让你面对FastAPI应用代码时不用晕头转向

其实我建议你在需要用到相应指示的时候直接查询FastAPI官方文档,而非一开始就全部学完

|´・ω・)ノ

ヾ(≧∇≦*)ゝ

(☆ω☆)

(╯‵□′)╯︵┴─┴

 ̄﹃ ̄

(/ω\)

∠( ᐛ 」∠)_

(๑•̀ㅁ•́ฅ)

→_→

୧(๑•̀⌄•́๑)૭

٩(ˊᗜˋ*)و

(ノ°ο°)ノ

(´இ皿இ`)

⌇●﹏●⌇

(ฅ´ω`ฅ)

(╯°A°)╯︵○○○

φ( ̄∇ ̄o)

ヾ(´・ ・`。)ノ"

( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃

(ó﹏ò。)

Σ(っ °Д °;)っ

( ,,´・ω・)ノ"(´っω・`。)

╮(╯▽╰)╭

o(*////▽////*)q

>﹏<

( ๑´•ω•) "(ㆆᴗㆆ)

😂

😀

😅

😊

🙂

🙃

😌

😍

😘

😜

😝

😏

😒

🙄

😳

😡

😔

😫

😱

😭

💩

👻

🙌

🖕

👍

👫

👬

👭

🌚

🌝

🙈

💊

😶

🙏

🍦

🍉

😣

Source: github.com/k4yt3x/flowerhd