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
2 changes: 2 additions & 0 deletions reflex/constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
DefaultPorts,
Expiration,
GitIgnore,
PyprojectToml,
RequirementsTxt,
)
from .custom_components import CustomComponents
Expand Down Expand Up @@ -106,6 +107,7 @@
"Page404",
"PageNames",
"Ping",
"PyprojectToml",
"Reflex",
"RequirementsTxt",
"RouteArgType",
Expand Down
7 changes: 7 additions & 0 deletions reflex/constants/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down
4 changes: 2 additions & 2 deletions reflex/reflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ 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.
console.success(
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 ""
)
)
Expand Down
16 changes: 11 additions & 5 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]:
Expand All @@ -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}"
Expand All @@ -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(
Expand Down
Loading