[REF-1352] Markdown component_map hash improvements (#2241)

This commit is contained in:
Masen Furer 2023-12-01 11:45:22 -08:00 committed by GitHub
parent 49ccd2f1fb
commit eb79da8538
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
import textwrap import textwrap
from functools import lru_cache
from hashlib import md5 from hashlib import md5
from typing import Any, Callable, Dict, Union from typing import Any, Callable, Dict, Union
@ -35,6 +36,7 @@ _REHYPE_PLUGINS = Var.create_safe([_REHYPE_KATEX, _REHYPE_RAW])
# Component Mapping # Component Mapping
@lru_cache
def get_base_component_map() -> dict[str, Callable]: def get_base_component_map() -> dict[str, Callable]:
"""Get the base component map. """Get the base component map.
@ -89,6 +91,9 @@ class Markdown(Component):
# Custom styles for the markdown (deprecated in v0.2.9). # Custom styles for the markdown (deprecated in v0.2.9).
custom_styles: Dict[str, Any] = {} custom_styles: Dict[str, Any] = {}
# The hash of the component map, generated at create() time.
component_map_hash: str = ""
@classmethod @classmethod
def create(cls, *children, **props) -> Component: def create(cls, *children, **props) -> Component:
"""Create a markdown component. """Create a markdown component.
@ -124,7 +129,12 @@ class Markdown(Component):
src = textwrap.dedent(src) src = textwrap.dedent(src)
# Create the component. # Create the component.
return super().create(src, component_map=component_map, **props) return super().create(
src,
component_map=component_map,
component_map_hash=cls._component_map_hash(component_map),
**props,
)
def get_custom_components( def get_custom_components(
self, seen: set[str] | None = None self, seen: set[str] | None = None
@ -264,11 +274,15 @@ class Markdown(Component):
return components return components
def _component_map_hash(self) -> str: @staticmethod
return md5(str(self.component_map).encode()).hexdigest() def _component_map_hash(component_map) -> str:
inp = str(
{tag: component(_MOCK_ARG) for tag, component in component_map.items()}
).encode()
return md5(inp).hexdigest()
def _get_component_map_name(self) -> str: def _get_component_map_name(self) -> str:
return f"ComponentMap_{self._component_map_hash()}" return f"ComponentMap_{self.component_map_hash}"
def _get_custom_code(self) -> str | None: def _get_custom_code(self) -> str | None:
hooks = set() hooks = set()
@ -292,7 +306,7 @@ class Markdown(Component):
remark_plugins=_REMARK_PLUGINS, remark_plugins=_REMARK_PLUGINS,
rehype_plugins=_REHYPE_PLUGINS, rehype_plugins=_REHYPE_PLUGINS,
) )
.remove_props("componentMap") .remove_props("componentMap", "componentMapHash")
) )
tag.special_props.add( tag.special_props.add(
Var.create_safe( Var.create_safe(

View File

@ -8,6 +8,7 @@ from reflex.vars import Var, BaseVar, ComputedVar
from reflex.event import EventChain, EventHandler, EventSpec from reflex.event import EventChain, EventHandler, EventSpec
from reflex.style import Style from reflex.style import Style
import textwrap import textwrap
from functools import lru_cache
from hashlib import md5 from hashlib import md5
from typing import Any, Callable, Dict, Union from typing import Any, Callable, Dict, Union
from reflex.compiler import utils from reflex.compiler import utils
@ -32,6 +33,7 @@ _REHYPE_KATEX = Var.create_safe("rehypeKatex", _var_is_local=False)
_REHYPE_RAW = Var.create_safe("rehypeRaw", _var_is_local=False) _REHYPE_RAW = Var.create_safe("rehypeRaw", _var_is_local=False)
_REHYPE_PLUGINS = Var.create_safe([_REHYPE_KATEX, _REHYPE_RAW]) _REHYPE_PLUGINS = Var.create_safe([_REHYPE_KATEX, _REHYPE_RAW])
@lru_cache
def get_base_component_map() -> dict[str, Callable]: ... def get_base_component_map() -> dict[str, Callable]: ...
class Markdown(Component): class Markdown(Component):
@ -42,6 +44,7 @@ class Markdown(Component):
*children, *children,
component_map: Optional[Dict[str, Any]] = None, component_map: Optional[Dict[str, Any]] = None,
custom_styles: Optional[Dict[str, Any]] = None, custom_styles: Optional[Dict[str, Any]] = None,
component_map_hash: Optional[str] = None,
style: Optional[Style] = None, style: Optional[Style] = None,
key: Optional[Any] = None, key: Optional[Any] = None,
id: Optional[Any] = None, id: Optional[Any] = None,
@ -101,6 +104,7 @@ class Markdown(Component):
*children: The children of the component. *children: The children of the component.
component_map: The component map from a tag to a lambda that creates a component. component_map: The component map from a tag to a lambda that creates a component.
custom_styles: Custom styles for the markdown (deprecated in v0.2.9). custom_styles: Custom styles for the markdown (deprecated in v0.2.9).
component_map_hash: The hash of the component map, generated at create() time.
style: The style of the component. style: The style of the component.
key: A unique key for the component. key: A unique key for the component.
id: The id for the component. id: The id for the component.