{ "cells": [ { "cell_type": "markdown", "id": "f7b949b7-2818-45fa-bb1a-5d1d4e99a403", "metadata": {}, "source": [ "# Streaming datagrid\n", "\n", "**This is a new feature preview, if you see any issue with this. Please fill out an issue on Github!**\n", "\n", "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", "\n", "This feature allows a **smaller memory footprint for the grid**, and **reduced loading time for the initial grid display.**" ] }, { "cell_type": "code", "execution_count": null, "id": "c6d44983-b311-46d3-8bf4-951dc163574e", "metadata": {}, "outputs": [], "source": [ "from ipydatagrid import DataGrid, StreamingDataGrid, TextRenderer, BarRenderer, Expr, ImageRenderer\n", "import pandas as pd\n", "import numpy as np\n", "\n", "\n", "def renderer_function(cell, default_value):\n", " return \"#fc8403\" if cell.value < SQRT1_2 else default_value\n", "\n", "\n", "conditional_expression = Expr(renderer_function)\n", "\n", "default_renderer = TextRenderer(background_color=conditional_expression)\n", "\n", "dataframe = pd.DataFrame(np.random.randn(100, 100))\n", "\n", "streaming_datagrid = StreamingDataGrid(\n", " dataframe,\n", " default_renderer=default_renderer,\n", " debounce_delay=50\n", ")\n", "streaming_datagrid" ] }, { "cell_type": "markdown", "id": "b064d19c-24a2-462e-94d9-bfb0241d0d57", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "id": "b50d175c-4a38-43e0-b20f-ccb8a86e6add", "metadata": {}, "outputs": [], "source": [ "new_columns = pd.DataFrame(np.random.randn(100, 100))\n", "dataframe.update(new_columns)\n", "streaming_datagrid.tick()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }