Questions tagged [starlette]

Questions about Starlette (a lightweight python ASGI framework/toolkit).

Starlette is a lightweight ASGI framework/toolkit, which is ideal for building high performance asyncio services.

https://www.starlette.io

259 questions
11
votes
2 answers

Starlette/FastApi route path components with forward slash

I have defined a route in Starlette/FastApi - @router.post("/{part}") def post_method(part): return "ok" @router.post("/{part}/{another_part}") def another_post_method(part, another_part): return "ok" I have some forward slash in the path…
hangc
  • 4,730
  • 10
  • 33
  • 66
11
votes
3 answers

Return multiple files from fastapi

Using fastapi, I can't figure out how to send multiple files as a response. For example, to send a single file, I'll use something like this from fastapi import FastAPI, Response app = FastAPI() @app.get("/image_from_id/") async def…
Hooked
  • 84,485
  • 43
  • 192
  • 261
11
votes
1 answer

FastAPI - mocking path function has no effect

I've got a simple FastAPI application and I'm trying to create tests with pytest for it. My goal is to test how app behaves in case of different errors. I've got a simple healthcheck route in my app: from fastapi import APIRouter router =…
umat
  • 607
  • 1
  • 13
  • 25
10
votes
7 answers

Get starlette request body in the middleware context

I have such middleware class RequestContext(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next: RequestResponseEndpoint): request_id = request_ctx.set(str(uuid4())) # generate uuid to request body = await…
sashaaero
  • 2,618
  • 5
  • 23
  • 41
10
votes
0 answers

RuntimeError: unable to perform operation on TCPTransport the handler is closed

I'm trying to make an HTTP benchmark test on my local machine with autocannon tool to my fresh, brand-new project setup that I've built on FastAPI + uvicorn. But at some point or randomly (I don't know what's happening) I'm having an ASGI…
yigidix
  • 123
  • 6
9
votes
1 answer

FastAPI - How to get app instance inside a router?

I want to get the app instance in my router file, what should I do ? My main.py is as follows: # ... app = FastAPI() app.machine_learning_model = joblib.load(some_path) app.include_router(some_router) # ... Now I want to use…
DachuanZhao
  • 1,181
  • 3
  • 15
  • 34
9
votes
2 answers

fastapi (starlette) RedirectResponse redirect to post instead get method

I have encountered strange redirect behaviour after returning a RedirectResponse object events.py router = APIRouter() @router.post('/create', response_model=EventBase) async def event_create( request: Request, user_id: str =…
Jekson
  • 2,892
  • 8
  • 44
  • 79
8
votes
1 answer

How to share variables between HTTP requests in FastAPI?

How can I share the value of variables between HTTP requests in FastAPI? For instance, I have a POST request in which I get some audio files and then I convert their info into a Pandas Dataframe. I would like to send that Dataframe in a GET request,…
0x55b1E06FF
  • 538
  • 1
  • 9
  • 24
8
votes
1 answer

FastAPI websocket ping/pong timeout

I am using FastAPI with @app.websocket to listen for incoming websockets. How does FastAPI (or Starlette or Uvicorn underneath) do ping/pong heartbeats? Is this configurable? I cannot find it in the documentation at all. from fastapi import FastAPI,…
Jonathan
  • 1,864
  • 17
  • 26
8
votes
0 answers

Excessive File Handles (on lsof) When Python logging with Starlette and Uvicorn

I have a minimal starlette app running via uvicorn doing nothing but returning a ping. Strangely, even with a single thread, I have 45 file handles to my log file. The handles increase with hits to the app and plateau at 45. I'm counting file…
CoderOfTheNight
  • 944
  • 2
  • 8
  • 21
8
votes
1 answer

Uvicorn server shutting down unexpectedly

I'm working with FastAPI framework, served by Uvicorn server. My application should run some time consuming numerical computation at a given endpoint (/run). For this I am using 'background_task' from fastAPI (which is basically 'background_task'…
Davide Melli
  • 151
  • 1
  • 9
7
votes
1 answer

How to Upload a large File (≥3GB) to FastAPI backend?

I am trying to upload a large file (≥3GB) to my FastAPI server, without loading the entire file into memory, as my server has only 2GB of free memory. Server side: async def uploadfiles(upload_file: UploadFile = File(...): Client side: m =…
7
votes
2 answers

FastAPI - adding route prefix to TestClient

I have a FastAPI app with a route prefix as /api/v1. When I run the test it throws 404. I see this is because the TestClient is not able to find the route at /ping, and works perfectly when the route in the test case is changed to /api/v1/ping. Is…
Shod
  • 801
  • 3
  • 12
  • 33
7
votes
0 answers

Starlette session state clearing

some strange behaviour I've found when trying to clear Starlette sessions state (using starlette.middleware.sessions.SessionMiddleware) while working through some OAuth/0Auth authentication examples. In Flask, the following session clearing code on…
Dmytro Bugayev
  • 606
  • 9
  • 13
7
votes
1 answer

Websockets bridge for audio stream in FastAPI

Objective My objective is to consume an audio stream. Logically, this is my objective: Audio stream comes through WebSocket A (FastAPI endpoint) Audio stream is bridged to a different WebSocket, B, which will return a JSON (Rev-ai's WebSocket) Json…
rmssoares
  • 175
  • 1
  • 11
1
2
3
17 18