Skip to content

Add auto-reloading behavior to reactpy.run #990

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

Closed
rmorshea opened this issue May 24, 2023 · 9 comments
Closed

Add auto-reloading behavior to reactpy.run #990

rmorshea opened this issue May 24, 2023 · 9 comments
Labels
priority-3-low May be resolved one any timeline. release-minor Warrents a minor release

Comments

@rmorshea
Copy link
Collaborator

rmorshea commented May 24, 2023

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.

@rmorshea rmorshea added flag-triage Not prioritized. type-feature priority-3-low May be resolved one any timeline. release-minor Warrents a minor release and removed flag-triage Not prioritized. labels May 24, 2023
@Archmonger
Copy link
Contributor

I'd say it's easier to run every backend using Uvicorn, and rely on Uvicorn's auto reloader.

@rmorshea
Copy link
Collaborator Author

Not all supported backends are async (e.g. flask/tornado). All the async ones can use the Uvicorn reloader though.

@Archmonger
Copy link
Contributor

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.

@rmorshea
Copy link
Collaborator Author

rmorshea commented May 24, 2023

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.

@Archmonger
Copy link
Contributor

Archmonger commented Jun 14, 2023

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.

@rmorshea
Copy link
Collaborator Author

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.

@Archmonger
Copy link
Contributor

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.

@dridk
Copy link

dridk commented Sep 11, 2024

I have no idea how to do that ! Could you share a code ?

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

Fixed: All is here https://reactpy.dev/docs/guides/getting-started/running-reactpy.html?highlight=uvicorn

@Archmonger
Copy link
Contributor

Closed in favor of #1113

@Archmonger Archmonger closed this as not planned Won't fix, can't repro, duplicate, stale Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority-3-low May be resolved one any timeline. release-minor Warrents a minor release
Projects
None yet
Development

No branches or pull requests

3 participants