参加你附近的 ,了解 VS Code 中的 AI 辅助开发。

Visual Studio Code 中的 FastAPI 教程

FastAPI 是一个现代高性能的 Python Web 框架,用于构建 API。它旨在帮助您快速高效地构建 API,同时提供自动验证、序列化和 API 文档等功能,使其成为构建 Web 服务和微服务的热门选择。

在本 FastAPI 教程中,我们将使用 FastAPI 创建一个购物清单应用程序。在本教程结束时,您将了解如何在 Visual Studio Code 终端、编辑器和调试器中使用 FastAPI。本教程并非 FastAPI 深度教程。如需深入了解,您可以查阅 FastAPI 官方文档

如果您是第一次使用 Python,我们建议您从我们的 Python 教程 开始,熟悉该语言和 VS Code 的 Python 支持。本教程更适合那些已经熟悉 Python 并希望学习如何在 VS Code 中使用 FastAPI 的人。

本 FastAPI 教程中完成的代码项目可在 GitHub 上找到:python-sample-vscode-fastapi-tutorial

如果你有任何问题,可以在 Python 扩展讨论问答中搜索答案或提问。

设置项目

有多种方法可以为本教程设置项目。我们将介绍如何在 GitHub Codespaces本地 VS Code 中设置它。

GitHub Codespaces

您可以将此项目设置为在 GitHub Codespaces 中开发,您可以在其中在 codespace 中远程编写代码、调试和运行应用程序。codespace 提供了一个完全配置的云托管开发环境,无需本地设置。此环境包括您项目的依赖项、工具和扩展,确保一致且可重现的开发体验。它通过提供实时编辑、集成版本控制以及轻松访问调试和测试工具来简化协作,同时保持项目的安全性和可靠性。

注意:所有 GitHub.com 帐户都包含在免费或专业计划中的 GitHub Codespaces 月度免费使用配额。有关详细信息,请访问 关于 GitHub Codespaces 的计费

要为本教程设置 codespace,请导航到此项目的 GitHub 存储库。此 codespace 包含所有必要的配置和依赖项,以便快速开始 FastAPI 开发。

对于本教程,选择 dictionarybased 分支

dictionarybased branch selected in the python-sample-vscode-fastapi-tutorial GitHub repo

然后,选择 Code > Codespaces > Create Codespace on <dictionarybased> 分支以创建并打开项目的 codespace。

完成后,您可以继续下面的 替换数据库 部分。

本地 VS Code

要在 VS Code 中成功完成本教程,您首先需要设置 Python 开发环境。具体来说,本教程要求

在本节中,我们将创建一个文件夹作为 VS Code 中的工作区打开,设置一个 Python 虚拟环境,并安装项目的依赖项。

  1. 在您的文件系统中,为本教程创建一个项目文件夹,例如 groceries-plugin

  2. 在 VS Code 中打开此新文件夹 (文件 > 打开文件夹…)。

  3. 当出现工作区信任提示时,选择是,我信任作者以允许工作区访问必要的资源和扩展。您可以在文档中了解有关工作区信任的更多信息。

现在,让我们创建一个 requirements.txt 文件,其中列出了我们希望为应用程序安装的依赖项。requirements.txt 文件是 Python 开发中的常见做法,用于指定项目所依赖的库及其版本。此文件有助于确保任何从事该项目的人都可以重新创建类似的开发环境,使其成为保持一致性的便捷组件。

我们将安装 FastAPI 用于创建应用程序,uvicorn 作为服务器,以及 Redistype-redis 用于处理数据存储和与 Redis 数据库交互。

  1. 在 VS Code 中创建新文件(文件 > 新建文本文件⌘N (Windows, Linux Ctrl+N))。

  2. 向其中添加以下内容

    fastapi
    redis
    types-redis
    uvicorn
    
  3. 保存文件(⌘S (Windows, Linux Ctrl+S))并将其命名为 requirements.txt

  4. 通过打开命令面板(⇧⌘P (Windows, Linux Ctrl+Shift+P))并运行 Python: Create Environment 命令来创建虚拟环境。

    注意:此步骤可能需要几分钟才能完成。

  5. 当询问环境类型时,选择 Venv

    Dropdown with "Venv" or "Conda" as options for environments that can be created with the Python: Create Environment command

  6. 然后选择您机器上可用的最新 Python 版本

    List of available global environments that can be used to create a virtual environment

  7. 从下拉列表中选择 requirements.txt 文件,以便自动安装依赖项,然后选择 OK

    Check box selected to install dependencies from requirements.txt file

虚拟环境将被创建,依赖项自动安装,并且为您的工作区选择该环境以供 Python 扩展使用。您可以通过检查 VS Code 的右下角来确认它已被选中

Environment in the Status bar

注意:如果您在状态栏上找不到新创建的环境信息,您可以单击 Python 解释器指示器(或从命令面板运行 Python: Select Interpreter 命令)并手动选择虚拟环境。

开始编码

让我们创建应用程序!

  1. 通过使用 文件 > 新建文件… 然后选择 Python 文件 来创建新的 Python 文件。

  2. 将其保存为 main.py⇧⌘S (Windows, Linux Ctrl+Shift+S))在 groceries-plugin 文件夹中。

  3. 将以下代码添加到 main.py 并保存文件

    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.get("/")
    def root():
        return {"message": "Hello World"}
    
  4. 通过启动调试器(F5)来运行代码。

  5. 从下拉菜单中,从列表中选择 FastAPI 配置选项

    Dropdown with debugger configuration options, with FastAPI being highlighted

    这会自动创建一个调试配置,该配置调用 uvicorn 通过调试器启动应用程序服务器,并允许您单步执行源代码以检查其行为。您应该在终端中看到以下内容

    Uvicorn server running message displayed in the terminal, with an URL to access the app

    提示:如果您的默认端口已被占用,请停止调试器并打开命令面板(⇧⌘P (Windows, Linux Ctrl+Shift+P)),搜索 Debug: Add Configuration,选择 Python Debugger,然后选择 FastAPI。这将创建一个自定义配置文件 .vscode/launch.json,您可以对其进行编辑。将以下内容添加到 "args":[] 以设置自定义端口:"--port=5000"。保存文件,然后使用(F5)重新启动调试器。

  6. Ctrl+Click 终端中的 http://127.0.0.1:8000/ URL 以在默认浏览器中打开该地址

    Hello World message displayed in the browser

    恭喜!您的 FastAPI 应用程序已启动并运行!

  7. 使用调试工具栏中的停止按钮或通过 ⇧F5 (Windows, Linux Shift+F5) 停止调试器。

为购物清单项创建模型

现在我们已经启动并运行了 FastAPI 应用程序,我们可以使用 Pydantic 来定义我们的购物清单项,Pydantic 是一个数据验证和解析库,可以与 FastAPI 无缝集成。Pydantic 允许您使用带有 类型提示 的 Python 类来定义数据模型,以自动验证和解析 API 请求中传入的数据(称为“负载”)。

让我们为购物清单项创建一个模型。我们将使用 ItemPayload 模型来定义要添加到购物清单中的项的数据结构。此模型将有三个字段:item_iditem_namequantity

  1. 使用 文件 > 新建文件… 然后选择 Python 文件 创建一个新的 Python 文件。

  2. 将以下行添加到文件中,然后将其保存到 groceries-plugin 文件夹中,名称为 models.py⇧⌘S (Windows, Linux Ctrl+Shift+S))。

    from typing import Optional
    from pydantic import BaseModel
    
    class ItemPayload(BaseModel):
        item_id: Optional[int]
        item_name: str
        quantity: int
    

Pylance,VS Code 中 Python 的默认语言服务器,支持类型提示功能,这对于使用 Pydantic 模型和 FastAPI 非常有用。这是因为 Pylance 是基于 Pyright 构建的,Pyright 是一个 Python 的静态类型检查器,可以检测代码中的类型错误,以防止 bug 并提高代码质量。

以下三个步骤是可选的,但考虑到 FastAPI 广泛使用类型提示来提高代码可读性和验证,我们可以利用 Pylance 的类型检查功能来尽早发现错误

  1. 打开设置编辑器(⌘, (Windows, Linux Ctrl+,))。

  2. 搜索“python type checking mode”并将其设置为 basic 进行基本类型检查。Pylance 现在将显示诊断和警告以捕获简单的类型相关错误。或者,您可以将其设置为 strict 以强制执行更高级的 类型检查规则

    Python Analysis Type Checking Mode options (off, basic and strict) in Settings editor

  3. 接下来,搜索“Python inlay type hints”,并为变量类型函数返回类型启用内联提示

    Two Python Analysis Type Hints settings being enabled in the Settings editor: for Function Return Types and for Variable Types

创建路由

现在我们需要一个地方来存储购物清单项。为了简单起见,让我们从一个空字典开始。

  1. 首先,让我们导入示例所需的所有包。打开 main.py 文件并将第一行导入替换为以下行

    from fastapi import FastAPI, HTTPException
    
    from models import ItemPayload
    
  2. 现在在 app = FastAPI() 下面添加以下行

    grocery_list: dict[int, ItemPayload] = {}
    

    这将创建一个新的空字典,该字典接收 int 类型的键(作为项目 ID)和 ItemPayload 类型的值。

    我们现在将在 FastAPI 应用程序中定义路由。在 Web 应用程序的上下文中,路由就像将特定 URL 映射到处理它们的代码的路径。这些路由充当应用程序中不同功能的入口点。当客户端(例如 Web 浏览器或另一个程序)向我们的应用程序发送带有特定 URL 的请求时,FastAPI 根据 URL 将该请求路由到适当的函数(也称为路由处理程序或视图函数),并且该函数处理请求并生成响应。

    让我们继续定义路由以添加和检索单个项目,以及返回购物清单中的所有项目。

  3. main.py 文件的末尾添加以下路由

    # Route to add a item
    @app.post("/items/{item_name}/{quantity}")
    def add_item(item_name: str, quantity: int):
        if quantity <= 0:
            raise HTTPException(status_code=400, detail="Quantity must be greater than 0.")
        # if item already exists, we'll just add the quantity.
        # get all item names
        items_ids = {item.item_name: item.item_id if item.item_id is not None else 0 for item in grocery_list.values()}
        if item_name in items_ids.keys():
            # get index of item_name in item_ids, which is the item_id
            item_id = items_ids[item_name]
            grocery_list[item_id].quantity += quantity
    # otherwise, create a new item
        else:
            # generate an ID for the item based on the highest ID in the grocery_list
            item_id = max(grocery_list.keys()) + 1 if grocery_list else 0
            grocery_list[item_id] = ItemPayload(
                item_id=item_id, item_name=item_name, quantity=quantity
            )
    
        return {"item": grocery_list[item_id]}
    

    如果您在上一节中启用了类型提示,您可能会注意到 Pylance 会添加带有函数返回类型以及 item_idsitem_id 类型的内联提示。您可以选择双击每个建议将其插入到代码中

    Inlay function return and variable type hints being displayed by Pylance throughout the sample code

    现在让我们检查此路由是否按预期工作。最快的方法是同时使用 VS Code 的调试器和 FastAPI 的 /docs 端点,该端点提供有关所有可用 API 路由的信息,并允许您与 API 交互以探索其参数和响应。此文档是根据 FastAPI 应用程序中定义的元数据和类型提示动态生成的。

  4. 通过单击行号的左侧边距(或 F9)在 if quantity <= 0 语句旁边添加一个断点。调试器将在该行执行之前停止,以便您可以逐行检查代码。

    Breakpoint set next to the first line in the add_item function

  5. 启动调试器(F5),然后导航到浏览器中的 http://127.0.0.1:8000/docs

    应该有一个 Swagger 界面,其中包含应用程序中可用的两个端点:/items 和根 (/)。

    Swagger UI displaying two endpoints: /items and /

  6. 选择 /items 路由旁边的向下箭头以展开它,然后选择右侧出现的 Try it out 按钮。

    "Try it out" button displayed next to the /items route in the Swagger UI

  7. 通过向 item_name 字段传递字符串并向 quantity 传递数字来添加购物清单项。例如,您可以提供 apple 作为 item_name,2 作为 quantity

  8. 选择 Execute

    Execute button displayed below the /items route

  9. 再次打开 VS Code 并注意调试器已在您之前设置的断点处停止。

    Debugger stopped at the breakpoint set in the add_item function

    在左侧,在此点定义的所有局部和全局变量都显示在“变量”窗口中,位于“运行和调试”视图下。在我们的示例中,item_name 在局部变量视图下设置为“apple”,quantity 设置为 2,并且在全局变量视图下有一个空的 grocery_list 字典。

    Variables window displayed in the Run and Debug view, with the item and grocery_list variables highlighted

    现在让我们使用 VS Code 的调试控制台进行一些探索。

  10. 选择 quantity <= 0 语句,右键单击编辑器并选择 在调试控制台中评估

    Evaluate in Debug Console option displayed in the context menu when right-clicking on a line of code

    这会打开调试控制台并运行选定的表达式。正如我们示例中预期的那样,表达式计算为 False

    调试控制台是一个强大的工具,可以快速测试表达式并更好地了解断点处代码的状态。您还可以使用它来运行任意代码,例如调用函数或打印变量。您可以在 Python 教程 中了解有关 VS Code 中 Python 调试的更多信息。

    您现在可以通过在调试视图工具栏中选择继续,或按 F5 来继续执行代码。

    最后,让我们为应用程序添加剩余的路由,以便我们可以列出所有项目或特定项目,以及从我们的购物清单中删除它们。您可以让调试器继续运行,因为它会在您在下一步中保存更改时自动重新加载应用程序。

  11. main.py 中的内容替换为以下代码

    from fastapi import FastAPI, HTTPException
    
    from models import ItemPayload
    
    app = FastAPI()
    
    grocery_list: dict[int, ItemPayload] = {}
    
    # Route to add an item
    @app.post("/items/{item_name}/{quantity}")
    def add_item(item_name: str, quantity: int) -> dict[str, ItemPayload]:
        if quantity <= 0:
            raise HTTPException(status_code=400, detail="Quantity must be greater than 0.")
        # if item already exists, we'll just add the quantity.
        # get all item names
        items_ids: dict[str, int] = {
            item.item_name: item.item_id if item.item_id is not None else 0
            for item in grocery_list.values()
        }
        if item_name in items_ids.keys():
            # get index of item_name in item_ids, which is the item_id
            item_id: int = items_ids[item_name]
            grocery_list[item_id].quantity += quantity
        # otherwise, create a new item
        else:
            # generate an ID for the item based on the highest ID in the grocery_list
            item_id: int = max(grocery_list.keys()) + 1 if grocery_list else 0
            grocery_list[item_id] = ItemPayload(
                item_id=item_id, item_name=item_name, quantity=quantity
            )
    
        return {"item": grocery_list[item_id]}
    
    
    # Route to list a specific item by ID
    @app.get("/items/{item_id}")
    def list_item(item_id: int) -> dict[str, ItemPayload]:
        if item_id not in grocery_list:
            raise HTTPException(status_code=404, detail="Item not found.")
        return {"item": grocery_list[item_id]}
    
    
    # Route to list all items
    @app.get("/items")
    def list_items() -> dict[str, dict[int, ItemPayload]]:
        return {"items": grocery_list}
    
    
    # Route to delete a specific item by ID
    @app.delete("/items/{item_id}")
    def delete_item(item_id: int) -> dict[str, str]:
        if item_id not in grocery_list:
            raise HTTPException(status_code=404, detail="Item not found.")
        del grocery_list[item_id]
        return {"result": "Item deleted."}
    
    
    # Route to remove some quantity of a specific item by ID
    @app.delete("/items/{item_id}/{quantity}")
    def remove_quantity(item_id: int, quantity: int) -> dict[str, str]:
        if item_id not in grocery_list:
            raise HTTPException(status_code=404, detail="Item not found.")
        # if quantity to be removed is higher or equal to item's quantity, delete the item
        if grocery_list[item_id].quantity <= quantity:
            del grocery_list[item_id]
            return {"result": "Item deleted."}
        else:
            grocery_list[item_id].quantity -= quantity
        return {"result": f"{quantity} items removed."}
    
    
  12. 保存文件(⌘S (Windows, Linux Ctrl+S))。应用程序应该自动重新加载。

您现在可以再次打开 /docs 页面并测试新路由,使用调试器和调试控制台更好地理解代码执行。完成后,您可以停止调试器(⇧F5 (Windows, Linux Shift+F5))。您还可以通过单击我们步骤 4 中添加的断点来将其删除。

恭喜!您现在拥有一个工作的 FastAPI 应用程序,其中包含用于添加、列出和删除购物清单中的项目的路由。

设置数据存储

至此,您已经拥有一个具有基本功能的应用程序的工作版本。本节将指导您设置数据存储以实现持久性,但如果您对已经学到的内容感到满意,则可以选择跳过此部分。

到目前为止,我们将数据存储在字典中,这并不理想,因为当应用程序重新启动时,所有数据都将丢失。

为了持久化数据,我们将使用 Redis,它是一个开源内存数据结构存储。由于其速度和多功能性,Redis 通常用作各种应用程序中的数据存储系统,包括 Web 应用程序、实时分析系统、缓存层、本教程等等。

如果您已经在 GitHub Codespaces 中使用我们现有的模板工作,您可以直接跳到 替换数据库 部分。

如果您在 Windows 上,可以通过设置 Docker 容器GitHub Codespace 来使用 Redis。在本教程中,我们将使用 Docker 容器,但您可以参考上面的部分,了解如何设置 GitHub Codespace 的说明。

否则,如果您在 Linux 或 macOS 机器上,您可以按照 其网站上的说明 安装 Redis,然后跳到 替换数据库 部分。

在 Windows 上设置 Docker 容器

VS Code 开发容器 扩展提供了一种简化方法,可以将您的项目、其依赖项和所有必要的工具整合到一个整洁的容器中,从而创建一个功能齐全的开发环境。该扩展允许您在 VS Code 中在容器内部(或挂载到容器中)打开您的项目,您将在其中拥有其完整的功能集。

对于以下步骤,请确保您的机器上安装了以下要求

要求

创建开发容器配置

  1. 打开命令面板并运行 Dev Containers: Add Dev Container Configuration Files…

  2. 选择 Python 3

    Python 3 option selected in the Dev Containers configuration files list

  3. 选择默认版本。

  4. 选择 Redis Server 作为要安装的附加功能,按 OK,然后选择 Keep Defaults

    我们可以选择安装要包含在容器中的 功能。对于本教程,我们将安装 Redis Server,这是一个社区贡献的功能,用于安装和添加 Redis 的正确开发容器设置。

    Redis Server option selected in the Dev Containers configuration files list

    这会在您的工作区中创建一个 .devcontainer 文件夹,其中包含一个 devcontainer.json 文件。让我们对此文件进行一些编辑,以便容器设置包括安装我们需要的 VS Code 扩展以及项目依赖项等步骤。

  5. 打开 devcontainer.json 文件。

  6. "features" : { ... } 条目之后添加一个“,”,以便我们可以向文件中添加更多设置。

    接下来,我们将把必要的依赖项安装命令添加到 devcontainer.json 文件中的 postCreateCommand 属性中,以便一旦容器设置好,我们的应用程序就可以运行。

  7. 找到以下内容并删除该行中的注释 (//),以便在创建容器后可以安装依赖项

    "postCreateCommand": "pip3 install --user -r requirements.txt",
    

    您可以在 开发容器规范 中了解有关 postCreateCommand 和更多生命周期脚本的信息。

    现在我们将使用 customizations 属性来添加我们想要在容器中安装的 VS Code 扩展。

  8. 将以下设置添加到 devcontainer.json

        // Use 'postCreateCommand' to run commands after the container is created.
        "postCreateCommand": "pip3 install --user -r requirements.txt",
    
        // Configure tool-specific properties.
        "customizations": {
            "vscode": {
                "extensions": [
                    "ms-python.python", //Python extension ID
                    "ms-python.vscode-pylance" //Pylance extension ID
                ]
            }
        }
    
  9. 保存文件。

  10. 从右下角显示的通知中选择 在容器中重新打开,或从命令面板运行 Dev Containers: Reopen in Container 命令。

    注意:构建容器可能需要几分钟,具体取决于互联网速度和机器性能。

    您可以在 开发容器文档 中了解有关开发容器配置的更多信息。

完成后,您将拥有一个完全配置的基于 Linux 的工作区,其中安装了 Python 3 和 Redis Server。

容器设置完成后,您会在 VS Code 的左下角看到一个指示器

Dev Containers indicator displayed on the bottom left corner of VS Code

注意:通过打开扩展视图(⇧⌘X (Windows, Linux Ctrl+Shift+X))并搜索它们,仔细检查 Python 和 Pylance 扩展是否已成功安装在容器中。如果没有,您可以通过运行 在开发容器中安装 来安装它们。

所选 Python 解释器信息显示在右下角的状态栏上,与 devcontainer.json 文件中指定的版本匹配

Python interpreter selection

注意:如果您在状态栏上找不到 Python 解释器信息,您可以单击 Python 解释器指示器(或从命令面板运行 Python: Select Interpreter 命令)并手动选择容器中的 Python 解释器。

我们现在准备好进入下一节,我们将替换数据存储。

替换数据库

我们有一个存储购物清单项的字典,但我们想用 Redis 数据库替换它。在本教程中,我们将使用 Redis 哈希来存储数据,它是一种可以存储多个键值对的数据结构。

与传统的数据库不同,在传统数据库中,您可以在不知道其 ID 的情况下检索项目,而您需要知道 Redis 哈希键才能从中检索值。在本教程中,我们将创建一个名为 item_name_to_id 的哈希以按名称检索项目,并将它们映射到其 ID。此外,我们将创建其他哈希以按 ID 检索项目,将它们映射到其名称和数量。每个项目哈希都命名为 item_id:{item_id},并且有两个字段:item_namequantity

首先,让我们从用连接到 Redis 服务器的 Redis 客户端对象替换字典开始。

  1. main.py 文件中,将文件开头的 grocery_list: dict[int, ItemPayload] = {} 替换为以下行

    redis_client = redis.StrictRedis(host='0.0.0.0', port=6379, db=0, decode_responses=True)
    

    Pylance 将显示一条错误消息,因为 Redis 尚未导入。

  2. 将光标放在编辑器中的“redis”上,然后单击显示的灯泡(或 ⌘. (Windows, Linux Ctrl+.))。然后选择 添加 'import redis'

    Light bulb displayed next to the Redis variable, with the option to add the import statement

    提示:您可以通过在设置编辑器(⌘, (Windows, Linux Ctrl+,))中查找 自动导入补全 设置并启用它来设置 Pylance 自动添加导入。

    我们现在有一个 Redis 客户端对象,它连接到在本地主机 (host="0.0.0.0") 上运行并侦听端口 6379 (port=6379) 的 Redis 服务器。db 参数指定要使用的 Redis 数据库。Redis 支持多个数据库,在此代码中,我们将使用数据库 0,它是默认数据库。我们还传递 decode_responses=True 以便将响应解码为字符串(而不是字节)。

    让我们在第一个路由 add_item 中进行更多替换。我们可以直接从 Redis 哈希中获取该信息,而不是查看字典中的所有键来查找已提供的项目名称。

    我们将假设 item_name_to_id 哈希已经存在,将项目名称映射到它们的 ID(别担心,我们很快就会添加此代码!)。然后,我们可以通过调用 Redis 的 hget 方法获取我们在请求中收到的项目名称的 ID,如果请求的名称已存在于哈希中,它将返回项目 ID,如果不存在则返回 None

  3. 删除以下内容行

    items_ids = {item.item_name: item.item_id if item.item_id is not None else 0 for item in grocery_list.values()}
    

    并替换为

      item_id = redis_client.hget("item_name_to_id", item_name)
    

    请注意,Pylance 会因此更改而引发问题。这是因为 hget 方法返回 strNone(如果项目不存在)。但是,我们尚未替换的代码行期望 item_id 的类型为 int。让我们通过重命名 item_id 符号来解决此警告。

  4. item_id 重命名为 item_id_str

  5. 如果您启用了内联提示,Pylance 应该在 item_id_str 旁边显示变量类型提示。您可以选择双击接受它

    Variable type hint displayed next to the item_id_str variable

  6. 如果项目不存在,则 item_id_strNone。所以现在我们可以删除包含以下内容的行

    if item_name in items_ids.keys():
    

    并替换为

    if item_id_str is not None:
    

    现在我们有了作为字符串的项目 ID,我们需要将其转换为 int 并更新项目的数量。目前,我们的 Redis 哈希只将项目名称映射到它们的 ID。为了也将项目 ID 映射到它们的名称和数量,我们将为每个项目创建一个单独的 Redis 哈希,使用 "item_id:{item_id}" 作为我们的哈希名称,以便更容易地按 ID 检索。我们还将为每个哈希添加 item_namequantity 字段。

  7. 删除 if 块内的代码

    item_id: int = items_ids[item_name]
    grocery_list[item_id].quantity += quantity
    

    并添加以下内容,将 item_id 转换为 int,然后通过调用 Redis 的 hincrby 方法来增加项目的数量。此方法按请求中给定的数量 (quantity) 增加 "quantity" 字段的值

    item_id = int(item_id_str)
    redis_client.hincrby(f"item_id:{item_id}", "quantity", quantity)
    

    我们现在只需要替换项目不存在时(即 item_id_strNone 时)的代码。在这种情况下,我们生成一个新的 item_id,为该项目创建一个新的 Redis 哈希,然后添加提供的项目名称和数量。

    要生成新的 item_id,让我们使用 Redis 的 incr 方法,传入一个名为 "item_ids" 的新哈希。此哈希用于存储最后生成的 ID,因此我们每次创建新项目时都可以递增它,确保它们都具有唯一的 ID。

  8. 删除包含以下内容的行

    item_id: int = max(grocery_list.keys()) + 1 if grocery_list else 0
    

    并添加以下内容

    item_id: int = redis_client.incr("item_ids")
    

    当第一次使用 item_ids 键运行此 incr 调用时,Redis 会创建该键并将其映射到值 1。然后,每次后续运行都会将存储的值增加 1。

    现在我们将使用 hset 方法将项目添加到 Redis 哈希中,并通过提供字段(item_iditem_namequantity)和值(项目新创建的 ID 及其提供的名称和数量)的映射来完成。

  9. 删除包含以下内容的行

    grocery_list[item_id] = ItemPayload(
            item_id=item_id, item_name=item_name, quantity=quantity
        )
    

    并替换为以下内容

    redis_client.hset(
                f"item_id:{item_id}",
                mapping={
                    "item_id": item_id,
                    "item_name": item_name,
                    "quantity": quantity,
                })
    

    现在我们只需要通过设置我们在开头引用的哈希 item_name_to_id 来将新创建的 ID 映射到项目名称。

  10. 将此行添加到路由的末尾,在 else 块内

    redis_client.hset("item_name_to_id", item_name, item_id)
    
  11. 删除包含以下内容的行

    return {"item": grocery_list[item_id]}
    

    并替换为

    return {"item": ItemPayload(item_id=item_id, item_name=item_name, quantity=quantity)}
    
  12. 如果您愿意,可以尝试对其他路由进行类似的替换。否则,您可以直接将文件的全部内容替换为以下行

    import redis
    from fastapi import FastAPI, HTTPException
    
    from models import ItemPayload
    
    app = FastAPI()
    
    redis_client = redis.StrictRedis(host="0.0.0.0", port=6379, db=0, decode_responses=True)
    
    # Route to add an item
    @app.post("/items/{item_name}/{quantity}")
    def add_item(item_name: str, quantity: int) -> dict[str, ItemPayload]:
        if quantity <= 0:
            raise HTTPException(status_code=400, detail="Quantity must be greater than 0.")
    
        # Check if item already exists
        item_id_str: str | None = redis_client.hget("item_name_to_id", item_name)
    
        if item_id_str is not None:
            item_id = int(item_id_str)
            redis_client.hincrby(f"item_id:{item_id}", "quantity", quantity)
        else:
            # Generate an ID for the item
            item_id: int = redis_client.incr("item_ids")
            redis_client.hset(
                f"item_id:{item_id}",
                mapping={
                    "item_id": item_id,
                    "item_name": item_name,
                    "quantity": quantity,
                },
            )
            # Create a set so we can search by name too
            redis_client.hset("item_name_to_id", item_name, item_id)
    
        return {
            "item": ItemPayload(item_id=item_id, item_name=item_name, quantity=quantity)
        }
    
    
    # Route to list a specific item by ID but using Redis
    @app.get("/items/{item_id}")
    def list_item(item_id: int) -> dict[str, dict[str, str]]:
        if not redis_client.hexists(f"item_id:{item_id}", "item_id"):
            raise HTTPException(status_code=404, detail="Item not found.")
        else:
            return {"item": redis_client.hgetall(f"item_id:{item_id}")}
    
    
    @app.get("/items")
    def list_items() -> dict[str, list[ItemPayload]]:
        items: list[ItemPayload] = []
        stored_items: dict[str, str] = redis_client.hgetall("item_name_to_id")
    
        for name, id_str in stored_items.items():
            item_id: int = int(id_str)
    
            item_name_str: str | None = redis_client.hget(f"item_id:{item_id}", "item_name")
            if item_name_str is not None:
                item_name: str = item_name_str
            else:
                continue  # skip this item if it has no name
    
            item_quantity_str: str | None = redis_client.hget(
                f"item_id:{item_id}", "quantity"
            )
            if item_quantity_str is not None:
                item_quantity: int = int(item_quantity_str)
            else:
                item_quantity = 0
    
            items.append(
                ItemPayload(item_id=item_id, item_name=item_name, quantity=item_quantity)
            )
    
        return {"items": items}
    
    
    # Route to delete a specific item by ID but using Redis
    @app.delete("/items/{item_id}")
    def delete_item(item_id: int) -> dict[str, str]:
        if not redis_client.hexists(f"item_id:{item_id}", "item_id"):
            raise HTTPException(status_code=404, detail="Item not found.")
        else:
            item_name: str | None = redis_client.hget(f"item_id:{item_id}", "item_name")
            redis_client.hdel("item_name_to_id", f"{item_name}")
            redis_client.delete(f"item_id:{item_id}")
            return {"result": "Item deleted."}
    
    
    # Route to remove some quantity of a specific item by ID but using Redis
    @app.delete("/items/{item_id}/{quantity}")
    def remove_quantity(item_id: int, quantity: int) -> dict[str, str]:
        if not redis_client.hexists(f"item_id:{item_id}", "item_id"):
            raise HTTPException(status_code=404, detail="Item not found.")
    
        item_quantity: str | None = redis_client.hget(f"item_id:{item_id}", "quantity")
    
        # if quantity to be removed is higher or equal to item's quantity, delete the item
        if item_quantity is None:
            existing_quantity: int = 0
        else:
            existing_quantity: int = int(item_quantity)
        if existing_quantity <= quantity:
            item_name: str | None = redis_client.hget(f"item_id:{item_id}", "item_name")
            redis_client.hdel("item_name_to_id", f"{item_name}")
            redis_client.delete(f"item_id:{item_id}")
            return {"result": "Item deleted."}
        else:
            redis_client.hincrby(f"item_id:{item_id}", "quantity", -quantity)
            return {"result": f"{quantity} items removed."}
    
    
  13. 重新运行调试器,通过与 /docs 路由交互来测试此应用程序。完成后可以停止调试器。

恭喜!您现在拥有一个运行正常的 FastAPI 应用程序,其中包含用于添加、列出和删除购物清单中的项目的路由,并且数据持久化在 Redis 数据库中。

可选:设置数据库删除

现在数据由 Redis 持久化,您可能希望创建一个脚本来擦除所有测试数据。为此,创建一个名为 flushdb.py 的新文件,其中包含以下内容

import redis

redis_client = redis.StrictRedis(host='0.0.0.0', port=6379, db=0, decode_responses=True)
redis_client.flushdb()

然后,当您想重置数据库时,您可以在 VS Code 中打开 flushdb.py 文件并选择编辑器右上角的运行按钮,或者从命令面板运行 Python: Run Python File in Terminal 命令。

请注意,这应该谨慎操作,因为它将删除当前数据库中的所有键,如果在生产环境中操作,可能会导致数据丢失。

可选:创建 ChatGPT 插件

使用 GitHub Codespaces,您可以使用 ChatGPT 插件 来托管您的应用程序进行测试。ChatGPT 插件是使 ChatGPT 能够与现有 API 交互以增强 ChatGPT 能力的工具,使其能够执行各种操作。ChatGPT 插件目前尚未公开提供,但您可以加入其 候补名单 以获得访问权限。一旦您获得访问权限,您可以按照下面的直播录制来创建您自己的 ChatGPT 购物清单插件

注意:所有 GitHub.com 个人帐户都包含在免费或专业计划中的 GitHub Codespaces 每月免费使用配额。有关详细信息,请访问 关于 GitHub Codespaces 的计费

后续步骤

感谢您跟随本教程!我们希望您学到了关于 FastAPI 以及如何在 VS Code 中使用它的新知识。

本教程中完成的代码项目可在 GitHub 上找到:python-sample-vscode-fastapi-tutorial

官方文档 中了解更多关于 FastAPI 的信息。

要在生产网站上尝试该应用程序,请查阅教程 使用 Docker 容器将 Python 应用程序部署到 Azure 应用服务

您还可以查看这些其他 VS Code Python 文章