Skip to content

Add auto-reloading behavior to reactpy.run #990

Closed as not planned
Closed as not planned

Description

@rmorshea

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.

Activity

  1. Archmonger commented on May 24, 2023

    @Archmonger
    Contributor

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

  2. rmorshea commented on May 24, 2023

    @rmorshea
    CollaboratorAuthor

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

  3. Archmonger commented on May 24, 2023

    @Archmonger
    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.

  4. rmorshea commented on May 24, 2023

    @rmorshea
    CollaboratorAuthor

    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.

  5. Archmonger commented on Jun 14, 2023

    @Archmonger
    Contributor

    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.

  6. rmorshea commented on Jun 14, 2023

    @rmorshea
    CollaboratorAuthor

    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.

  7. Archmonger commented on Jun 14, 2023

    @Archmonger
    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.

  8. dridk commented on Sep 11, 2024

    @dridk

    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

  9. Archmonger commented on Jan 25, 2025

    @Archmonger
    Contributor

    Closed in favor of #1113

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority-3-lowMay be resolved one any timeline.release-minorWarrents a minor release

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions