Skip to content

Commit 3595723

Browse files
committed
Showcase the streaming datagrid for the "Paris trees" dataset
+ Fix cases for "raw" buffers Signed-off-by: martinRenou <[email protected]>
1 parent 4b850ad commit 3595723

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

examples/Streaming.ipynb

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"source": [
88
"# Streaming datagrid\n",
99
"\n",
10-
"ipydatagrid provides the ability to lazily request data to the back-end. This requires a live kernel (does not work with a notebook exported to static HTML with nbconvert)."
10+
"ipydatagrid provides the ability to lazily request data to the back-end. This requires a live kernel (does not work with a notebook exported to static HTML with nbconvert).\n",
11+
"\n",
12+
"This feature allows a **smaller memory footprint for the grid**, and **reduced loading time for the initial grid display.**"
1113
]
1214
},
1315
{
@@ -45,7 +47,7 @@
4547
"id": "b064d19c-24a2-462e-94d9-bfb0241d0d57",
4648
"metadata": {},
4749
"source": [
48-
"The `StreamingDataGrid` class provides a `tick()` method to for notifying that the underlying dataframe has changed."
50+
"The `StreamingDataGrid` class provides a `tick()` method to for notifying that the underlying dataframe has changed. **This method CANNOT be called in a loop**, due to its implementation. In fact, the front-end cannot request back the viewport for each iteration, because the kernel is already busy executing the loop and cannot respond back until the execution has finished."
4951
]
5052
},
5153
{

examples/Trees_paris.ipynb

+32-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"\n",
3535
"from py2vega.functions.color import rgb\n",
3636
"\n",
37-
"from ipydatagrid import DataGrid, TextRenderer, BarRenderer, Expr\n",
37+
"from ipydatagrid import StreamingDataGrid, TextRenderer, BarRenderer, Expr\n",
3838
"\n",
3939
"with open(\"./trees.json\") as fobj:\n",
4040
" data = load(fobj)\n",
@@ -102,12 +102,40 @@
102102
" ),\n",
103103
"}\n",
104104
"\n",
105-
"datagrid = DataGrid(\n",
105+
"datagrid = StreamingDataGrid(\n",
106106
" df, base_row_size=32, base_column_size=90, renderers=renderers\n",
107107
")\n",
108108
"VBox((highlight_box, datagrid))"
109109
]
110110
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"metadata": {},
115+
"outputs": [],
116+
"source": [
117+
"df.iloc[87642]"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"metadata": {},
124+
"outputs": [],
125+
"source": [
126+
"datagrid._handle_comm_msg(\n",
127+
" None,\n",
128+
" dict(\n",
129+
" type=\"data-request\",\n",
130+
" r1=87641,\n",
131+
" r2=87643,\n",
132+
" c1=0,\n",
133+
" c2=3,\n",
134+
" ),\n",
135+
" None\n",
136+
")"
137+
]
138+
},
111139
{
112140
"cell_type": "code",
113141
"execution_count": null,
@@ -151,7 +179,7 @@
151179
],
152180
"metadata": {
153181
"kernelspec": {
154-
"display_name": "Python 3",
182+
"display_name": "Python 3 (ipykernel)",
155183
"language": "python",
156184
"name": "python3"
157185
},
@@ -165,7 +193,7 @@
165193
"name": "python",
166194
"nbconvert_exporter": "python",
167195
"pygments_lexer": "ipython3",
168-
"version": "3.6.6"
196+
"version": "3.12.2"
169197
}
170198
},
171199
"nbformat": 4,

ipydatagrid/datagrid.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -1032,12 +1032,9 @@ def _handle_comm_msg(self, _, content, buffs):
10321032
# Extract all buffers
10331033
buffers = []
10341034
for column in serialized["data"].keys():
1035-
if not isinstance(serialized["data"][column], list):
1036-
if serialized["data"][column]["type"] == "raw":
1037-
serialized["data"][column] = serialized["data"][column]["value"]
1038-
else:
1039-
buffers.append(serialized["data"][column]["value"])
1040-
serialized["data"][column]["value"] = len(buffers) - 1
1035+
if not isinstance(serialized["data"][column], list) and not serialized["data"][column]["type"] == "raw":
1036+
buffers.append(serialized["data"][column]["value"])
1037+
serialized["data"][column]["value"] = len(buffers) - 1
10411038

10421039
answer = {
10431040
"event_type": "data-reply",

js/datagrid.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -854,10 +854,13 @@ export class StreamingDataGridModel extends DataGridModel {
854854
if (content.event_type === 'data-reply') {
855855
// Bring back buffers at their original position in the data structure
856856
for (const column of Object.keys(content.value.data)) {
857-
content.value.data[column].value =
858-
buffers[content.value.data[column].value];
857+
if (content.value.data[column].type !== 'raw') {
858+
content.value.data[column].value =
859+
buffers[content.value.data[column].value];
860+
}
859861
}
860862

863+
console.log('set data')
861864
const deserialized = deserialize_data(content.value, null);
862865
this.data_model.setModelRangeData(
863866
content.r1,

0 commit comments

Comments
 (0)