Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep app id (project hash) the same even after re-init #2195

Merged
merged 4 commits into from
Nov 20, 2023
Merged
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
41 changes: 35 additions & 6 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,28 @@ def initialize_app_directory(app_name: str, template: constants.Templates.Kind):
)


def get_project_hash() -> int | None:
"""Get the project hash from the reflex.json file if the file exists.

Returns:
project_hash: The app hash.
"""
if not os.path.exists(constants.Reflex.JSON):
return None
# Open and read the file
with open(constants.Reflex.JSON, "r") as file:
data = json.load(file)
project_hash = data["project_hash"]
return project_hash


def initialize_web_directory():
"""Initialize the web directory on reflex init."""
console.log("Initializing the web directory.")

# Re-use the hash if one is already created, so we don't over-write it when running reflex init
project_hash = get_project_hash()

path_ops.cp(constants.Templates.Dirs.WEB_TEMPLATE, constants.Dirs.WEB)

initialize_package_json()
Expand All @@ -291,7 +309,7 @@ def initialize_web_directory():
update_next_config()

# Initialize the reflex json file.
init_reflex_json()
init_reflex_json(project_hash=project_hash)


def _compile_package_json():
Expand All @@ -315,11 +333,22 @@ def initialize_package_json():
file.write(code)


def init_reflex_json():
"""Write the hash of the Reflex project to a REFLEX_JSON."""
# Get a random project hash.
project_hash = random.getrandbits(128)
console.debug(f"Setting project hash to {project_hash}.")
def init_reflex_json(project_hash: int | None):
"""Write the hash of the Reflex project to a REFLEX_JSON.

Re-use the hash if one is already created, therefore do not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a blank line after the first line when expanding the docstring.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

overwrite it every time we run the reflex init command
.

Args:
project_hash: The app hash.
"""
if project_hash is not None:
console.debug(f"Project hash is already set to {project_hash}.")
else:
# Get a random project hash.
project_hash = random.getrandbits(128)
console.debug(f"Setting project hash to {project_hash}.")

# Write the hash and version to the reflex json file.
reflex_json = {
Expand Down
Loading