| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"id": "f7b949b7-2818-45fa-bb1a-5d1d4e99a403", |
| 6 |
"metadata": {}, |
| 7 |
"source": [ |
| 8 |
"# Streaming datagrid\n", |
| 9 |
"\n", |
| 10 |
"**This is a new feature preview, if you see any issue with this. Please fill out an issue on Github!**\n", |
| 11 |
"\n", |
| 12 |
"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", |
| 13 |
"\n", |
| 14 |
"This feature allows a **smaller memory footprint for the grid**, and **reduced loading time for the initial grid display.**" |
| 15 |
] |
| 16 |
}, |
| 17 |
{ |
| 18 |
"cell_type": "code", |
| 19 |
"execution_count": null, |
| 20 |
"id": "c6d44983-b311-46d3-8bf4-951dc163574e", |
| 21 |
"metadata": {}, |
| 22 |
"outputs": [], |
| 23 |
"source": [ |
| 24 |
"from ipydatagrid import DataGrid, StreamingDataGrid, TextRenderer, BarRenderer, Expr, ImageRenderer\n", |
| 25 |
"import pandas as pd\n", |
| 26 |
"import numpy as np\n", |
| 27 |
"\n", |
| 28 |
"\n", |
| 29 |
"def renderer_function(cell, default_value):\n", |
| 30 |
" return \"#fc8403\" if cell.value < SQRT1_2 else default_value\n", |
| 31 |
"\n", |
| 32 |
"\n", |
| 33 |
"conditional_expression = Expr(renderer_function)\n", |
| 34 |
"\n", |
| 35 |
"default_renderer = TextRenderer(background_color=conditional_expression)\n", |
| 36 |
"\n", |
| 37 |
"dataframe = pd.DataFrame(np.random.randn(100, 100))\n", |
| 38 |
"\n", |
| 39 |
"streaming_datagrid = StreamingDataGrid(\n", |
| 40 |
" dataframe,\n", |
| 41 |
" default_renderer=default_renderer,\n", |
| 42 |
" debounce_delay=50\n", |
| 43 |
")\n", |
| 44 |
"streaming_datagrid" |
| 45 |
] |
| 46 |
}, |
| 47 |
{ |
| 48 |
"cell_type": "markdown", |
| 49 |
"id": "b064d19c-24a2-462e-94d9-bfb0241d0d57", |
| 50 |
"metadata": {}, |
| 51 |
"source": [ |
| 52 |
"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." |
| 53 |
] |
| 54 |
}, |
| 55 |
{ |
| 56 |
"cell_type": "code", |
| 57 |
"execution_count": null, |
| 58 |
"id": "b50d175c-4a38-43e0-b20f-ccb8a86e6add", |
| 59 |
"metadata": {}, |
| 60 |
"outputs": [], |
| 61 |
"source": [ |
| 62 |
"new_columns = pd.DataFrame(np.random.randn(100, 100))\n", |
| 63 |
"dataframe.update(new_columns)\n", |
| 64 |
"streaming_datagrid.tick()" |
| 65 |
] |
| 66 |
} |
| 67 |
], |
| 68 |
"metadata": { |
| 69 |
"kernelspec": { |
| 70 |
"display_name": "Python 3 (ipykernel)", |
| 71 |
"language": "python", |
| 72 |
"name": "python3" |
| 73 |
}, |
| 74 |
"language_info": { |
| 75 |
"codemirror_mode": { |
| 76 |
"name": "ipython", |
| 77 |
"version": 3 |
| 78 |
}, |
| 79 |
"file_extension": ".py", |
| 80 |
"mimetype": "text/x-python", |
| 81 |
"name": "python", |
| 82 |
"nbconvert_exporter": "python", |
| 83 |
"pygments_lexer": "ipython3", |
| 84 |
"version": "3.12.2" |
| 85 |
} |
| 86 |
}, |
| 87 |
"nbformat": 4, |
| 88 |
"nbformat_minor": 5 |
| 89 |
} |