fasthtml
fasthtml copied to clipboard
[FEATURE] Add root_path parameter to serve()
Is your feature request related to a problem? Please describe.
I'm working on a project that merges the reusability of fasthtml components with the delightful development experience of .qmd files, and I would like to trigger quarto render <file path> whenever a change is registered for these files.
I'm currently doing this by monkey patching the uvicorn.supervisors.basereload.BaseReload class, but to ensure the image paths generated by quarto are correct, and the converted html is stored in the proper location, I need access to the root path for the project.
Describe the solution you'd like
uvicorn.run has a root_path parameter that is added to the server config, which makes it accessible to instances of BaseReload, so all I need is to be able to pass the project's root to serve.
I'm currently doing this by copying the the serve function from fasthtml's source code and adding the parameter, which totally works...but passing the root_path to uvicorn's objects makes it much easier to extend and customize a webserver project! Alternatively, **kwargs could work since uvicorn.run has a bunch of parameters.
Example code
At the moment I've copied the serve function into my project and added the parameter.
def serve(
appname=None,
app='app',
host='0.0.0.0',
port=None,
reload=True,
reload_includes:list[str]|str|None=None,
reload_excludes:list[str]|str|None=None,
root_path:str="", # <-------- Added parameter here
):
"Run the app in an async server, with live reload set as the default."
bk = inspect.currentframe().f_back
glb = bk.f_globals
code = bk.f_code
if not appname:
if glb.get('__name__')=='__main__': appname = Path(glb.get('__file__', '')).stem
elif code.co_name=='main' and bk.f_back.f_globals.get('__name__')=='__main__': appname = inspect.getmodule(bk).__name__
if appname:
if not port: port=int(os.getenv("PORT", default=5001))
uvicorn.run(
f'{appname}:{app}',
host=host,
port=port,
reload=reload,
reload_includes=reload_includes,
reload_excludes=reload_excludes,
root_path=root_path, # <------- Added argument here
)
Problem solved This change improves customization of the webserver, especially for development!
Additional Context
This is admittedly a small feature request, especially since the serve function is only a handful of lines, but I trust ya'll to be much smarter than me with spinning up servers, so I'd like to use the current (and any future) setup code implemented prior to calling .run, without needing to copy directly from the source code.
Given how simple this appears to be I'm more than happy to open a PR for this, but idk if there's a reason for why the parameters have been limited for the serve function or if ya'll have a design stance on the use of kwargs, though a quick project search shows 31 files with a kwargs reference so maybe nvm on that 😆
Confirmation Please confirm the following:
- [x] I have checked the existing issues and pull requests to ensure this feature hasn't been requested before.
- [x] I have read the project's documentation to ensure this feature doesn't already exist.