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

Correctly detect max depth of NestedSelect if level is empty #7194

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion panel/tests/widgets/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ def test_nested_select_init_empty(document, comm):
assert select.options is None
assert select.levels == []

def test_nested_select_max_depth_empty_first_sublevel(document, comm):
select = NestedSelect(options={'foo': ['a', 'b'], 'bar': []})

assert select._max_depth == 2

def test_nested_select_max_depth_empty_second_sublevel(document, comm):
select = NestedSelect(options={'foo': {'0': ['a', 'b'], '1': []}, 'bar': {'0': []}})

assert select._max_depth == 3

def test_nested_select_init_levels(document, comm):
options = {
Expand Down Expand Up @@ -500,7 +509,7 @@ def test_nested_select_partial_options_set(document, comm):
select.options = {"Ben": []}
assert select._widgets[0].value == 'Ben'
assert select._widgets[0].visible
assert select.value == {0: 'Ben'}
assert select.value == {0: 'Ben', 1: None}
Copy link
Contributor

@ahuang11 ahuang11 Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this 1: None is correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems correct to me, there are two levels after all, even if one of them is empty.



def test_nested_select_partial_value_init(document, comm):
Expand Down
14 changes: 3 additions & 11 deletions panel/widgets/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,19 +422,11 @@ def _uses_callable(self, d):
return False

def _find_max_depth(self, d, depth=1):
if d is None or len(d) == 0:
return 0
elif not isinstance(d, dict):
return depth

if isinstance(d, list) or d is None:
return depth-1
max_depth = depth
for value in d.values():
if isinstance(value, dict):
max_depth = max(max_depth, self._find_max_depth(value, depth + 1))
# dict means it's a level, so it's not the last level
# list means it's a leaf, so it's the last level
if isinstance(value, list) and len(value) == 0 and max_depth > 0:
max_depth -= 1
max_depth = max(max_depth, self._find_max_depth(value, depth + 1))
return max_depth

def _resolve_callable_options(self, i, options) -> dict | list:
Expand Down
Loading