Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23247

FastAPI: routing submodul functions to root

$
0
0

I have the following in my main.py

from fastapi import FastAPI, Requestfrom fastapi.responses import HTMLResponsefrom fastapi.middleware.gzip import GZipMiddlewarefrom fastapi.templating import Jinja2Templatesfrom fastapi.staticfiles import StaticFilesfrom my_app.web.views import defaultapp = FastAPI()app.add_middleware(GZipMiddleware, minimum_size=500)app.include_router(default.router)#app.include_router(data_mgr_view.router)app.mount("/static", StaticFiles(directory="static"), name="static")app.mount("/templates", Jinja2Templates(directory="templates"), name="templates")@app.get('/test')async def test():    return "test successful"

This works fine. i can hit the url at localhost:5000/test and it returns expected string.Now i have this file, 'default.py' whose handlers i want to be based off root:

import sys, tracebackimport pandas as pdfrom fastapi import APIRouter, Request, Queryfrom my_app.web.models.response_data import ResponseDatafrom my_app.data.providers.internals_provider import MktInternalsProviderfrom my_app.config import tmpltsrouter = APIRouter(prefix="")@router.route('/')async def root(request: Request):    context = {"title":"Playground", "content":f"Place for my charts, studies, etc..."}    return tmplts.TemplateResponse(request=request, name="index.html", context=context)@router.route('/test2')async def test2():    return "test2 success"

The first method works fine when i hit: localhost:5000/

The 2nd method throws a exception when i hit: localhost:5000/test2

Traceback (most recent call last):  File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 411, in run_asgi    result = await app(  # type: ignore[func-returns-value]             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 69, in __call__    return await self.app(scope, receive, send)           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__    await super().__call__(scope, receive, send)  File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 123, in __call__    await self.middleware_stack(scope, receive, send)  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 186, in __call__    raise exc  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 164, in __call__    await self.app(scope, receive, _send)  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/gzip.py", line 24, in __call__    await responder(scope, receive, send)  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/gzip.py", line 44, in __call__    await self.app(scope, receive, self.send_with_gzip)  File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 65, in __call__    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)  File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app    raise exc  File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app    await app(scope, receive, sender)  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 756, in __call__    await self.middleware_stack(scope, receive, send)  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 776, in app    await route.handle(scope, receive, send)  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 297, in handle    await self.app(scope, receive, send)  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 77, in app    await wrap_app_handling_exceptions(app, request)(scope, receive, send)  File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app    raise exc  File "/usr/local/lib/python3.11/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app    await app(scope, receive, sender)  File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 72, in app    response = await func(request)                     ^^^^^^^^^^^^^TypeError: test2() takes 0 positional arguments but 1 was given

any help appreciated


Viewing all articles
Browse latest Browse all 23247

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>