Skip to content

Commit 54f16d4

Browse files
authored
Make id generation customizable (#203)
Branca/folium uses random numbers to generate ids. This has some disadvantages. E.g. in the streamlit-folium plugin they have to replace the random ids with predictable ids for comparison between streamlit runs. Also, in the folium tests, we have to normalize the variable names to compare between expected and actual results. This change in Branca makes it possible to override or customize id generation. This allows client libraries to experiment with new id generation schemes.
1 parent 12c4885 commit 54f16d4

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

branca/element.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(
5858
template_name: Optional[str] = None,
5959
):
6060
self._name: str = "Element"
61-
self._id: str = hexlify(urandom(16)).decode()
61+
self._id: str = self._generate_id()
6262
self._children: OrderedDict[str, Element] = OrderedDict()
6363
self._parent: Optional[Element] = None
6464
self._template_str: Optional[str] = template
@@ -69,6 +69,10 @@ def __init__(
6969
elif template_name is not None:
7070
self._template = ENV.get_template(template_name)
7171

72+
@classmethod
73+
def _generate_id(cls) -> str:
74+
return hexlify(urandom(16)).decode()
75+
7276
def __getstate__(self) -> dict:
7377
"""Modify object state when pickling the object.
7478

0 commit comments

Comments
 (0)