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

Remove a specific component from the container #145

Closed
me21 opened this issue Nov 1, 2022 · 9 comments
Closed

Remove a specific component from the container #145

me21 opened this issue Nov 1, 2022 · 9 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@me21
Copy link
Contributor

me21 commented Nov 1, 2022

There is a method called clear which removes the children of a container. But is there a way to remove the specific child component?
In addition, this method removes JustPy objects, but does it remove corresponding NiceGUI wrappers?

@me21 me21 changed the title Remove a specific component from the page Remove a specific component from the container Nov 1, 2022
@rodja
Copy link
Member

rodja commented Nov 2, 2022

Interesting. We worked around this in our own projects by wrapping the elements which need to be removed into another row/column. This group can then be cleared individually. Another approach is to not remove the elements but simply change their visibility to False.

But there may be cases where both approaches are not ideal. We need to investigate this further. Do you think we need have a remove(element: Element) and remove_at(position: int)?

@me21
Copy link
Contributor Author

me21 commented Nov 2, 2022

I thought about remove(element: Element). Index can be counted as the order of creation or the order of on-screen position, which could be misleading if we somehow add a component before the previous one. Or this can't be easily done?

Anyway, adding a row without any gap/margin as a workaround should be good enough, thank you!

@falkoschindler
Copy link
Contributor

I added a group.remove(element) method, where element is either an Element or an index.

The operation is not very efficient in terms of bandwidth, since the whole container is sent to the client with the one element missing. But it should be handy in some situations nonetheless. (In version 1.0 will will probably support manipulating the DOM without re-transmitting the whole subtree again and again.)

@falkoschindler falkoschindler self-assigned this Nov 3, 2022
@falkoschindler falkoschindler added the enhancement New feature or request label Nov 3, 2022
@falkoschindler falkoschindler added this to the v0.9.22 milestone Nov 3, 2022
@falkoschindler
Copy link
Contributor

By the way: Like clear, remove doesn't delete the NiceGUI element, but only removes it from the DOM. It's up to you to delete all references (if any) and let the garbage collector free the memory.

@me21
Copy link
Contributor Author

me21 commented Nov 3, 2022

Thank you. How do I remove the references? Consider:

with ui.row()
    ui.label("label")

Are there any references to the label object? In this code there is none; are there any inside NiceGUI?
If I execute "clear" or "remove" methods, will label object be eventually freed by garbage collector?

@falkoschindler
Copy link
Contributor

No, NiceGUI doesn't keep references to UI elements. But if you would assign elements to global variables or add them to a list, for example, you would need to get rid of them after calling remove (e.g. setting the variable to None or removing it from a list with del labels[0]). Otherwise the garbage collector won't be allowed to free the space.

You're right: In your example there are no references to the label kept. So the GC can automatically remove it.

@me21
Copy link
Contributor Author

me21 commented Nov 7, 2022

@falkoschindler I see that in clear method you update active_links and binding_list variables whereas in remove method you do not. Is it how it's supposed to be?

Although in JustPy it's similar: delete_components method calls delete for each component, whereas remove_component just removes component from the list.

@falkoschindler
Copy link
Contributor

@me21 Thanks for pointing this out! The behavior is indeed inconsistent. I will have a closer look.

@falkoschindler falkoschindler reopened this Nov 7, 2022
@falkoschindler falkoschindler modified the milestones: v0.9.22, v0.9.24, v0.9.25 Nov 7, 2022
@falkoschindler falkoschindler modified the milestones: v0.9.26, v1.0, v1.0.1, v1.0.0 Dec 14, 2022
@falkoschindler
Copy link
Contributor

This issue is fix since NiceGUI 1.0.

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

No branches or pull requests

3 participants