Skip to content

provide tools and prompts to clone child element if the child already has a parent #186

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

rl-utility-man
Copy link

@rl-utility-man rl-utility-man commented Jan 21, 2025

I reported Folium issue 1885, which occurs because passing a single element to the add_child routine twice overwrites the first value of the parent variable inside the element object. @Conengmo suggested adding a clone routine to address this problem, which I have done. Applying add_child(exampleChild) always overwrites the value of the exampleChild.parent variable, which will lead to undesirable behavior, so I changed add_child to emit a warning that recommends submitting a clone instead when users try to do that.

Thanks for your help. Looking forward to discussing the best way forward.

@rl-utility-man rl-utility-man changed the title automatically clone child element if the child already has a parent provide tools and prompts to clone child element if the child already has a parent Feb 4, 2025
@Conengmo Conengmo self-requested a review March 19, 2025 07:43
Copy link
Member

@Conengmo Conengmo left a comment

Choose a reason for hiding this comment

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

Hi @rl-utility-man, thanks for taking an interest and trying to improve this aspect of the library. I added some comments. I'm mostly interested in how robust this copy method is, I hope we can discuss that. Thanks!

"""Add a child and modify the child with a pointer to its parent."""

if child._parent is not None:
warnings.warn(
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit conflicted about this warning. On the one hand, it's good to warn for potential issues. On the other hand, there are cases where you want to overwrite the _parent attribute, and generating warnings will lead to confusion. An example is the current workaround we have for using one Icon with multiple Markers: python-visualization/folium#2068

Maybe as a compromise, instead of using the warnings library, instead use a logging message.

logger = logging.getLogger("branca")
logger.warn("bla"

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I'd also try to make the message shorter.

@@ -88,6 +89,14 @@ def __setstate__(self, state: dict):

self.__dict__.update(state)

def clone(self):
Copy link
Member

Choose a reason for hiding this comment

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

Add a type hint:

Suggested change
def clone(self):
def clone(self) -> 'Element':

"""creates a new copy of an element, but with a unique identifier
and without the prior version's relationship to a parent object"""
clone = copy(self)
clone._id = hexlify(urandom(16)).decode()
Copy link
Member

Choose a reason for hiding this comment

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

we should split this logic off into a _create_id method or something, since we use it now in two places. But okay if you don't do that in this PR.

def clone(self):
"""creates a new copy of an element, but with a unique identifier
and without the prior version's relationship to a parent object"""
clone = copy(self)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder about cases where this copy is not sufficient.

  • What if the element has children?
  • Does it matter if we don't create a new Template object?
  • Folium has all kinds of classes, maybe there are some that have class attributes for which a simple copy is not sufficient?

I'm not sure how we can know this clone method works robustly for all Folium classes.

And if we use deepcopy, is that too much?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants