Skip to content

Commit 1acdbb4

Browse files
authored
clean up app._compile (#5599)
* clean up app._compile * remove fastapi better
1 parent f1eaab5 commit 1acdbb4

4 files changed

Lines changed: 80 additions & 94 deletions

File tree

pyproject.toml

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ keywords = ["web", "framework"]
2121
requires-python = ">=3.10,<4.0"
2222
dependencies = [
2323
"alembic >=1.15.2,<2.0",
24-
"fastapi >=0.115.0",
24+
"click >=8.2",
2525
"granian[reload] >=2.4.0",
2626
"httpx >=0.28.0,<1.0",
2727
"jinja2 >=3.1.2,<4.0",
2828
"packaging >=24.2,<26",
2929
"platformdirs >=4.3.7,<5.0",
3030
"psutil >=7.0.0,<8.0; sys_platform == 'win32'",
3131
"pydantic >=1.10.21,<3.0",
32-
"python-socketio >=5.12.0,<6.0",
3332
"python-multipart >=0.0.20,<1.0",
33+
"python-socketio >=5.12.0,<6.0",
3434
"redis >=5.2.1,<7.0",
3535
"reflex-hosting-cli >=0.1.51",
3636
"rich >=13,<15",
3737
"sqlmodel >=0.0.24,<0.1",
38-
"click >=8.2",
38+
"starlette >=0.47.0",
3939
"typing_extensions >=4.13.0",
4040
"wrapt >=1.17.0,<2.0",
4141
]
@@ -58,18 +58,49 @@ documentation = "https://reflex.dev/docs/getting-started/introduction"
5858
[project.scripts]
5959
reflex = "reflex.reflex:cli"
6060

61+
[dependency-groups]
62+
dev = [
63+
"asynctest",
64+
"darglint",
65+
"dill",
66+
"fastapi",
67+
"hatchling",
68+
"libsass",
69+
"numpy",
70+
"pandas",
71+
"pillow",
72+
"playwright",
73+
"plotly",
74+
"pre-commit",
75+
"psutil",
76+
"psycopg[binary]",
77+
"pyright",
78+
"pytest-asyncio",
79+
"pytest-benchmark",
80+
"pytest-codspeed",
81+
"pytest-cov",
82+
"pytest-mock",
83+
"pytest-playwright",
84+
"pytest-retry",
85+
"pytest-split",
86+
"pytest",
87+
"python-dotenv",
88+
"ruff",
89+
"selenium",
90+
"starlette-admin",
91+
"toml",
92+
"uvicorn",
93+
]
94+
95+
6196
[build-system]
6297
requires = ["hatchling"]
6398
build-backend = "hatchling.build"
6499

65100
[tool.hatch.build]
66101
include = ["reflex", "scripts/hatch_build.py"]
67-
68-
[tool.hatch.build.targets.sdist]
69-
artifacts = ["*.pyi"]
70-
71-
[tool.hatch.build.targets.wheel]
72-
artifacts = ["*.pyi"]
102+
targets.sdist.artifacts = ["*.pyi"]
103+
targets.wheel.artifacts = ["*.pyi"]
73104

74105
[tool.hatch.build.hooks.custom]
75106
path = "scripts/hatch_build.py"
@@ -152,39 +183,6 @@ asyncio_mode = "auto"
152183
skip = "docs/*,*.html,examples/*, *.pyi, poetry.lock, uv.lock"
153184
ignore-words-list = "te, TreeE"
154185

155-
[dependency-groups]
156-
dev = [
157-
"asynctest >=0.13",
158-
"darglint >=1.8",
159-
"dill >=0.3",
160-
"hatchling >=1.27",
161-
"libsass >=0.23",
162-
"numpy >=2.2",
163-
"pandas >=2.2",
164-
"pillow >=11",
165-
"playwright >=1.51",
166-
"plotly >=6.0",
167-
"pre-commit ==4.2.0",
168-
"psutil >=7.0.0",
169-
"psycopg[binary] >=3.2",
170-
"pyright >=1.1.400",
171-
"pytest >=8.3",
172-
"pytest-asyncio >=0.26",
173-
"pytest-benchmark >=5.1",
174-
"pytest-codspeed >=3.2",
175-
"pytest-cov >=6.1",
176-
"pytest-mock >=3.14",
177-
"pytest-playwright >=0.7",
178-
"pytest-retry >=1.7",
179-
"pytest-split >=0.10",
180-
"python-dotenv >=1",
181-
"ruff >=0.11",
182-
"selenium >=4.31",
183-
"starlette-admin >=0.14",
184-
"toml >=0.10.2",
185-
"uvicorn >=0.34.0",
186-
]
187-
188186

189187
[tool.coverage.run]
190188
source = ["reflex"]

reflex/app.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from types import SimpleNamespace
2929
from typing import TYPE_CHECKING, Any, BinaryIO, ParamSpec, get_args, get_type_hints
3030

31-
from fastapi import FastAPI
3231
from rich.progress import MofNCompleteColumn, Progress, TimeElapsedColumn
3332
from socketio import ASGIApp as EngineIOApp
3433
from socketio import AsyncNamespace, AsyncServer
@@ -441,9 +440,6 @@ class App(MiddlewareMixin, LifespanMixin):
441440
| None
442441
) = None
443442

444-
# FastAPI app for compatibility with FastAPI.
445-
_cached_fastapi_app: FastAPI | None = None
446-
447443
@property
448444
def event_namespace(self) -> EventNamespace | None:
449445
"""Get the event namespace.
@@ -598,32 +594,22 @@ def __call__(self) -> ASGIApp:
598594
Returns:
599595
The backend api.
600596
"""
597+
from reflex.vars.base import GLOBAL_CACHE
598+
601599
# For py3.9 compatibility when redis is used, we MUST add any decorator pages
602600
# before compiling the app in a thread to avoid event loop error (REF-2172).
603601
self._apply_decorated_pages()
604602

605-
compile_future = concurrent.futures.ThreadPoolExecutor(max_workers=1).submit(
606-
self._compile, prerender_routes=is_prod_mode()
607-
)
603+
self._compile(prerender_routes=is_prod_mode())
608604

609-
def callback(f: concurrent.futures.Future):
610-
# Force background compile errors to print eagerly
611-
return f.result()
612-
613-
compile_future.add_done_callback(callback)
614-
# Wait for the compile to finish to ensure all optional endpoints are mounted.
615-
compile_future.result()
605+
# We will not be making more vars, so we can clear the global cache to free up memory.
606+
GLOBAL_CACHE.clear()
616607

617608
if not self._api:
618609
msg = "The app has not been initialized."
619610
raise ValueError(msg)
620611

621-
if self._cached_fastapi_app is not None:
622-
asgi_app = self._cached_fastapi_app
623-
asgi_app.mount("", self._api)
624-
App._add_cors(asgi_app)
625-
else:
626-
asgi_app = self._api
612+
asgi_app = self._api
627613

628614
if self.api_transformer is not None:
629615
api_transformers: Sequence[Starlette | Callable[[ASGIApp], ASGIApp]] = (

tests/units/test_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
import pytest
1313
import sqlmodel
14-
from fastapi.responses import StreamingResponse
1514
from pytest_mock import MockerFixture
1615
from starlette.applications import Starlette
1716
from starlette.datastructures import UploadFile
17+
from starlette.responses import StreamingResponse
1818
from starlette_admin.auth import AuthProvider
1919
from starlette_admin.contrib.sqla.admin import Admin
2020
from starlette_admin.contrib.sqla.view import ModelView

uv.lock

Lines changed: 33 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)