-
Notifications
You must be signed in to change notification settings - Fork 795
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
DuplicateIds exception raised when removing a tab and adding a new tab #5215
Comments
Are you awaiting the calls to add_pane and remove_pane? await remove_pane(...)
await add_pane(...) |
No, I wasn't awaiting. I just changed my code to |
I think what's happening is something like this:
|
I've created a quick MRE to demonstrate the issue. This also revealed that the tabs highlighting isn't updated correctly when the tab is removed. from textual.app import App, ComposeResult
from textual.widgets import Footer, Label, TabbedContent, TabPane
class ExampleApp(App):
BINDINGS = [
("r", "remove_pane", "Remove first pane"),
("a", "add_pane", "Add pane"),
]
def compose(self) -> ComposeResult:
with TabbedContent(initial="tab-2"):
with TabPane("tab-1"):
yield Label("tab-1")
with TabPane("tab-2"):
yield Label("tab-2")
yield Footer()
def action_remove_pane(self) -> None:
tabbed_content = self.query_one(TabbedContent)
tabbed_content.remove_pane("tab-1")
def action_add_pane(self) -> None:
tabbed_content = self.query_one(TabbedContent)
new_pane = TabPane("tab-3", Label("tab-3"))
tabbed_content.add_pane(new_pane)
if __name__ == "__main__":
app = ExampleApp()
app.run() |
This may be an easy fix. That reference to |
That was my initial reaction too, but worried that those default ID's might start to get confusing. But I suppose if you want to access the TabPanes programmatically, you should really be supplying your own ID's anyway. |
Fixes Textualize#5215 where removing then adding a pane could crash with a `DuplicateIds` exception. Rather than assigning the ID based on the *current* tab count, this adds a `_cumulative_tab_count` to ensure added panes have a unique ID.
After looking into this a bit deeper, it seems the Should the |
As a workaround, I've changed my app to store an incrementing counter and use it to generate explicit IDs for the |
I have an app in which multiple tabs of a
TabbedContent
widget may be created on startup. These tabs can also be closed/removed by the user. If two (or more) tabs are created at startup, closing a tab other than the last followed by creating a new tab crashes the app with an exception like the following:DuplicateIds: Tried to insert a widget with ID '--content-tab-tab-2', but a widget ContentTab(id='--content-tab-tab-2') already exists with that ID in this list of children. The children of a widget must have unique IDs.
My code adds new tabs via
TabbedContent.add_pane
and removes them viaTabbedContent.remove_pane
.I think the exception is due to how new
TabPane
ids are generated byTabbedContent._set_id
here:textual/src/textual/widgets/_tabbed_content.py
Line 427 in 09003df
Textual Diagnostics
Versions
Python
Operating System
Terminal
Rich Console options
The text was updated successfully, but these errors were encountered: