Skip to content

Commit 146bd67

Browse files
committed
use filename instead of module
1 parent f729d89 commit 146bd67

1 file changed

Lines changed: 44 additions & 6 deletions

File tree

reflex/utils/exec.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,47 @@ def get_app_module():
217217
Returns:
218218
The app module for the backend.
219219
"""
220-
config = get_config()
220+
return get_config().module
221+
222+
223+
def get_app_instance():
224+
"""Get the app module for the backend.
225+
226+
Returns:
227+
The app module for the backend.
228+
"""
229+
return f"{get_app_module()}:{constants.CompileVars.APP}"
230+
231+
232+
def get_app_file() -> Path:
233+
"""Get the app file for the backend.
234+
235+
Returns:
236+
The app file for the backend.
237+
238+
Raises:
239+
ImportError: If the app module is not found.
240+
"""
241+
module_spec = importlib.util.find_spec(get_app_module())
242+
if module_spec is None:
243+
raise ImportError(
244+
f"Module {get_app_module()} not found. Make sure the module is installed."
245+
)
246+
file_name = module_spec.origin
247+
if file_name is None:
248+
raise ImportError(
249+
f"Module {get_app_module()} not found. Make sure the module is installed."
250+
)
251+
return Path(file_name).resolve()
252+
221253

222-
return f"{config.module}:{constants.CompileVars.APP}"
254+
def get_app_instance_from_file() -> str:
255+
"""Get the app module for the backend.
256+
257+
Returns:
258+
The app module for the backend.
259+
"""
260+
return f"{get_app_file()}:{constants.CompileVars.APP}"
223261

224262

225263
def run_backend(
@@ -321,7 +359,7 @@ def run_uvicorn_backend(host: str, port: int, loglevel: LogLevel):
321359
import uvicorn
322360

323361
uvicorn.run(
324-
app=f"{get_app_module()}",
362+
app=f"{get_app_instance()}",
325363
factory=True,
326364
host=host,
327365
port=port,
@@ -347,7 +385,7 @@ def run_granian_backend(host: str, port: int, loglevel: LogLevel):
347385
from granian.server import MPServer as Granian
348386

349387
Granian(
350-
target=get_app_module(),
388+
target=get_app_instance_from_file(),
351389
factory=True,
352390
address=host,
353391
port=port,
@@ -464,7 +502,7 @@ def run_uvicorn_backend_prod(host: str, port: int, loglevel: LogLevel):
464502

465503
config = get_config()
466504

467-
app_module = get_app_module()
505+
app_module = get_app_instance()
468506

469507
command = (
470508
[
@@ -564,7 +602,7 @@ def run_granian_backend_prod(host: str, port: int, loglevel: LogLevel):
564602
*("--host", host),
565603
*("--port", str(port)),
566604
*("--interface", str(Interfaces.ASGI)),
567-
*("--factory", get_app_module()),
605+
*("--factory", get_app_instance_from_file()),
568606
]
569607
processes.new_process(
570608
command,

0 commit comments

Comments
 (0)