diff --git a/reflex/constants/__init__.py b/reflex/constants/__init__.py index b403b9eccb0..b765bb5c064 100644 --- a/reflex/constants/__init__.py +++ b/reflex/constants/__init__.py @@ -41,6 +41,7 @@ DefaultPorts, Expiration, GitIgnore, + PyprojectToml, RequirementsTxt, ) from .custom_components import CustomComponents @@ -106,6 +107,7 @@ "Page404", "PageNames", "Ping", + "PyprojectToml", "Reflex", "RequirementsTxt", "RouteArgType", diff --git a/reflex/constants/config.py b/reflex/constants/config.py index a49216c00cf..6ae12a92aeb 100644 --- a/reflex/constants/config.py +++ b/reflex/constants/config.py @@ -49,6 +49,13 @@ class GitIgnore(SimpleNamespace): } +class PyprojectToml(SimpleNamespace): + """Pyproject.toml constants.""" + + # The pyproject.toml file. + FILE = "pyproject.toml" + + class RequirementsTxt(SimpleNamespace): """Requirements.txt constants.""" diff --git a/reflex/reflex.py b/reflex/reflex.py index f1391355f43..0310efd5c93 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -86,7 +86,7 @@ def _init( prerequisites.initialize_gitignore() # Initialize the requirements.txt. - wrote_to_requirements = prerequisites.initialize_requirements_txt() + needs_user_manual_update = prerequisites.initialize_requirements_txt() template_msg = f" using the {template} template" if template else "" # Finish initializing the app. @@ -94,7 +94,7 @@ def _init( f"Initialized {app_name}{template_msg}." + ( f" Make sure to add {constants.RequirementsTxt.DEFAULTS_STUB + constants.Reflex.VERSION} to your requirements.txt or pyproject.toml file." - if not wrote_to_requirements + if needs_user_manual_update else "" ) ) diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 278283d05fb..d3c3c7c4242 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -840,18 +840,24 @@ def initialize_gitignore( def initialize_requirements_txt() -> bool: """Initialize the requirements.txt file. - If absent, generate one for the user. + If absent and no pyproject.toml file exists, generate one for the user. If the requirements.txt does not have reflex as dependency, generate a requirement pinning current version and append to the requirements.txt file. Returns: - True if the requirements.txt file was created or updated, False otherwise. + True if the user has to update the requirements.txt file. Raises: Exit: If the requirements.txt file cannot be read or written to. """ requirements_file_path = Path(constants.RequirementsTxt.FILE) + if ( + not requirements_file_path.exists() + and Path(constants.PyprojectToml.FILE).exists() + ): + return True + requirements_file_path.touch(exist_ok=True) for encoding in [None, "utf-8"]: @@ -864,12 +870,12 @@ def initialize_requirements_txt() -> bool: console.error(f"Failed to read {requirements_file_path}.") raise click.exceptions.Exit(1) from e else: - return False + return True for line in content.splitlines(): if re.match(r"^reflex[^a-zA-Z0-9]", line): console.debug(f"{requirements_file_path} already has reflex as dependency.") - return True + return False console.debug( f"Appending {constants.RequirementsTxt.DEFAULTS_STUB} to {requirements_file_path}" @@ -879,7 +885,7 @@ def initialize_requirements_txt() -> bool: "\n" + constants.RequirementsTxt.DEFAULTS_STUB + constants.Reflex.VERSION ) - return True + return False def initialize_app_directory(