Use ITables in Streamlit applications
mainsrc/itables/itables_for_streamlit.repository·main·Indexed 21 days ago
https://github.com/mwouts/itablesA Python package that renders Pandas and Polars DataFrames as interactive, sortable, paginatable, and searchable DataTables. It provides integrations for Jupyter notebooks, Shiny for Python, Dash, Streamlit, and Marimo, as well as functionality to export DataFrames to interactive HTML fragments.
src/itables/itables_for_streamlit.ITables is compatible with a wide range of notebook and document rendering environments, including:
jupyter nbconvert --to html.init_notebook_mode(connected=True) for reliable reloading.html and revealjs formats.When displaying interactive tables in notebooks, itables embeds the table data directly into the notebook file. To prevent notebooks from becoming excessively large and unresponsive, itables uses a downsampling mechanism.
Downsampling is triggered when the table data size exceeds the maxBytes threshold. When this happens, a warning is displayed below the table.
maxBytes: Controls the data size limit. Defaults to 64KB. You can set it to 0 to deactivate the limit entirely, though this is not recommended for large dataframes.maxRows: Limits the number of rows displayed. Defaults to 0 (no limit).maxColumns: Limits the number of columns displayed. Defaults to 200.You can apply these settings either globally via itables.options or locally for a specific table call using itables.show().
import itables
# Global configuration
itables.init_notebook_mode()
itables.options.maxBytes = "1MB"
# Local configuration for a single table
itables.show(df, maxBytes=32768)
# Other limit options
itables.options.maxRows = 100
itables.options.maxColumns = 50When calling itables.show() in an environment that cannot render HTML (such as a plain Python script or a standard python interactive session without an IPython-based frontend), ITables automatically falls back to printing a static preview.
This preview is a Markdown table containing a subset of the data: by default, it shows the first 10 rows, or a number determined by your pageLength or lengthMenu configuration options. This allows you to see a representation of your data in text-only consoles.
The itables.to_html_datatable function accepts a connected argument that controls how the necessary JavaScript dependencies are loaded:
connected=True (default): Generates an autonomous HTML fragment that loads dt_for_itables from the Internet. This is the easiest way to get a working table if you have an active internet connection.connected=False: Generates an HTML snippet that requires local dependencies. This snippet will only work if you have previously added the output of generate_init_offline_itables_html() to your HTML document. This is suitable for offline environments or air-gapped systems.ITables can be configured to work either offline or by loading libraries dynamically from the internet (connected mode).
init_notebook_mode(), the JavaScript code is embedded directly into the notebook. This works without an internet connection but increases the notebook file size.init_notebook_mode(connected=True) to load libraries dynamically from the internet. This makes the notebook file significantly lighter (by approximately 900kB).Important: In Google Colab, connected=True is the only working option.
import itables
# Use connected mode (required for Google Colab)
itables.init_notebook_mode(connected=True)connected, maxBytes, and allow_html. For a complete list of these ITables-specific options and their expected types, refer to the ITableOptions type definition in itables.typing.When customizing tables, note the following distinctions:
style argument. Instead, it is governed by either lengthMenu or scrollY.itables style argument.For ITables v2.7.0 and above, floats in Polars DataFrames are formatted according to the Polars configuration. You can control precision using pl.Config(float_precision=...).
import polars as pl
import itables
with pl.Config(float_precision=2):
itables.show(pl.Series([i * math.pi for i in range(1, 6)]))To develop itables_anywidget, install the package in editable mode with development dependencies, then install and run the JavaScript dependencies to enable hot-reloading for changes made in the js/ directory.
python -m venv .venv
source .venv/bin/activate[dev] extras:pip install -e ".[dev]"npm install
npm run devexample.ipynb in JupyterLab, VS Code, or another editor to begin testing. Changes in the js/ folder will be reflected in the notebook.python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
npm install
npm run devIf you prefer not to use the dedicated ITable Widget, you can use DT as an alternative way to render ITables in a Shiny application.
To set up the demo application using the DT approach:
pip install -r itables_DT/requirements.txtshiny run itables_DT/app-express.py
# OR
shiny run itables_DT/app-core.pypip install -r itables_DT/requirements.txt
shiny run itables_DT/app-express.py