AnswerDotAI / AnswerDotAI/fasthtml
[FEATURE] Add root_path parameter to serve()
- Dominant language
- Jupyter Notebook
- Stars
- 7k
- Forks
- 319
- Avg merge
- 28m
- Merged PRs (30d)
- 3
Description
**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](https://nbdev.fast.ai/tutorials/qmd_intro.html), and I would like to trigger `quarto render ` 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](https://github.com/encode/uvicorn/blob/fe3910083e3990695bc19c2ef671dd447262ae18/uvicorn/main.py#L494) that is added to the [server config](https://github.com/encode/uvicorn/blob/fe3910083e3990695bc19c2ef671dd447262ae18/uvicorn/main.py#L546), 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.
```python
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](https://github.com/AnswerDotAI/fasthtml/blob/9fab6eec28389ca8a00bb34b7b8b48a518ece93a/fasthtml/core.py#L598-L603) 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.
Contributor guide
Assessment
This issue has not been assessed yet.