reactpy
reactpy copied to clipboard
Add auto-reloading behavior to `reactpy.run`
Current Situation
Currently, if you want to get auto-reloading you need to configure an application and run it using a production-grade web server like Uvicorn and follow their directions on how to set up auto-reloading. It would be great if the reactpy.run function did that by default.
Proposed Actions
First, decide if we should pass a new DebugOptions object to the run function or simply use **kwargs. The former aligns with the configure interface, but the latter may help delineate between the two.
Add reload=True settings for the run function as well as BackendImplementation.create_development_app. It's possible that a reload_dirs parameter could make sense here as well, but if the interfaces for specifying what directories to watch varies significantly between the backends we might choose not to do that. It may also be worth adding a
Then, it's just a question of making this happen for each backend development server. Since the all the async frameworks run on Uvicorn, that should be fairly straightforward, for Flask/Tornado that will require some investigation.
I'd say it's easier to run every backend using Uvicorn, and rely on Uvicorn's auto reloader.
Not all supported backends are async (e.g. flask/tornado). All the async ones can use the Uvicorn reloader though.
asgiref contains a WsgiToAsgi converter. It would technically be less performant than a true WSGI server, but our run utility is for debug purposes only anyways.
I have a vague memory of having tried WsgiToAsgi and that the websocket implementation I used in Flask was problematic. The Flask websocket implementation changed at some point though so it may be worth looking into. I know it still uses gevent though so there may be some conflicts with the fact that WsgiToAsgi runs the WSGI server in a thread.
uvicorn auto reloading does not work when using uvicorn's Python API.
Unfortunately it only works through command line. Or more accurately, uvicorn.Config(app=...) needs to be a string.
There are two problems we'd need to solve:
- Figure out the name of the entrypoint file
- Somehow be able to point to an ASGI application that lives in that file
If you can solve that, you can write the app string ("the_entrypoint:the_app").
I wonder if if would be possible to solve both those problems with some frame trickery:
from inspect import currentframe
def run():
frame = currentframe().f_back
app = frame.f_globals["the_app"] = create_development_app() # patch in the application to __main__
entrypoint = frame.f_globals["__file__"] # get the path to the entrypoint file
if frame.f_globals["__name__"] == "__main__":
serve_development_app(app, entrypoint)
This is very hand-wavy, but hopefully it gets the idea across.
Yep, I have a simpler solution that might involve changing our BackendImplementation protocol.
I don't want to overload #1051 with that. Will do in a follow up PR.