PyGWalker

repository·main·Indexed 12 days ago

https://github.com/kanaries/pygwalker

A Python library that transforms pandas, polars, and pyarrow dataframes into an interactive, drag-and-drop visual exploration interface. It acts as a Pythonic alternative to Tableau within Jupyter Notebooks and supports integration with web frameworks such as Streamlit, Dash, Gradio, Reflex, and Marimo.

Tokens
19.7K
Snippets
80
Records
101
Agent score
95%

What's inside PyGWalker

  1. External Resources and Community Support for PyGWalker

    main

    For developers looking to extend their knowledge or seek help, the following resources are available:

    • Research: Read the PyGWalker Paper for the theoretical foundation of on-the-fly exploratory visual data analysis.
    • Core Engine: Learn more about Graphic Walker, the underlying engine.
    • Advanced Automation: Explore RATH, an open-source automated exploratory data analysis software.
    • Integrations: See the guide for using pygwalker with Streamlit to build visual analytics apps.
    • Support: Join the community on Slack or Discord for troubleshooting and assistance.
  2. Supported environments for pygwalker

    main

    pygwalker is compatible with a wide range of notebook and web application environments. Key supported platforms include:

    • Notebooks: Jupyter Notebook, Jupyter Lab, Jupyter Lite, Google Colab, Kaggle Code, and Databricks Notebook (v0.1.4a0+).
    • IDE Extensions: Jupyter extension for Visual Studio Code (v0.1.4a0+).
    • Web Apps: Streamlit (v0.1.4.9+) via pyg.walk(df, env='Streamlit'), DataCamp Workspace (v0.1.4a0+), and marimo (v0.4.9.11).
    • Other: Panel (via panel-graphic-walker) and most web applications compatible with IPython kernels (v0.1.4a0+).
  3. Understand the PyGWalker Architecture

    main

    PyGWalker is a hybrid library consisting of a Python package and a React frontend.

    • Python package (pygwalker/): Provides the public API (walk, render, table), handles dataframe parsing, and manages the communication transports that exchange data and chart specifications with the UI.
    • Frontend app (app/): A React + Vite application. During the build process, it is compiled into JavaScript bundles and stored in pygwalker/templates/dist/ within the Python wheel.

    At runtime, the Python side serializes the dataframe and configuration, then hands the JavaScript bundles to the notebook or browser. The UI then communicates back to the Python kernel to fetch data, execute queries, and save chart specifications.

  4. How the Message Protocol Works

    main

    Communication between the kernel and the frontend follows a standardized envelope defined in protocol.py (Python) and comm.generated.ts (TypeScript).

    Request Envelope:

    • action: The operation to perform (e.g., get_datas, save_chart, update_spec, export).
    • data: The payload for the action.
    • rid: Request ID.
    • gid: Widget/App ID.

    Response Envelope:

    • code: Status/error code.
    • data: The requested data payload.
    • message: Status message.
  5. Configure pygwalker with spec and kernel_computation

    main

    When using pyg.walk(), you can use specific parameters to manage chart configurations and large datasets:

    • spec: A JSON string or file path used to save or load chart configurations (e.g., settings saved manually in the UI).
    • kernel_computation: A boolean flag. When set to True, it uses DuckDB as the computation engine, which is recommended for working with large datasets (up to 100 GB) locally.

    Note: use_kernel_calc is deprecated; use kernel_computation instead.

    df = pd.read_csv('./bike_sharing_dc.csv')
    walker = pyg.walk(
        df,
        spec="./chart_meta_0.json",    # configuration saved manually in UI
        kernel_computation=True,       # enable DuckDB for large datasets
    )
  6. Configure Reflex package structure for PyGWalker

    main

    When building a Reflex application that integrates PyGWalker, follow the standard Reflex package structure to ensure the app is correctly imported as app.app.

    Required structure:

    • rxconfig.py: Must contain app_name="app".
    • app/: A directory containing the Python package.
    • app/app.py: Must contain the app = rx.App() object.

    Example structure:

    project_root/
    ├── app/
    │   ├── __init__.py
    │   └── app.py          # Main app with PyGWalker integration
    ├── rxconfig.py         # Reflex configuration
    └── .gitignore
  7. Configure Computation Modes in PyGWalker

    main

    The computation setting determines where data queries are executed. Choosing the right mode depends on your data size and environment:

    • browser: All data is sent to the client (browser) and computed there. Best for small datasets.
    • kernel: A local DuckDB engine running within the Python process answers queries on demand. This is the recommended mode for large datasets because the frontend parses the Graphic Walker DSL to SQL, and only aggregated results are sent across the boundary.
    • cloud: Uses Kanaries cloud for computation.
  8. Access PyGWalker progressive learning tutorials

    main

    For users looking to move beyond quick scripts and master real-world workflows, the tutorials/ directory provides in-depth, step-by-step learning materials. Unlike the concise scripts in /examples, these tutorials focus on progressive learning and best practices.

    The primary resource is complete_tutorial.ipynb, which covers:

    • Environment setup and initialization
    • Core chart types and interactions
    • Data exploration workflows
    • Real-world use cases (e.g., sales analytics, segmentation, experimentation)
    • Performance tips and recommended patterns.
  9. Run the PyGWalker dev stack with hot reload

    main

    The recommended way to develop is using the scripts/dev.py orchestrator. This starts a Vite watcher for the frontend and a JupyterLab instance with the necessary environment variables (PYGWALKER_DEV=1 and ANYWIDGET_HMR=1) enabled for hot reloading.

    Start the stack

    source venv/bin/activate
    python scripts/dev.py

    Usage in Notebook

    Once the stack is running, open the JupyterLab URL provided in the console and run PyGWalker as usual:

    import pandas as pd
    import pygwalker as pyg
    
    pyg.walk(pd.DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]}))

    When you edit files in app/src/, the widget should hot-reload in place. For large changes, re-run the cell.

    Orchestrator Flags

    FlagEffect
    --no-jupyterOnly run the frontend watch build.
    --no-frontendOnly start JupyterLab (bundle already built).
    --jupyter-port NSet the JupyterLab port.
    --no-browserDon't open a browser.
    --log-dir DIRChange where per-service logs are written (default logs/).
  10. Set up the Frontend development environment

    main

    To set up the React frontend located in the app/ directory, you need to install dependencies and build the bundles.

    Prerequisites:

    • Node.js 22.x
    • Yarn 1.x

    Steps:

    1. Navigate to the app/ directory.
    2. Install dependencies using yarn install.
    3. Build the frontend bundles using yarn build.

    Note: Built assets are written to pygwalker/templates/dist/. For faster local iteration on the main app bundle specifically, use yarn build:app. This command is faster but does not perform TypeScript checking or build auxiliary DSL conversion bundles.

    cd app
    yarn install
    yarn build
  11. Use best practices for large datasets and chart persistence

    main

    For better performance and to save your work, use the following parameters in pyg.walk():

    • spec: A path to a JSON file that saves the chart state. Note: You must click the 'Save' button in the UI to persist changes to this file; auto-save is not yet supported.
    • kernel_computation: Set to True to use DuckDB as the calculation engine. This allows you to explore much larger datasets (up to 100 GB).
    df = pd.read_csv('./bike_sharing_dc.csv')
    walker = pyg.walk(
        df,
        spec="./chart_meta_0.json",    # Saves chart state to this JSON file
        kernel_computation=True,       # Uses DuckDB for datasets up to 100 GB
    )