Stop double compiles in dev mode (#1990)

This commit is contained in:
Nikhil Rao 2023-10-19 15:26:14 -07:00 committed by GitHub
parent cbf5b61a23
commit e7f0cd8e4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 1 deletions

View File

@ -593,10 +593,30 @@ class App(Base):
parent = child parent = child
return root return root
def _should_compile(self) -> bool:
"""Check if the app should be compiled.
Returns:
Whether the app should be compiled.
"""
# Check the environment variable.
if os.environ.get(constants.SKIP_COMPILE_ENV_VAR) == "yes":
return False
# Check the nocompile file.
if os.path.exists(constants.NOCOMPILE_FILE):
# Delete the nocompile file
os.remove(constants.NOCOMPILE_FILE)
return False
# By default, compile the app.
return True
def compile(self): def compile(self):
"""Compile the app and output it to the pages folder.""" """Compile the app and output it to the pages folder."""
if os.environ.get(constants.SKIP_COMPILE_ENV_VAR) == "yes": if not self._should_compile():
return return
# Create a progress bar. # Create a progress bar.
progress = Progress( progress = Progress(
*Progress.get_default_columns()[:-1], *Progress.get_default_columns()[:-1],

View File

@ -17,6 +17,7 @@ from .base import (
Templates, Templates,
) )
from .compiler import ( from .compiler import (
NOCOMPILE_FILE,
SETTER_PREFIX, SETTER_PREFIX,
CompileVars, CompileVars,
ComponentName, ComponentName,
@ -70,6 +71,7 @@ __ALL__ = [
LogLevel, LogLevel,
Next, Next,
Node, Node,
NOCOMPILE_FILE,
PackageJson, PackageJson,
PageNames, PageNames,
Page404, Page404,

View File

@ -5,6 +5,9 @@ from types import SimpleNamespace
# The prefix used to create setters for state vars. # The prefix used to create setters for state vars.
SETTER_PREFIX = "set_" SETTER_PREFIX = "set_"
# The file used to specify no compilation.
NOCOMPILE_FILE = ".web/nocompile"
class Ext(SimpleNamespace): class Ext(SimpleNamespace):
"""Extension used in Reflex.""" """Extension used in Reflex."""

View File

@ -154,6 +154,13 @@ def run_backend(
""" """
config = get_config() config = get_config()
app_module = f"{config.app_name}.{config.app_name}:{constants.CompileVars.APP}" app_module = f"{config.app_name}.{config.app_name}:{constants.CompileVars.APP}"
# Create a .nocompile file to skip compile for backend.
if os.path.exists(constants.Dirs.WEB):
with open(constants.NOCOMPILE_FILE, "w"):
pass
# Run the backend in development mode.
uvicorn.run( uvicorn.run(
app=f"{app_module}.{constants.CompileVars.API}", app=f"{app_module}.{constants.CompileVars.API}",
host=host, host=host,