reflex-dev / reflex

🕸️ Web apps in pure Python 🐍
https://reflex.dev
Apache License 2.0
19.05k stars 1.08k forks source link

How can we run reflex app programatically in python code? #3665

Open bglgwyng opened 2 months ago

bglgwyng commented 2 months ago

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?

abulvenz commented 1 month ago

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.

dentro-innovation commented 1 month ago

Not sure what the use case for such functionality would be

Lendemor commented 1 month ago

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

TimChild commented 1 month ago

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.

bglgwyng commented 1 month ago

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 commented 1 month ago

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.

itsmeadarsh2008 commented 1 month ago

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