Skip to content

Commit

Permalink
refactoring so it is still usable
Browse files Browse the repository at this point in the history
  • Loading branch information
jnumainville committed Apr 18, 2024
1 parent 177ad93 commit a8ff0b6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
28 changes: 27 additions & 1 deletion plugins/ui/src/deephaven/ui/_internal/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def wrap_callable(func: Callable) -> Callable:
return func


def create_props(args: dict[str, Any]) -> tuple[tuple[Any], dict[str, Any]]:
def create_props(args: dict[str, Any]) -> tuple[tuple[Any, ...], dict[str, Any]]:
"""
Create props from the args. Combines the named props with the kwargs props.
Expand All @@ -197,3 +197,29 @@ def create_props(args: dict[str, Any]) -> tuple[tuple[Any], dict[str, Any]]:
children, props = args.pop("children"), args.pop("props")
props.update(args)
return children, props


def unpack_item_table_source(
children: tuple[Any, ...],
props: dict[str, Any],
supported_args: set[str],
) -> tuple[tuple[Any, ...], dict[str, Any]]:
"""
Unpack children and props if the children are of type dict
and merge the supported arguments into the props.
Args:
children: The children to possibly unpack.
props: The props to unpack.
supported_args: The supported arguments for the ItemTableSource.
Returns:
The unpacked children and props.
"""
if len(children) == 1 and isinstance(children[0], dict):
item_table_source = children[0]
children = (item_table_source.pop("table"),)
for key in supported_args:
if key in item_table_source:
props[key] = item_table_source.pop(key)
return children, props
12 changes: 11 additions & 1 deletion plugins/ui/src/deephaven/ui/components/list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
from .item import ItemElement
from .item_table_source import ItemTableSource
from ..elements import BaseElement, Element
from .._internal.utils import create_props
from .._internal.utils import create_props, unpack_item_table_source
from ..types import Stringable, Selection, SelectionMode

ListViewItem = Union[Stringable, ItemElement]
ListViewElement = Element

SUPPORTED_SOURCE_ARGS = {
"key_column",
"label_column",
"description_column",
"icon_column",
"actions",
}


def list_view(
*children: ListViewItem | Table | ItemTableSource,
Expand Down Expand Up @@ -58,4 +66,6 @@ def list_view(
"""
children, props = create_props(locals())

children, props = unpack_item_table_source(children, props, SUPPORTED_SOURCE_ARGS)

return BaseElement("deephaven.ui.components.ListView", *children, **props)
12 changes: 11 additions & 1 deletion plugins/ui/src/deephaven/ui/components/picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
from .section import SectionElement, PickerItem
from .item_table_source import ItemTableSource
from ..elements import BaseElement
from .._internal.utils import create_props
from .._internal.utils import create_props, unpack_item_table_source
from ..types import Key

PickerElement = BaseElement

SUPPORTED_SOURCE_ARGS = {
"key_column",
"label_column",
"description_column",
"icon_column",
"title_column",
}


def picker(
*children: PickerItem | SectionElement | Table | PartitionedTable | ItemTableSource,
Expand Down Expand Up @@ -53,4 +61,6 @@ def picker(
"""
children, props = create_props(locals())

children, props = unpack_item_table_source(children, props, SUPPORTED_SOURCE_ARGS)

return BaseElement("deephaven.ui.components.Picker", *children, **props)
41 changes: 41 additions & 0 deletions plugins/ui/test/deephaven/ui/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest

from .BaseTest import BaseTestCase


Expand Down Expand Up @@ -217,6 +218,46 @@ def test_func_with_all_args(a, /, b, *args, c=1, **kwargs):
# Test that wrapping a function without a signature doesn't throw an error
wrapped = wrap_callable(print)

def test_unpack_item_table_source(self):
from deephaven.ui._internal.utils import unpack_item_table_source
from deephaven.ui import item_table_source

children = ("table",)
props = {
"test": "foo",
}

expected_children = ("table",)
expected_props = {
"test": "foo",
}

self.assertTupleEqual(
unpack_item_table_source(children, props, {}),
(expected_children, expected_props),
)

item_data_source = item_table_source(
table="table", key_column="key", actions="actions"
)

children = (item_data_source,)
props = {
"test": "foo",
}

expected_children = ("table",)

expected_props = {
"test": "foo",
"key_column": "key",
}

self.assertTupleEqual(
unpack_item_table_source(children, props, {"table", "key_column"}),
(expected_children, expected_props),
)


if __name__ == "__main__":
unittest.main()

0 comments on commit a8ff0b6

Please sign in to comment.