How can we run reflex app programatically in python code?
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?
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.
Not sure what the use case for such functionality would be
If anything, if you really need to launch Reflex from another script, using subprocess.run() would be the recommended way by the team.
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.
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.
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.
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
@bglgwyng Have you found the solution? I also want to export my reflex app using pyinstaller
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:
- you want the frontend pre-compiled, no need to ship JS in some exe
- 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 takeapp()and run it, you can setREFLEX_SKIP_COMPILEto not run the frontend compile
@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.
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