使用 PyCharm 构建 FastAPI 项目:零基础入门 Web API 开发

使用 PyCharm 构建 FastAPI 项目:零基础入门 Web API 开发

本文提供了一份完整的 FastAPI 入门指南,涵盖从环境搭建、依赖安装到创建并运行一个简单的 FastAPI 应用的各个步骤。通过 FastAPI 和 Uvicorn,开发者可以快速构建现代化的 Web API。文章还介绍了如何使用 PyCharm 创建 Python 项目、如何编写 API 路由和数据模型,并通过 Swagger UI 和 ReDoc 自动生成交互式 API 文档进行测试。本文适合初学者了解 FastAPI 的基础知识,并快速上手开发高效的 Web API。

文章目录

  • [使用 PyCharm 构建 FastAPI 项目:零基础入门 Web API 开发](#使用 PyCharm 构建 FastAPI 项目:零基础入门 Web API 开发)
      • [一 FastAPI 依赖简述](#一 FastAPI 依赖简述)
      • [二 使用 PyCharm 创建 python 应用](#二 使用 PyCharm 创建 python 应用)
        • [1 使用 Pure Python](#1 使用 Pure Python)
        • [2 使用 FastAPI 插件](#2 使用 FastAPI 插件)
      • [三 FastAPI 安装](#三 FastAPI 安装)
        • [1 安装 FastAPI](#1 安装 FastAPI)
        • [2 安装 ASGI 服务器](#2 安装 ASGI 服务器)
      • [四 FastAPI 示例](#四 FastAPI 示例)
      • [五 启动应用](#五 启动应用)
      • [六 检查运行状态](#六 检查运行状态)
      • [七 交互式 API 文档](#七 交互式 API 文档)
        • [1 Swagger UI](#1 Swagger UI)
        • [2 ReDoc 文档](#2 ReDoc 文档)
      • [八 源码地址](#八 源码地址)

一 FastAPI 依赖简述

FastAPI 核心技术栈 说明
Python 建议版本 python 3.6+
Starlette 用于处理 Web 部分
Pydantic 用于处理数据验证和解析

二 使用 PyCharm 创建 python 应用

File -> New Project... ,本文使用 Pure Python 创建应用。

1 使用 Pure Python
2 使用 FastAPI 插件

三 FastAPI 安装

1 安装 FastAPI

要安装 FastAPI,可以运行以下命令。

sh 复制代码
# 本文安装的是 fastapi[standard]
pip install "fastapi[standard]"
pip install "fastapi[all]"
pip install fastapi
2 安装 ASGI 服务器

ASGI 服务器是用来运行应用程序的,推荐在生产环境中使用 Uvicorn

sh 复制代码
pip install "uvicorn[standard]"

安装 FastAPI 一般会带上 uvicorn,当然你也可以用其他 ASGI 服务器。

四 FastAPI 示例

下面是一个简单的 FastAPI 示例,将这段代码拷贝到 main.py 中 。

python 复制代码
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("/")
def read_root():
    return {"Hello": "World"}

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

@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}

五 启动应用

要运行应用程序,使用以下命令启动服务器:

sh 复制代码
uvicorn main:app --reload

命令说明:

  • main:指向 main.py 文件(即 Python 模块)。
  • app:在 main.py 文件中通过 app = FastAPI() 创建的应用实例。
  • --reload:开启代码热重载,使得修改代码后服务器自动重启(适用于开发环境)。

运行日志

lua 复制代码
INFO:     Will watch for changes in these directories:[xxx]
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [26400] using WatchFiles
INFO:     Started server process [26404]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

六 检查运行状态

访问 http://127.0.0.1:8000/items/5?q=somequery 来验证应用的工作情况,返回 JSON。

json 复制代码
{
	"item_id": 5,
	"q": "somequery"
}

七 交互式 API 文档

1 Swagger UI

访问 http://127.0.0.1:8000/docs,看到自动生成的交互式 API 文档( Swagger UI )。

2 ReDoc 文档

访问 http://127.0.0.1:8000/redoc,查看另一种自动生成的文档界面( ReDoc )。

八 源码地址

详情见:GitHub FastApiProj

引用: FastAPI 文档

相关推荐
IT_陈寒43 分钟前
折腾一天才明白:Vite的热更新为什么偶尔会罢工
前端·人工智能·后端
希望永不加班1 小时前
SpringBoot 自动配置类加载顺序与优先级
java·spring boot·后端·spring·mybatis
AI茶水间管理员2 小时前
学习ClaudeCode源码之Agent核心循环
前端·人工智能·后端
7年前端辞职转AI2 小时前
Python 文件操作
python·编程语言
龙文浩_2 小时前
AI梯度下降与PyTorch张量操作技术指南
人工智能·pytorch·python·深度学习·神经网络·机器学习·自然语言处理
呱牛do it2 小时前
企业级绩效考核系统设计与实现:基于FastAPI + Vue3的全栈解决方案
python·fastapi
Reart2 小时前
从0解构tinyWeb项目--(Day:2)
javascript·后端·架构
7年前端辞职转AI2 小时前
Python 容器数据类型
python·编程语言
云霄IT2 小时前
安卓开发之java转dex再转smali
android·java·python
果汁华2 小时前
Typer:基于类型提示的现代Python CLI框架
开发语言·网络·python