Skip to content

Commit d969552

Browse files
committed
0.4.1 - change update_data_by_route
Change the behavior of `DashJsonGrid.update_data_by_route(...)`. When a column is specified, a length-one sequence is provided, and the number available items of the column is also one, will broadcast the one available value rather than broadcasting all the sequence item to all columns.
1 parent 337b925 commit d969552

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
1. Fix: Update the docstring of the component and the typehint `ThemeConfigs` to the newest version.
1212

13+
#### :floppy_disk: Change
14+
15+
1. Change the behavior of `DashJsonGrid.update_data_by_route(...)`. When a column is specified, a length-one sequence is provided, and the number available items of the column is also one, will broadcast the one available value rather than broadcasting all the sequence item to all columns.
16+
1317
### 0.4.0 @ 10/21/2024
1418

1519
#### :mega: New

dash_json_grid/mixins.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,24 @@ def _broadcast_sequence(data: Sequence[Any], index_key: str, value: Sequence[Any
138138
`[{"key": val1, ...}, {"key": val2, ...}, ...]`
139139
and `value` is a sequence, broadcast `value` items to each mapping-like item
140140
of `data`."""
141-
sub_data = tuple(
141+
_sub_data = tuple(
142142
ditem for ditem in data if isinstance(ditem, collections.abc.MutableMapping)
143143
)
144-
n_data = len(sub_data)
144+
n_data = len(_sub_data)
145145
if n_data == len(value):
146-
for item, vitem in zip(sub_data, value):
146+
for item, vitem in zip(_sub_data, value):
147147
item[index_key] = vitem
148148
return
149-
sub_data = tuple(ditem for ditem in sub_data if index_key in ditem)
149+
sub_data = tuple(ditem for ditem in _sub_data if index_key in ditem)
150150
if len(sub_data) == len(value):
151151
for item, vitem in zip(sub_data, value):
152152
item[index_key] = vitem
153153
return
154+
elif n_data > 1 and len(value) == 1:
155+
vitem = value[0]
156+
for item in _sub_data:
157+
item[index_key] = vitem
158+
return
154159
raise IndexError(
155160
"The current index {0} locates a table column. However, "
156161
'the provided argument "val" does not match the length of '
@@ -175,11 +180,8 @@ def _set_by_broadcast(data: Sequence[Any], index_key: str, value: Any):
175180
data[key][index_key] = val
176181
return
177182
_set_item_of_object._broadcast_scalar(data, index_key, value)
178-
elif len(value) > 1:
183+
elif len(value) > 0:
179184
_set_item_of_object._broadcast_sequence(data, index_key, value)
180-
elif len(value) == 1:
181-
vitem = value[0]
182-
_set_item_of_object._broadcast_scalar(data, index_key, vitem)
183185
else:
184186
raise ValueError('The provided argument "value" is empty.')
185187

tests/test_table_columns.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def test_table_columns_incomplete_cols(self, data: Any) -> None:
137137
data_deleted = dash_json_grid.DashJsonGrid.delete_data_by_route(
138138
test_data, [["key3"]]
139139
)
140-
assert utils.is_eq_mapping(data_deleted, {3: 4.0})
140+
assert utils.is_eq_mapping(data_deleted, {3: ref_data[3]["key3"]})
141141
assert all("key3" not in ditem for ditem in test_data)
142142
assert not all("key3" not in ditem for ditem in ref_data)
143143
log.info(
@@ -155,6 +155,16 @@ def test_table_columns_incomplete_cols(self, data: Any) -> None:
155155
"{0}".format(data_deleted)
156156
)
157157

158+
dash_json_grid.DashJsonGrid.update_data_by_route(test_data, [["key3"]], (5.0,))
159+
data_routed = dash_json_grid.DashJsonGrid.get_data_by_route(
160+
test_data, [["key3"]]
161+
)
162+
assert utils.is_eq(data_routed, {3: 5.0})
163+
log.info(
164+
"Successfully update a column with one value. The value is: "
165+
"{0}".format(data_routed)
166+
)
167+
158168
dash_json_grid.DashJsonGrid.update_data_by_route(
159169
test_data, [["key2"]], "NEWDATA"
160170
)

0 commit comments

Comments
 (0)