Serving static files
Robyn has some powerful features for serving static files. However, there are a couple of issues/
Hidden Documentation
At the moment the docs for serving files are "hidden" in "File Uploads" https://robyn.tech/documentation/api_reference/file-uploads
Can we make this more visible by moving it to the top level of API reference (and preferably renaming it to something like "Serving Static Files")
I tried editing the docs but npm hates me ;-)
Simple default
[Update] this PR starts realm code and I think it is simpler/better https://github.com/sparckles/Robyn/pull/1024
As we can serve directories (for CSS, etc.), providing a default would save people a lot of reading.
I'd like us to add ROBYN_STATIC_FILE_DIRECTORY to the robyn.env file. If this is set then we automatically call app.serve_directory() to provide this static directory under the URL "/static/"
SubRouters
It would also seem sensible be able to add static directories to each SubRouter eg by passing a Bool argument to the function
frontend = SubRouter(name, prefix="/frontend", True) ## make /frontend/static/ serve files frontend = SubRouter(name, prefix="/frontend", False) ## No support for /frontend/static/ frontend = SubRouter(name, prefix="/frontend") ## Default to False ie No support for /frontend/static/
url_for
The flask version of url_for supports static files but only for a single directory.
I'm thinking we could have url_for('static', 'filename.css') to serve from the application-wide static directory
Then for the subrouters we have url_for('static', 'filename.css', subroute='RouterName')
This way we have minimal impact on our templates if we restructure the project.
See https://github.com/sparckles/Robyn/issues/996#issuecomment-2451516002
@VishnuSanal
I think that my plan in PR https://github.com/sparckles/Robyn/pull/1024 makes this even simpler.
Hey @dave42w 👋
[Update] this PR starts realm code and I think it is simpler/better https://github.com/sparckles/Robyn/pull/1024
As we can serve directories (for CSS, etc.), providing a default would save people a lot of reading.
I'd like us to add ROBYN_STATIC_FILE_DIRECTORY to the robyn.env file. If this is set then we automatically call app.serve_directory() to provide this static directory under the URL "/static/"
Why can't someone just create a static folder themself? Why do we need a new default?
Why can't someone just create a static folder themself? Why do we need a new default?
My PR updates this. The reason for it is a) to be simpler than serve_directory is at present and b) to work for subroutes as well
I'm suggesting:
app = Robyn(__file__)
app.add_static_files_directory() #using a sane default directory path and "static" as the route both customisable via arguments
@app.post("/crimes")
frontend = SubRouter(name, prefix="/frontend")
frontend.add_static_files_directory() #using a sane default directory path and "static" as the route both customisable via arguments
@frontend.get("/")
What I want to support from templates is
get_static_url("crimes.css")
get_static_url("frontend/fronts.css")
It would even support
get_static_url("frontend/crimes.css")
there could be a fallback so that if it doesn't find crimes.css in the subrouter static directory it looks in the main one. That could provide a nice fallback support.
Personally I prefer robyn's serve_directory which is very clear for the meaning, better than fastapi's mount, way much better than flask's url_for, didn't see a need for a default static folder to hold frontend resources, since static may means specific things like img fonts etc