reflex icon indicating copy to clipboard operation
reflex copied to clipboard

How can we run reflex app programatically in python code?

Open bglgwyng opened this issue 1 year ago β€’ 7 comments

I'm looking for a way to run a Reflex app programmatically within Python code, rather than using the command line interface. I expect something like

app = rx.App(...)
app.run(port=8000)

I tried to use run function in reflex.py but I haven't made it yet. Do we have an official way to achieve it?

bglgwyng avatar Jul 12 '24 06:07 bglgwyng

AFAIK reflex should be considered more as framework not as a toolkit or library. Which means: Reflex calls you, not the other way round.

Some concepts might hinder you to "simply" start a server, e.g. state-instrumentation during inheritance, compilation of the frontend, ...

Do we have an official way to achieve it?

I guess no. Maybe you can also look into AppHarness if you really need to run the app programmatically.

Another suggestion: In case you want to run it from within another existing non-reflex project, maybe you can find another link but inclusion such as FastAPI Routes/coupling via DB/... to marry the two.

abulvenz avatar Jul 16 '24 09:07 abulvenz

Not sure what the use case for such functionality would be

dentroai avatar Jul 16 '24 10:07 dentroai

If anything, if you really need to launch Reflex from another script, using subprocess.run() would be the recommended way by the team.

Lendemor avatar Jul 16 '24 17:07 Lendemor

I do something like this when I want to be able to debug from pycharm.

from reflex.reflex import _run
if __name__ == "__main__":
    _run()

Obviously, not a good idea for any production case, but I'm not sure what your use case is.

TimChild avatar Jul 16 '24 20:07 TimChild

My use case is a bit weird. I need to distribute reflex app in installable format rather than python code thanks to an unconvincing policy. So I'm trying pyInstaller and need to make entry code in .py. Yes, I admit that nobody else is in the same situation.

However, I also belive that, in general sense, the requested functionality is expected to be provided by default.

bglgwyng avatar Jul 17 '24 13:07 bglgwyng

I do something like this when I want to be able to debug from pycharm.


from reflex.reflex import _run

if __name__ == "__main__":

    _run()

Obviously, not a good idea for any production case, but I'm not sure what your use case is.

I tried it but it didnt work. I'll try again! Thanks.

bglgwyng avatar Jul 17 '24 13:07 bglgwyng

My use case is a bit weird. I need to distribute reflex app in installable format rather than python code thanks to an unconvincing policy. So I'm trying pyInstaller and need to make entry code in .py. Yes, I admit that nobody else is in the same situation.

However, I also belive that, in general sense, the requested functionality is expected to be provided by default.

You may want to export the app. This command exports the app's both frontend and backend to zip files. You have to figure out how you can start the backend so you interact with it via your front end. https://reflex.dev/docs/api-reference/cli/#export

itsmeadarsh2008 avatar Aug 07 '24 12:08 itsmeadarsh2008

@bglgwyng Have you found the solution? I also want to export my reflex app using pyinstaller

CohPhol avatar Jan 13 '25 05:01 CohPhol

If it's only for testing or within your organization, then you can try this or that.

morags avatar Jan 26 '25 11:01 morags

there are many functions in reflex.py that basically "runs" the code, _run is the one you're looking for, as for

I tried it but it didnt work

i'm assuming what you are doing to be more difficult than just doing that but here are some clues:

  1. you want the frontend pre-compiled, no need to ship JS in some exe
  2. the backend can be run with any ASGI-runner, reflex uses granian at the moment, so you can do something like granian --factory app.app, or have any ASGI runner take app() and run it, you can set REFLEX_SKIP_COMPILE to not run the frontend compile

adhami3310 avatar Apr 30 '25 07:04 adhami3310

@adhami3310 This kind of breaks the support for building desktop apps in Reflex. When this functionality comes to Reflex, we can make desktop apps easily with Reflex (Theoretically and Practically Approved). You know that Python GUIs aren't that good in terms of modernization, Reflex could be a great framework to start making GUIs. It could be much bigger than just a web framework.

I had made a desktop app in Reflex, but it wasn't portable and redistributable due to lack of this feature.

Approach: Reflex currently separates frontend and backend. The front end can be integrated into the app (loading the front end or the static files) - which loads at the very first start of the app and runs run backend separately to allow the front end to connect with the backend. The problem is about the configuration of running the backend separately and loading the front end to the desktop app (pywebview). Also (the topic of running apps programmatically), could help set up an MCP server for Reflex in future.

itsmeadarsh2008 avatar May 02 '25 16:05 itsmeadarsh2008

it should be technically possible to do, but we're not going to add support for it, that means we will not promise you that such API remains that same in non breaking updates, but if you're dedicated enough you can maintain your integration

i get that this feature is wanted in the community, and i encourage people who want it to build solutions for it, but most likely these solutions will not be inside of the reflex repo, if you need help don't hesitate to reach out on discord

adhami3310 avatar May 02 '25 16:05 adhami3310