Skip to content
Merged
Changes from 1 commit
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
47 changes: 46 additions & 1 deletion reflex/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import contextlib
import copy
import dataclasses
import enum
import functools
import inspect
import typing
Expand Down Expand Up @@ -479,6 +480,49 @@ def _components_from(
return ()


def _deterministic_hash(value: object) -> int:
"""Hash a rendered dictionary.

Args:
value: The dictionary to hash.

Returns:
The hash of the dictionary.
"""
if isinstance(value, BaseComponent):
# If the value is a component, hash its rendered code.
rendered_code = value.render()
return _deterministic_hash(rendered_code)
if isinstance(value, Var):
return _deterministic_hash((value._js_expr, value._get_all_var_data()))
if isinstance(value, VarData):
return _deterministic_hash(dataclasses.asdict(value))
if isinstance(value, dict):
# Sort the dictionary to ensure consistent hashing.
return _deterministic_hash(
tuple(sorted((k, _deterministic_hash(v)) for k, v in value.items()))
)
if isinstance(value, int):
# Hash numbers and booleans directly.
return int(value)
if isinstance(value, float):
return _deterministic_hash(str(value))
Comment on lines +511 to +512
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why not hash the float directly, like the int?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

_deterministic_hash has to return an int, so I'm not sure how to map it

if isinstance(value, str):
return int(md5(value.encode("utf-8")).hexdigest(), 16)
if isinstance(value, (tuple, list)):
# Hash tuples by hashing each element.
return _deterministic_hash("[" + ",".join(map(str, value)) + "]")
if isinstance(value, enum.Enum):
# Hash enums by their name.
return _deterministic_hash(value.name)
Comment thread
adhami3310 marked this conversation as resolved.
Outdated

msg = (
f"Cannot hash value `{value}` of type `{type(value).__name__}`. "
"Only BaseComponent, Var, VarData, dict, str, tuple, and enum.Enum are supported."
)
raise TypeError(msg)


DEFAULT_TRIGGERS: Mapping[str, types.ArgsSpec | Sequence[types.ArgsSpec]] = {
EventTriggers.ON_FOCUS: no_args_event_spec,
EventTriggers.ON_BLUR: no_args_event_spec,
Expand Down Expand Up @@ -2359,6 +2403,7 @@ def create(cls, component: Component) -> StatefulComponent | None:
if should_memoize or component.event_triggers:
# Render the component to determine tag+hash based on component code.
tag_name = cls._get_tag_name(component)
print(tag_name)
Comment thread
adhami3310 marked this conversation as resolved.
Outdated
if tag_name is None:
return None

Expand Down Expand Up @@ -2430,7 +2475,7 @@ def _get_tag_name(cls, component: Component) -> str | None:
return None

# Compute the hash based on the rendered code.
code_hash = md5(str(rendered_code).encode("utf-8")).hexdigest()
code_hash = _deterministic_hash(rendered_code)

# Format the tag name including the hash.
return format.format_state_name(
Expand Down
Loading