itables

repository·main·Indexed 21 days ago

https://github.com/mwouts/itables

A 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.

Tokens
32.3K
Snippets
148
Records
178
Agent score
75%

What's inside itables

  1. Supported environments for ITables

    main

    ITables is compatible with a wide range of notebook and document rendering environments, including:

    • Jupyter Notebook and Jupyter Lab
    • Jupyter NB convert: Tables remain interactive when downloading notebooks as HTML or using jupyter nbconvert --to html.
    • Jupyter Book: Supported in interactive books.
    • Google Colab
    • VS Code: Works in both Jupyter Notebooks and Python scripts.
    • PyCharm: Requires init_notebook_mode(connected=True) for reliable reloading.
    • Quarto: Works with html and revealjs formats.
  2. Understand and configure ITables downsampling

    main

    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.

    Configuration Options

    • 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.

    Applying Settings

    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 = 50
  3. How `itables.show()` behaves outside of a notebook

    main

    When 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.

  4. Configure connectivity for HTML DataTable exports

    main

    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.
  5. Configure ITables Offline vs. Connected mode

    main

    ITables can be configured to work either offline or by loading libraries dynamically from the internet (connected mode).

    • Offline Mode (Default): When using init_notebook_mode(), the JavaScript code is embedded directly into the notebook. This works without an internet connection but increases the notebook file size.
    • Connected Mode: Use 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)
  6. Configure ITable-specific options

    main
    In addition to standard DataTables options, ITables provides its own set of configuration parameters. Examples include 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.
  7. Format floats using Polars configuration

    main

    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)]))
  8. Set up itables_anywidget for development

    main

    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.

    1. Create and activate a virtual environment:
    python -m venv .venv
    source .venv/bin/activate
    1. Install the package in editable mode with [dev] extras:
    pip install -e ".[dev]"
    1. Install and run JavaScript dependencies:
    npm install
    npm run dev
    1. Open example.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 dev
  9. Use DT as an alternative in Shiny for Python

    main

    If 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:

    1. Install the required dependencies:
      pip install -r itables_DT/requirements.txt
    2. Launch the application using Shiny (choose either the Express or Core version):
      shiny run itables_DT/app-express.py
      # OR
      shiny run itables_DT/app-core.py
    pip install -r itables_DT/requirements.txt
    shiny run itables_DT/app-express.py