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
Parameter | Type | Default | Description |
---|---|---|---|
dataset | Union[DataFrame, Connector] | - | Input data source |
spec | str | "" | Chart configuration data (ID, JSON, or URL) |
spec_io_mode | Literal["r", "rw"] | "r" | Spec I/O mode: "r" (read) or "rw" (read/write) |
kernel_computation | bool | None | Enable high-performance kernel computation |
appearance | Literal['media', 'light', 'dark'] | 'media' | Theme setting |
default_tab | Literal["data", "vis"] | "vis" | Default tab to show |
Best Practices
-
Data Loading: Load your data efficiently, considering caching for large datasets.
-
Configuration: Use a separate JSON file for chart configurations (
spec
parameter) to easily manage and update visualizations. -
Performance: For large datasets, set
kernel_computation=True
to enhance performance. -
Theming: Adjust the
appearance
parameter to match your Gradio app's theme. -
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).