Clean up: use constants, remove unused code

Handle closing files with contextmanager
This commit is contained in:
Masen Furer 2025-02-13 14:15:26 -08:00
parent 9b47e3e460
commit d597a600b3
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95
4 changed files with 27 additions and 13 deletions

View File

@ -504,7 +504,7 @@ class App(MiddlewareMixin, LifespanMixin):
if not self.api:
return
upload_is_used_marker = (
prerequisites.get_web_dir() / "backend" / "upload_is_used"
prerequisites.get_backend_dir() / constants.Dirs.UPLOAD_IS_USED
)
if Upload.is_used or upload_is_used_marker.exists():
# To upload files.
@ -982,11 +982,12 @@ class App(MiddlewareMixin, LifespanMixin):
return str(datetime.now().time()).split(".")[0]
should_compile = self._should_compile()
backend_dir = prerequisites.get_web_dir() / "backend"
backend_dir = prerequisites.get_backend_dir()
if not should_compile and backend_dir.exists():
enable_state_marker = backend_dir / "enable_state"
if enable_state_marker.exists():
stateful_pages = json.load(enable_state_marker.open("r"))
stateful_pages_marker = backend_dir / constants.Dirs.STATEFUL_PAGES
if stateful_pages_marker.exists():
with stateful_pages_marker.open("r") as f:
stateful_pages = json.load(f)
for route in stateful_pages:
console.info(f"BE Evaluating stateful page: {route}")
self._compile_page(route, save_page=False)
@ -1235,9 +1236,12 @@ class App(MiddlewareMixin, LifespanMixin):
# Pickle dynamic states
if self._state is not None:
enable_state = prerequisites.get_web_dir() / "backend" / "enable_state"
enable_state.parent.mkdir(parents=True, exist_ok=True)
json.dump(list(self._stateful_pages), enable_state.open("w"))
stateful_pages_marker = (
prerequisites.get_backend_dir() / constants.Dirs.STATEFUL_PAGES
)
stateful_pages_marker.parent.mkdir(parents=True, exist_ok=True)
with stateful_pages_marker.open("w") as f:
json.dump(list(self._stateful_pages), f)
@contextlib.asynccontextmanager
async def modify_state(self, token: str) -> AsyncIterator[BaseState]:

View File

@ -53,6 +53,12 @@ class Dirs(SimpleNamespace):
POSTCSS_JS = "postcss.config.js"
# The name of the states directory.
STATES = ".states"
# Where compilation artifacts for the backend are stored.
BACKEND = "backend"
# JSON-encoded list of page routes that need to be evaluated on the backend.
STATEFUL_PAGES = "stateful_pages.json"
# Marker file indicating that upload component was used in the frontend.
UPLOAD_IS_USED = "upload_is_used"
class Reflex(SimpleNamespace):

View File

@ -98,6 +98,15 @@ def get_states_dir() -> Path:
return environment.REFLEX_STATES_WORKDIR.get()
def get_backend_dir() -> Path:
"""Get the working directory for the backend.
Returns:
The working directory.
"""
return get_web_dir() / constants.Dirs.BACKEND
def check_latest_package_version(package_name: str):
"""Check if the latest version of the package is installed.

View File

@ -1996,9 +1996,6 @@ class ComputedVar(Var[RETURN_TYPE]):
default_factory=lambda: lambda _: None
) # pyright: ignore [reportAssignmentType]
# Flag determines whether we are pickling the computed var itself
_is_pickling: ClassVar[bool] = False
def __init__(
self,
fget: Callable[[BASE_STATE], RETURN_TYPE],
@ -2410,8 +2407,6 @@ class ComputedVar(Var[RETURN_TYPE]):
Returns:
The class of the var.
"""
if self._is_pickling:
return type(self)
return FakeComputedVarBaseClass
@property