-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
205b759
commit ab5d738
Showing
4 changed files
with
69 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,36 @@ | ||
"""Module containing the standard implementation of `dash_table.DataTable`.""" | ||
from collections import defaultdict | ||
from collections.abc import Mapping | ||
from typing import Any | ||
|
||
import pandas as pd | ||
from dash import dash_table | ||
|
||
from vizro.models.types import capture | ||
|
||
|
||
def _set_defaults_nested(supplied: Mapping[str,Any], defaults: Mapping[str,Any]) -> dict[str,Any]: | ||
supplied = defaultdict(dict, supplied) | ||
for default_key, default_value in defaults.items(): | ||
if isinstance(default_value, Mapping): | ||
supplied[default_key] = _set_defaults_nested(supplied[default_key], default_value) | ||
else: | ||
supplied.setdefault(default_key, default_value) | ||
return dict(supplied) | ||
|
||
|
||
@capture("table") | ||
def dash_data_table(data_frame: pd.DataFrame, **kwargs): | ||
"""Standard `dash_table.DataTable`.""" | ||
kwargs.setdefault("columns", [{"name": i, "id": i} for i in data_frame.columns]) | ||
defaults = { | ||
"columns": [{"name": i, "id": i} for i in data_frame.columns], | ||
"style_as_list_view": True, | ||
"style_data": {"border_bottom": "1px solid var(--border-subtle-alpha-01)", "height": "40px"}, | ||
"style_header": { | ||
"border_bottom": "1px solid var(--state-overlays-selected-hover)", | ||
"border_top": "1px solid var(--main-container-bg-color)", | ||
"height": "32px", | ||
}, | ||
} | ||
kwargs = _set_defaults_nested(kwargs, defaults) | ||
return dash_table.DataTable(data=data_frame.to_dict("records"), **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from vizro.tables.dash_table import _set_defaults_nested | ||
import pytest | ||
|
||
@pytest.fixture | ||
def default_dictionary(): | ||
return {"a": {"b": {"c": 1, "d": 2}},"e": 3} | ||
|
||
@pytest.mark.parametrize("input, expected_output", [ | ||
({},{"a": {"b": {"c": 1, "d": 2}},"e": 3}), # nothing supplied | ||
({'e': 10},{"a": {"b": {"c": 1, "d": 2}},"e": 10}), # flat main key | ||
({"a": {"b": {"c": 11, "d": 12}}},{"a": {"b": {"c": 11, "d": 12}},"e": 3}), # updated multiple nested keys | ||
({"a": {"b": {"c": 1, "d": {"f":42}}}},{"a": {"b": {"c": 1, "d": {"f":42}}},"e": 3}), # add new dict | ||
({"a": {"b": {"c": 5}}},{'a': {'b': {'c': 5, 'd': 2}},"e": 3}) #arbitrary nesting | ||
]) | ||
def test_set_defaults_nested(default_dictionary, input, expected_output): | ||
assert _set_defaults_nested(input, default_dictionary) == expected_output |