Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ dependencies = [
"alembic >=1.15.2,<2.0",
"fastapi >=0.115.0",
"granian[reload] >=2.2.5",
"gunicorn >=23.0.0,<24.0.0",
"httpx >=0.28.0,<1.0",
"jinja2 >=3.1.2,<4.0",
"packaging >=24.2,<26",
Expand All @@ -38,7 +37,6 @@ dependencies = [
"sqlmodel >=0.0.24,<0.1",
"typer >=0.15.2,<1.0",
"typing_extensions >=4.13.0",
"uvicorn >=0.34.0",
"wrapt >=1.17.0,<2.0",
]
classifiers = [
Expand Down Expand Up @@ -166,4 +164,6 @@ dev = [
"ruff >=0.11",
"selenium >=4.31",
"starlette-admin >=0.14",
"uvicorn >=0.34.0",

]
80 changes: 59 additions & 21 deletions reflex/utils/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,9 @@ def run_frontend_prod(root: Path, port: str, backend_present: bool = True):

@once
def _warn_user_about_uvicorn():
# When we eventually switch to Granian by default, we should enable this warning.
if False:
console.warn(
"Using Uvicorn for backend as it is installed. This behavior will change in 0.8.0 to use Granian by default."
)
console.warn(
"Using Uvicorn for backend as it is installed. This behavior will change in 0.8.0 to use Granian by default."
)


def should_use_granian():
Expand All @@ -202,8 +200,8 @@ def should_use_granian():
Returns:
True if Granian should be used.
"""
if environment.REFLEX_USE_GRANIAN.get():
return True
if environment.REFLEX_USE_GRANIAN.is_set():
return environment.REFLEX_USE_GRANIAN.get()
if (
importlib.util.find_spec("uvicorn") is None
or importlib.util.find_spec("gunicorn") is None
Expand All @@ -219,9 +217,51 @@ def get_app_module():
Returns:
The app module for the backend.
"""
config = get_config()
return get_config().module


def get_app_instance():
"""Get the app module for the backend.

Returns:
The app module for the backend.
"""
return f"{get_app_module()}:{constants.CompileVars.APP}"

return f"{config.module}:{constants.CompileVars.APP}"

def get_app_file() -> Path:
"""Get the app file for the backend.

Returns:
The app file for the backend.

Raises:
ImportError: If the app module is not found.
"""
current_working_dir = str(Path.cwd())
if current_working_dir not in sys.path:
# Add the current working directory to sys.path
sys.path.insert(0, current_working_dir)
module_spec = importlib.util.find_spec(get_app_module())
if module_spec is None:
raise ImportError(
f"Module {get_app_module()} not found. Make sure the module is installed."
)
file_name = module_spec.origin
if file_name is None:
raise ImportError(
f"Module {get_app_module()} not found. Make sure the module is installed."
)
return Path(file_name).resolve()


def get_app_instance_from_file() -> str:
"""Get the app module for the backend.

Returns:
The app module for the backend.
"""
return f"{get_app_file()}:{constants.CompileVars.APP}"


def run_backend(
Expand Down Expand Up @@ -323,7 +363,7 @@ def run_uvicorn_backend(host: str, port: int, loglevel: LogLevel):
import uvicorn

uvicorn.run(
app=f"{get_app_module()}",
app=f"{get_app_instance()}",
factory=True,
host=host,
port=port,
Expand All @@ -349,7 +389,7 @@ def run_granian_backend(host: str, port: int, loglevel: LogLevel):
from granian.server import MPServer as Granian

Granian(
target=get_app_module(),
target=get_app_instance_from_file(),
factory=True,
address=host,
port=port,
Expand All @@ -367,14 +407,12 @@ def _deprecate_asgi_config(
config_name: str,
reason: str = "",
):
# When we eventually switch to Granian by default, we should enable this deprecation.
if False:
console.deprecate(
f"config.{config_name}",
reason=reason,
deprecation_version="0.7.5",
removal_version="0.8.0",
)
console.deprecate(
f"config.{config_name}",
reason=reason,
deprecation_version="0.7.9",
removal_version="0.8.0",
)


@once
Expand Down Expand Up @@ -468,7 +506,7 @@ def run_uvicorn_backend_prod(host: str, port: int, loglevel: LogLevel):

config = get_config()

app_module = get_app_module()
app_module = get_app_instance()

command = (
[
Expand Down Expand Up @@ -568,7 +606,7 @@ def run_granian_backend_prod(host: str, port: int, loglevel: LogLevel):
*("--host", host),
*("--port", str(port)),
*("--interface", str(Interfaces.ASGI)),
*("--factory", get_app_module()),
*("--factory", get_app_instance_from_file()),
]
processes.new_process(
command,
Expand Down
18 changes: 2 additions & 16 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading