-
-
Notifications
You must be signed in to change notification settings - Fork 521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Panel App in FastAPI Router #7514
Comments
This issue has been mentioned on HoloViz Discourse. There might be relevant details there: https://discourse.holoviz.org/t/panel-app-in-fastapi-router/8414/3 |
I'm not a regular fastapi user. Can you explain why you need it to be within the |
If you have multiple, related items but want to keep separation of code then use of routers is a convenient way to go. Say you have (for a particular subject matter) prototype/research endpoints, development endpoints and production endpoints. Maybe you'd place them all on separate actual FastAPI services, probably even on separate machines/VMs. In which case switch the above out for several subject matters hosted on the (for example) development method service API. That means any particular subject matter (for latter example) or a stage (proto/dev/production for former example) can be adjusted with complete independence from all others. It also avoids too much code residing in the main.py (or wherever module the "app = FastAPI()" line would go into) If your a flask user (?), then consider it akin to blueprints. Pretty sensible to use that, or an equivalent, when doing anything of substantial size. That help? [As said in original post, being able to structure the source code in separate routers, even if the actual url isn't "routered" is a good step in allowing good code structuring. Just it'd be even better if it were able to do the full thing and the fix was something simple that I wasn't grasping.] |
I'm currently travelling so I haven't worked through all the details but broadly I agree we should enable this use case. The initial work will likely have to happen in bokeh-fastapi and then we have to if there's anything we need to change in Panel. |
Just for more related/unrelated context to this issue. ChatGPT 4o hallucinates and provides using ApiRouter as the recommended solution to the prompt “Create an example Python panel application within fastapi to also have api endpoints.” https://chatgpt.com/share/67444906-5460-8008-8788-5800c329095d I experienced this several weeks ago, and because I typically have additional complexity such as OAuth in my panel app, I tabled my questions about FastApi for the future. And, was just considering other options such as different port and process for the FastApi endpoints to run independent of panel. |
This may not be news to those more familiar with the codebase, but just in case. Modifying the baseline (main.py)
as per:
Will return a valid view on a localhost port. |
... and a bit further. Within main.py, I've defined the missing .js files using StaticFiles import - note the app.mount.
needs to reconcile with:
Then had to put a load of .js files into /static/ folder alongside the main.py script - these are detailed in point 2. Notes: (2) plenty more javascripts were deemed missing, behaviour did differ depending on where prefix was declared. My tree eventually ended up looking like:
That has me up and running. At least as far as an APIRouter is concerned. I suspect the much more elegant solution than my hacking may reside somewhere around what is getting passed to ComponentResourceHandler at lines 141-143 at panel.io.fastapi.py Final main.py code (still need the changes to BokehFastAPI)
|
[Originally posted here and raising as issue on recommendation from @ahuang11 ]
Hi all,
Was trying Running Panel apps in FASTAPI and have the example up and running. But in a more real world use case, I would need to run it within routers - even if just for source code structuring.
Taking Marc's example and extending it as below:
On initial running, there were complaints within the bokeh_fastapi.application.BokehFastAPI class because a Router instance has been supplied rather than a FastAPI app instance.
I then tried to dummy this module by modifying as code snip below (only top to tail of section that changed shown).
Basically I mapped the Router onto a dummy representation of a FastAPI app.
This worked... but only if prefix="/test_router" does not exist when specifying the app.include_router() line in main.py
While this in itself is useful - it does allow source code structuring - it would be neater if it did align with the router paths, so onward the investigation went.
When specifying the prefix, while the endpoint is visible on the openAPI /docs page, attempting to access the endpoint will return multiple failures to access .js files in paths:
/test_router/static/extensions/panel/.. ../es-module-shims.min.js
/test_router/static/js/bokeh.min.js
/test_router/static/js/bokeh-gl.min.js
/test_router/static/extensions/panel/panel.min.js
/test_router/static/js/bokeh-widgets.min.js
/test_router/static/js/bokeh-tables.min.js
Circled around it a few times now from different directions trying to specify StaticFiles and mount the app - but to be honest not really knowing what I'm doing its akin to pinning the tail on a donkey! 😄 So putting this infront of the experts who do actually understand the library and see what you all think.
bokeh_fastapi\application.py
The text was updated successfully, but these errors were encountered: