Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

bool(DashTable) is always False #2906

Open
JakkuSakura opened this issue Jun 28, 2024 · 4 comments
Open

bool(DashTable) is always False #2906

JakkuSakura opened this issue Jun 28, 2024 · 4 comments
Labels
bug something broken feature something new P3 backlog

Comments

@JakkuSakura
Copy link

Describe your context

dash                      2.17.1
dash-core-components      2.0.0
dash-html-components      2.0.0
dash-table                5.0.0

Describe the bug
bool(DashTable) is always False. I can't use tb or [] for shorthand when there is no data

from dash import dash_table
df = pd.DataFrame({'a': [1, 2]})
tb = dash_table.DataTable(df.to_dict('records'), columns=[{'name': column, 'id': column} for column in df.columns])
print(bool(tb))

Expected behavior

bool(tb) evaluates to True, either always or when there is data

Screenshots

image
@T4rk1n
Copy link
Contributor

T4rk1n commented Jul 4, 2024

The condition for components is based on it's __len__ property,

def __len__(self):
"""Return the number of items in the tree."""
# TODO - Should we return the number of items that have IDs
# or just the number of items?
# The number of items is more intuitive but returning the number
# of IDs matches __iter__ better.
length = 0
if getattr(self, "children", None) is None:
length = 0
elif isinstance(self.children, Component):
length = 1
length += len(self.children)
elif isinstance(self.children, (tuple, MutableSequence)):
for c in self.children:
length += 1
if isinstance(c, Component):
length += len(c)
else:
# string or number
length = 1
return length

It only looks for the children prop to determine this and the DataTable doesn't have a children prop. You can workaround this by looking at bool(tb.data) instead.

@alexcjohnson
Copy link
Contributor

Would it be reasonable to add an explicit __bool__ for components that’s always True? That could be useful for cases where a variable might be a component or might be None.

@JakkuSakura
Copy link
Author

Yeah it would be helpful

@JakkuSakura
Copy link
Author

bool(tb.data) it's not a direct replacement of bool(tb), as it might be None and raise None value exception

@gvwilson gvwilson assigned gvwilson and unassigned gvwilson Jul 26, 2024
@gvwilson gvwilson added feature something new P3 backlog bug something broken labels Aug 13, 2024
@gvwilson gvwilson changed the title [BUG] bool(DashTable) is always False bool(DashTable) is always False Aug 13, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug something broken feature something new P3 backlog
Projects
None yet
Development

No branches or pull requests

4 participants