Skip to content
PYGWALKER
API Reference
Gradio Component

PyGWalker Gradio API Guide

This guide explains how to integrate PyGWalker with Gradio applications, enabling interactive data visualization capabilities within your Gradio UI.

Quick Start

To render PyGWalker in a Gradio app, use the get_html_on_gradio function:

import gradio as gr
import pandas as pd
from pygwalker.api.gradio import PYGWALKER_ROUTE, get_html_on_gradio
 
with gr.Blocks() as demo:
    df = pd.read_csv("data.csv")
    pyg_html = get_html_on_gradio(df, spec="./gw_config.json", spec_io_mode="rw")
    gr.HTML(pyg_html)
 
app = demo.launch(app_kwargs={"routes": [PYGWALKER_ROUTE]})

Key Function: get_html_on_gradio

This function generates the HTML for PyGWalker to be embedded in a Gradio application.

Important Parameters

ParameterTypeDefaultDescription
datasetUnion[DataFrame, Connector]-Input data source
specstr""Chart configuration data (ID, JSON, or URL)
spec_io_modeLiteral["r", "rw"]"r"Spec I/O mode: "r" (read) or "rw" (read/write)
kernel_computationboolNoneEnable high-performance kernel computation
appearanceLiteral['media', 'light', 'dark']'media'Theme setting
default_tabLiteral["data", "vis"]"vis"Default tab to show

Best Practices

  1. Data Loading: Load your data efficiently, considering caching for large datasets.

  2. Configuration: Use a separate JSON file for chart configurations (spec parameter) to easily manage and update visualizations.

  3. Performance: For large datasets, set kernel_computation=True to enhance performance.

  4. Theming: Adjust the appearance parameter to match your Gradio app's theme.

  5. Interactivity: Set spec_io_mode="rw" to allow users to modify and save chart configurations.

Advanced Usage

Custom Routing

Always include the PYGWALKER_ROUTE in your Gradio app's routes:

app = demo.launch(app_kwargs={"routes": [PYGWALKER_ROUTE]})

This ensures proper communication between PyGWalker and your Gradio app.

Combining with Other Gradio Components

You can combine PyGWalker with other Gradio components for a more comprehensive data analysis tool:

with gr.Blocks() as demo:
    gr.Markdown("# Data Visualization with PyGWalker")
 
    with gr.Tab("PyGWalker Explorer"):
        pyg_html = get_html_on_gradio(df, spec="./gw_config.json")
        gr.HTML(pyg_html)
 
    with gr.Tab("Data Summary"):
        gr.DataFrame(df.describe())
 
    with gr.Tab("Raw Data"):
        gr.DataFrame(df)

Troubleshooting

  • If visualizations don't render, check your data format and ensure PYGWALKER_ROUTE is properly included.
  • For performance issues with large datasets, try enabling kernel_computation and consider data sampling techniques.

For more detailed information and advanced features, refer to the PyGWalker documentation (opens in a new tab).