@rollin
To pass a variable from a FastAPI route to another class, you can simply instantiate the class and pass the variable as an argument to a method or a constructor of the class. Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from fastapi import FastAPI from pydantic import BaseModel from typing import List app = FastAPI() class SomeClass: def do_something(self, variable): print(f"Received variable: {variable}") # Do something with the variable some_instance = SomeClass() class Item(BaseModel): name: str @app.post("/items/") async def create_item(item: Item): some_instance.do_something(item.name) return {"name": item.name} |
In this example, the /items/
route receives an Item
object, extracts the name
attribute, and passes it to the do_something
method of the SomeClass
instance. You can modify the example according to your requirements and structure of your code.