Skip to content

bool(DashTable) is always False #2906

Open

Description

@JakkuSakura

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

Activity

  1. T4rk1n commented on Jul 4, 2024

    @T4rk1n
    Contributor

    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.

  2. alexcjohnson commented on Jul 5, 2024

    @alexcjohnson
    Collaborator

    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.

  3. JakkuSakura commented on Jul 8, 2024

    @JakkuSakura
    Author

    Yeah it would be helpful

  4. JakkuSakura commented on Jul 8, 2024

    @JakkuSakura
    Author

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

  5. changed the title [-][BUG] bool(DashTable) is always False[/-] [+]bool(DashTable) is always False[/+] on Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3backlogbugsomething brokenfeaturesomething new

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions