### Install PyGWalker Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Install the PyGWalker library using pip. The '-q' flag ensures a quiet installation. ```python # Install PyGWalker !pip install pygwalker -q print("โœ… PyGWalker installed successfully!") ``` -------------------------------- ### Install Frontend Dependencies Source: https://github.com/kanaries/pygwalker/blob/main/docs/DEVELOPMENT.md Navigate to the app directory and install frontend dependencies using Yarn. ```bash cd app yarn install ``` -------------------------------- ### Example: Cloud Spec with Workspace and File Name Source: https://github.com/kanaries/pygwalker/wiki/Use-cloud-config-in-pygwalker-to-save-your-spec An example demonstrating how to specify a cloud configuration file using your workspace name and desired file name. Ensure your Kanaries token is set up. ```python walker = pyg.walk(df, spec="ksf://dkxmfdn12/share_config.json") ``` -------------------------------- ### Install PyGWalker with Snowflake Support Source: https://github.com/kanaries/pygwalker/wiki/PyGWalker-with-Snowflake Install PyGWalker and its Snowflake integration. Use the --pre flag for pre-release versions. ```bash pip install --upgrade --pre pygwalker pip install --upgrade --pre "pygwalker[snowflake]" ``` -------------------------------- ### Install Pygwalker with Reflex Plugin Source: https://github.com/kanaries/pygwalker/blob/main/examples/README.md Install Pygwalker with the optional Reflex plugin for Reflex application integration. ```shell pip install "pygwalker[reflex]" ``` -------------------------------- ### Install Pygwalker Source: https://github.com/kanaries/pygwalker/wiki/How-to-save-charts-in-juypter-cell-and-share-it-with-others? Install the pygwalker library using pip. This is a prerequisite for using any of its functionalities. ```bash pip install pygwalker ``` -------------------------------- ### Install Pygwalker Beta Version Source: https://github.com/kanaries/pygwalker/wiki/How-to-upload-your-pygwalker-charts-to-kanaries-cloud-and-share-others? Install the beta version of Pygwalker to enable the cloud publishing feature. This command ensures you have the latest pre-release version. ```bash pip install --pre --upgrade pygwalker ``` -------------------------------- ### Run Reflex Application Source: https://github.com/kanaries/pygwalker/blob/main/examples/reflex_demo/README.md Start the Reflex development server. This command compiles and runs the web application, making it accessible in a browser. ```bash reflex run ``` -------------------------------- ### Clone and Set Up Python Environment Source: https://github.com/kanaries/pygwalker/blob/main/docs/DEVELOPMENT.md Clone the PyGWalker repository and set up a Python virtual environment. Install PyGWalker in editable mode with development dependencies. ```bash git clone https://github.com/Kanaries/pygwalker.git cd pygwalker # Create and activate virtual environment python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate # Install pygwalker in editable mode with dev dependencies pip install -e ".[dev]" ``` -------------------------------- ### Hot-Reload Development: Start Vite Dev Server Source: https://github.com/kanaries/pygwalker/blob/main/docs/DEVELOPMENT.md Start the Vite development server in the `app` directory. This enables live reloading for frontend code changes. ```bash cd app yarn dev ``` -------------------------------- ### Hot-Reload Development: Start JupyterLab with Server Proxy Source: https://github.com/kanaries/pygwalker/blob/main/docs/DEVELOPMENT.md In a new terminal, activate your virtual environment and start JupyterLab. Configure it to proxy the Vite dev server by specifying the `pyg_dev_app` server details. ```bash source venv/bin/activate jupyter lab --ServerProxy.servers="{'pyg_dev_app': {'command': [], 'absolute_url': True, 'port': 8769, 'timeout': 30}}" ``` -------------------------------- ### Get Help for Pygwalker Config CLI Source: https://github.com/kanaries/pygwalker/wiki/How-to-set-your-privacy-configuration? Displays help information for the pygwalker config command, outlining available options and their usage. ```bash pygwalker config --help ``` -------------------------------- ### Install Pygwalker and Gradio Dependencies Source: https://github.com/kanaries/pygwalker/wiki/Use-pygwalker-in-graido Install the required Python packages using pip. This includes pygwalker for visualization, gradio for the web UI, and pandas for data manipulation. ```bash pip install pygwalker pip install gradio pip install pandas ``` -------------------------------- ### Install ipywidgets for PygWalker Source: https://github.com/kanaries/pygwalker/blob/main/pygwalker/templates/pygwalker_main_page.html If the PygWalker UI fails to load, ensure ipywidgets is installed or upgraded. This command installs or upgrades the package. ```bash pip install --upgrade ipywidgets ``` -------------------------------- ### Run Gradio Application Source: https://github.com/kanaries/pygwalker/wiki/Use-pygwalker-in-graido Execute the Gradio application from your terminal. This command starts the web server, making your Pygwalker visualization accessible via a web browser. ```bash gradio gradio_demo.py ``` -------------------------------- ### Import Libraries and Load Data with PygWalker Source: https://github.com/kanaries/pygwalker/blob/main/tests/field-spec.ipynb Import pandas and PygWalker, then load a CSV file into a pandas DataFrame. This is the initial setup for using PygWalker. ```python import pandas as pd df = pd.read_csv('./bike_sharing_dc.csv', parse_dates=['date']) import pygwalker as pyg ``` -------------------------------- ### Install PyGWalker with Conda Source: https://github.com/kanaries/pygwalker/blob/main/README.md Install the PyGWalker package from conda-forge using either conda or mamba. ```bash conda install -c conda-forge pygwalker ``` ```bash mamba install -c conda-forge pygwalker ``` -------------------------------- ### Integrating PyGWalker with Streamlit Source: https://github.com/kanaries/pygwalker/blob/main/README.md Provides an example of how to use PyGWalker within a Streamlit application. It includes setting page configuration, caching the renderer, and displaying the PyGWalker explorer. ```python from pygwalker.api.streamlit import StreamlitRenderer import pandas as pd import streamlit as st # Adjust the width of the Streamlit page st.set_page_config( page_title="Use Pygwalker In Streamlit", layout="wide" ) # Add Title st.title("Use Pygwalker In Streamlit") # You should cache your pygwalker renderer, if you don't want your memory to explode @st.cache_resource def get_pyg_renderer() -> "StreamlitRenderer": df = pd.read_csv("./bike_sharing_dc.csv") # If you want to use feature of saving chart config, set `spec_io_mode="rw"` return StreamlitRenderer(df, spec="./gw_config.json", spec_io_mode="rw") renderer = get_pyg_renderer() renderer.explorer() ``` -------------------------------- ### Import Libraries and Check Version Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Import necessary libraries like pandas and pygwalker, and suppress warnings. It also prints the installed PyGWalker version. ```python # Import necessary libraries import pandas as pd import pygwalker as pyg import warnings warnings.filterwarnings('ignore') # Check PyGWalker version print(f"๐Ÿท PyGWalker version: {pyg.__version__}") print("โœ… All imports successful!") ``` -------------------------------- ### Visualize DataFrame with PygWalker Source: https://github.com/kanaries/pygwalker/blob/main/tests/main-polars.ipynb Launches PygWalker to visualize the loaded Polars DataFrame. This requires PygWalker to be installed. ```python import pygwalker as pyg pyg.walk(df) ``` -------------------------------- ### Get PygWalker walk Function Signature Source: https://github.com/kanaries/pygwalker/blob/main/tests/field-spec.ipynb Use the '?' operator to inspect the signature and documentation of the `pyg.walk` function. This helps understand its parameters and usage. ```python pyg.walk? ``` -------------------------------- ### Build Frontend for First Run Source: https://github.com/kanaries/pygwalker/blob/main/docs/DEVELOPMENT.md Build the frontend application. This is required before the first run. ```bash yarn build ``` -------------------------------- ### Initialize Reflex Application Source: https://github.com/kanaries/pygwalker/blob/main/examples/reflex_demo/README.md Initialize a new Reflex project. This command should be run once to set up the basic project structure. ```bash reflex init ``` -------------------------------- ### Production Build Source: https://github.com/kanaries/pygwalker/blob/main/docs/DEVELOPMENT.md Build all variants of the frontend for production. The built files will be placed in `pygwalker/templates/dist/`. ```bash cd app yarn build # Builds all variants (iife, es, dsl-to-workflow, vega-to-dsl) ``` -------------------------------- ### Simple Development: Rebuild Frontend Source: https://github.com/kanaries/pygwalker/blob/main/docs/DEVELOPMENT.md For simple development, make changes in `app/src/`, then rebuild the frontend using `yarn build:app` which is faster than a full build. Remember to restart your Jupyter kernel to test changes. ```bash cd app yarn build:app # Faster than full build ``` -------------------------------- ### Using PyGWalker with Custom Specifications Source: https://github.com/kanaries/pygwalker/blob/main/README.md Demonstrates how to load a CSV file and initialize PyGWalker with a custom specification file for chart configuration. It also shows how to enable kernel computation for handling larger datasets. ```python df = pd.read_csv('./bike_sharing_dc.csv') walker = pyg.walk( df, spec="./chart_meta_0.json", # this json file will save your chart state, you need to click save button in ui mannual when you finish a chart, 'autosave' will be supported in the future. kernel_computation=True, # set `kernel_computation=True`, pygwalker will use duckdb as computing engine, it support you explore bigger dataset(<=100GB). ) ``` -------------------------------- ### Import Libraries for PygWalker Source: https://github.com/kanaries/pygwalker/blob/main/examples/component_demo.ipynb Import the necessary libraries, pygwalker and pandas, to start using PygWalker functionalities. ```python import pygwalker as pyg import pandas as pd ``` -------------------------------- ### Sample Data for Efficient Exploration Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Demonstrates how to sample a large DataFrame to significantly speed up initial data exploration in PygWalker. This avoids performance issues with very large datasets. ```python # โœ… Good Practice: Sample for exploration sample_size = 10000 df_sample = large_dataset.sample(n=sample_size, random_state=42) print(f"โœ… Sampled {sample_size:,} rows for exploration") print(f"๐Ÿ“Š That's {(sample_size/len(large_dataset)*100):.1f}% of the data") print(f"โšก Speed improvement: ~{len(large_dataset)//sample_size}x faster!") ``` ```python # Fast exploration with sampled data pyg.walk(df_sample, hide_data_source_config=True, kernel_computation=True) ``` -------------------------------- ### Get Current Pygwalker Privacy Setting with GlobalVarManager Source: https://github.com/kanaries/pygwalker/wiki/How-to-set-your-privacy-configuration? Retrieve and print the current privacy level that is active for the Pygwalker session. ```python from pygwalker import GlobalVarManager print(GlobalVarManager.privacy) ``` -------------------------------- ### PygWalker with Custom Configuration Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Launch PygWalker with specific configurations for a customized user experience and improved performance. Options include hiding data source configuration, specifying a configuration file, and enabling kernel computation. ```python # PyGWalker with custom configuration pyg.walk( df_enhanced, hide_data_source_config=True, spec="./config.json", # Save/load your chart configurations (optional) kernel_computation=True # Better performance for large datasets ) ``` -------------------------------- ### Upload DataFrame to Kanaries Cloud Source: https://github.com/kanaries/pygwalker/wiki/How-to-upload-your-dataset-to-kanaries-cloud? Use this function to upload a Pandas DataFrame to Kanaries Cloud. Ensure you have pandas and pygwalker installed. ```python from pygwalker.api.kanaries_cloud import create_cloud_dataset import pandas as pd df = pd.read_csv("xxx.csv") create_cloud_dataset(df) ``` -------------------------------- ### Navigate to Demo Directory Source: https://github.com/kanaries/pygwalker/blob/main/examples/reflex_demo/README.md Change the current directory to the Reflex demo's location. This is a necessary step before initializing or running the Reflex application. ```bash cd examples/reflex_demo ``` -------------------------------- ### PygWalker Usage with Options Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Shows how to use PygWalker with various configuration options to customize the user interface and behavior. Options include hiding data source configuration, setting appearance, enabling kernel computation, and loading/saving specifications. ```python pyg.walk( df, hide_data_source_config=True, # Cleaner UI appearance='light', # or 'dark' kernel_computation=True, # Better performance spec="./config.json" # Save/load config ) ``` -------------------------------- ### Initialize Pygwalker with Data and Configuration Source: https://github.com/kanaries/pygwalker/blob/main/examples/jupyter_demo.ipynb Load a dataset using pandas and initialize Pygwalker for interactive data exploration. The 'spec' argument allows loading a previous configuration. ```python import pygwalker as pyg import pandas as pd df = pd.read_csv("https://kanaries-app.s3.ap-northeast-1.amazonaws.com/public-datasets/bike_sharing_dc.csv") walker = pyg.walk(df, spec="./gw_config.json") ``` -------------------------------- ### Initial Visual Exploration with Pygwalker Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Launches Pygwalker for interactive visual exploration of a DataFrame. Recommended for a quick overview before deep dives. ```python # 4. PyGWalker for visual exploration โญ # Spend 10-15 minutes exploring interactively pyg.walk(df, hide_data_source_config=True) ``` -------------------------------- ### Get PygWalker FieldSpec Class Signature Source: https://github.com/kanaries/pygwalker/blob/main/tests/field-spec.ipynb Use the '?' operator to inspect the signature and documentation of the `pyg.FieldSpec` class. This is useful for understanding how to define custom field specifications. ```python pyg.FieldSpec? ``` -------------------------------- ### Summarize and Analyze Sales Data with Pandas Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Provides a quick statistical overview and aggregated sales by product using pandas describe() and groupby(). Useful for initial data exploration before visualization. ```python print("๐Ÿ“Š Sales Summary Statistics:\n") print(sales_data.describe()) print("\n" + "="*60) print("๐ŸŽฏ Sales by Product:\n") print(sales_data.groupby('product')['revenue'].agg(['sum', 'mean', 'count']).sort_values('sum', ascending=False)) ``` -------------------------------- ### Initialize PygWalker with Visualization Specification Source: https://github.com/kanaries/pygwalker/blob/main/tests/main.ipynb Initialize PygWalker with a DataFrame and a predefined visualization specification. This allows for reproducible visualizations. ```python import pygwalker as pyg vis_spec = r"""{"config":[{"config":{"defaultAggregated":true,"geoms":["bar"],"coordSystem":"generic","limit":-1,"timezoneDisplayOffset":0},"encodings":{"dimensions":[{"dragId":"gw_d0SN","fid":"date","name":"date","basename":"date","semanticType":"temporal","analyticType":"dimension","offset":0},{"dragId":"gw_fCVU","fid":"month","name":"month","basename":"month","semanticType":"ordinal","analyticType":"dimension","offset":0},{"dragId":"gw_xAWV","fid":"season","name":"season","basename":"season","semanticType":"nominal","analyticType":"dimension","offset":0},{"dragId":"gw_ho7q","fid":"year","name":"year","basename":"year","semanticType":"ordinal","analyticType":"dimension","offset":0},{"dragId":"gw_1bIC","fid":"holiday","name":"holiday","basename":"holiday","semanticType":"nominal","analyticType":"dimension","offset":0},{"dragId":"gw_K8Ek","fid":"work yes or not","name":"work yes or not","basename":"work yes or not","semanticType":"ordinal","analyticType":"dimension","offset":0},{"dragId":"gw_tORa","fid":"am or pm","name":"am or pm","basename":"am or pm","semanticType":"nominal","analyticType":"dimension","offset":0},{"dragId":"gw_RMSm","fid":"Day of the week","name":"Day of the week","basename":"Day of the week","semanticType":"quantitative","analyticType":"dimension","offset":0},{"dragId":"gw_mea_key_fid","fid":"gw_mea_key_fid","name":"Measure names","analyticType":"dimension","semanticType":"nominal","offset":0},{"fid":"gw_hYau","dragId":"gw_hYau","name":"Weekday [date]","semanticType":"ordinal","analyticType":"dimension","aggName":"sum","computed":true,"expression":{"op":"dateTimeFeature","as":"gw_hYau","params":[{"type":"field","value":"date"},{"type":"value","value":"weekday"},{"type":"format","value":"%Y-%m-%d"}]},"offset":0},{"fid":"gw_lSdd","dragId":"gw_lSdd","name":"Quarter [date]","semanticType":"ordinal","analyticType":"dimension","aggName":"sum","computed":true,"expression":{"op":"dateTimeFeature","as":"gw_lSdd","params":[{"type":"field","value":"date"},{"type":"value","value":"quarter"},{"type":"format","value":"%Y-%m-%d"}]},"offset":0}]},"measures":[{"dragId":"gw_oE-g","fid":"hour","name":"hour","basename":"hour","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0},{"dragId":"gw_LZNz","fid":"temperature","name":"temperature","basename":"temperature","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0},{"dragId":"gw_JbdF","fid":"feeling_temp","name":"feeling_temp","basename":"feeling_temp","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0},{"dragId":"gw_7hAr","fid":"humidity","name":"humidity","basename":"humidity","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0},{"dragId":"gw_a8mK","fid":"winspeed","name":"winspeed","basename":"winspeed","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0},{"dragId":"gw_Yb-_","fid":"casual","name":"casual","basename":"casual","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0},{"dragId":"gw_fdQ9","fid":"registered","name":"registered","basename":"registered","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0},{"dragId":"gw_Bdj1","fid":"count","name":"count","basename":"count","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0},{"dragId":"gw_count_fid","fid":"gw_count_fid","name":"Row count","analyticType":"measure","semanticType":"quantitative","aggName":"sum","computed":true,"expression":{"op":"one","params":[],"as":"gw_count_fid"},"offset":0},{"dragId":"gw_mea_val_fid","fid":"gw_mea_val_fid","name":"Measure values","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0}],"rows":[{"dragId":"gw_XI5j","fid":"registered","name":"registered","basename":"registered","analyticType":"measure","semanticType":"quantitative","aggName":"sum","offset":0}],"columns":[{"fid":"gw_hYau","dragId":"gw_Jx83","name":"Weekday [date]","semanticType":"ordinal","analyticType":"dimension","aggName":"sum","computed":true,"expression":{"op":"dateTimeFeature","as":"gw_hYau","params":[{"type":"field","value":"date"},{"type":"value","value":"weekday"},{"type":"format","value":"%Y-%m-%d"}]},"offset":0}],"color":[{"fid":"gw_lSdd","dragId":"gw_BZBe","name":"Quarter [date]","semanticType":"ordinal","analyticType":"dimension","aggName":"sum","computed":true,"expression":{"op":"dateTimeFeature","as":"gw_lSdd","params":[{"type":"field","value":"date"},{"type":"value","value":"quarter"},{"type":"format","value":"%Y-%m-%d"}]},"offset":0}],"opacity":[],"size":[],"shape":[],"radius":[],"theta":[],"longitude":[],"latitude":[],"geoId":[],"details":[],"filters":[{"dragId":"gw_U38p","fid":"month","name":"month","basename":"month","semanticType":"ordinal","analyticType":"dimension","rule":{"type":"range" ``` -------------------------------- ### Create Realistic Sales Dataset with Pandas Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Generates a synthetic sales dataset using pandas and numpy, including date, product, region, channel, units sold, unit price, and calculated revenue. Useful for demonstrating sales analysis. ```python import pandas as pd import numpy as np from datetime import datetime, timedelta np.random.seed(42) date_range = pd.date_range(end=datetime.now(), periods=365, freq='D') products = ['Laptop', 'Phone', 'Tablet', 'Headphones', 'Smartwatch', 'Camera'] regions = ['North America', 'Europe', 'Asia', 'South America'] channels = ['Online', 'Retail'] n_records = 1000 sales_data = pd.DataFrame({ 'date': np.random.choice(date_range, n_records), 'product': np.random.choice(products, n_records), 'region': np.random.choice(regions, n_records), 'channel': np.random.choice(channels, n_records), 'units_sold': np.random.randint(1, 50, n_records), 'unit_price': np.random.uniform(50, 2000, n_records).round(2), }) sales_data['revenue'] = (sales_data['units_sold'] * sales_data['unit_price']).round(2) sales_data.loc[sales_data['date'].dt.month.isin([11, 12]), 'revenue'] *= 1.5 sales_data['revenue'] = sales_data['revenue'].round(2) sales_data = sales_data.sort_values('date').reset_index(drop=True) print("๐Ÿ’ฐ Sales dataset created!") print(f"๐Ÿ“Š Records: {len(sales_data):,}") print(f"๐Ÿ’ต Total Revenue: ${sales_data['revenue'].sum():,.2f}") print(f"๐Ÿ“… Date Range: {sales_data['date'].min().date()} to {sales_data['date'].max().date()}") print("\n" + "="*60) sales_data.head(10) ``` -------------------------------- ### Create A/B Test Dataset with NumPy and Pandas Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Generates a synthetic dataset for A/B testing, simulating user behavior and conversion rates for two variants (A and B). Includes user ID, variant assignment, conversion status, time on page, and pages viewed. Sets a random seed for reproducibility. ```python # Create A/B test dataset np.random.seed(789) n_users = 1000 # Variant B performs slightly better (simulate this) variant_a_users = n_users // 2 variant_b_users = n_users - variant_a_users ab_test_data = pd.DataFrame({ 'user_id': range(1, n_users + 1), 'variant': ['A'] * variant_a_users + ['B'] * variant_b_users, 'converted': ( list(np.random.choice([0, 1], variant_a_users, p=[0.75, 0.25])) + # A: 25% conversion list(np.random.choice([0, 1], variant_b_users, p=[0.65, 0.35])) # B: 35% conversion ), 'time_on_page': np.concatenate([ np.random.uniform(30, 180, variant_a_users), # A: average 105 seconds np.random.uniform(45, 210, variant_b_users) # B: average 127 seconds ]), 'pages_viewed': np.concatenate([ np.random.randint(1, 8, variant_a_users), # A: fewer pages np.random.randint(2, 10, variant_b_users) # B: more pages ]), }) ``` -------------------------------- ### Connect to Snowflake and Visualize Data with PyGWalker Source: https://github.com/kanaries/pygwalker/wiki/PyGWalker-with-Snowflake Connect to Snowflake using provided credentials and a SQL query, then visualize the data with PyGWalker. Ensure your Snowflake connection string and query are correctly formatted. ```python import pygwalker as pyg from pygwalker.data_parsers.database_parser import Connector conn = Connector( "snowflake://user_name:password@account_identifier/database/schema", """ SELECT * FROM SNOWFLAKE_SAMPLE_DATA.TPCH_SF1.ORDERS """ ) walker = pyg.walk(conn) ``` -------------------------------- ### Streamlit Multiple Rendering Modes with PyGWalker Source: https://github.com/kanaries/pygwalker/wiki/About-PyGWalker-0.3.8 Demonstrates how to initialize PyGWalker communication in Streamlit and use the StreamlitRenderer for both exploration and pure chart display. Cache the renderer for memory efficiency and set debug=False for production. ```python from pygwalker.api.streamlit import init_streamlit_comm, StreamlitRenderer # Initialize pygwalker communication init_streamlit_comm() # You should cache your pygwalker renderer, if you don't want your memory to explode @st.cache_resource def get_pyg_renderer() -> "StreamlitRenderer": df = get_data() # When you need to publish your application, you need set `debug=False`,prevent other users to write your config file. return StreamlitRenderer(df, spec="./billion_config.json", debug=True) renderer = get_pyg_renderer() # Display explore ui, Developers can use this to prepare the charts you need to display. renderer.render_explore() # Display pure chart, index is the order of charts in explore mode, starting from 0. renderer.render_pure_chart(0) ``` -------------------------------- ### Prepare A/B Test Data with PygWalker Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb This code prepares A/B test data by adding order values, rounding numeric columns, and adding test days. It then prints a summary of the dataset. ```python ab_test_data['order_value'] = 0 ab_test_data.loc[ab_test_data['converted'] == 1, 'order_value'] = np.random.uniform(20, 200, ab_test_data['converted'].sum()) ab_test_data['time_on_page'] = ab_test_data['time_on_page'].round(1) ab_test_data['order_value'] = ab_test_data['order_value'].round(2) ab_test_data['test_day'] = np.random.randint(1, 15, n_users) print("๐Ÿงช A/B Test dataset created!") print(f"๐Ÿ‘ฅ Total Users: {len(ab_test_data):,}") print(f"๐Ÿ“Š Variant A: {variant_a_users:,} users") print(f"๐Ÿ“Š Variant B: {variant_b_users:,} users") print("\n" + "="*60) ab_test_data.head(10) ``` -------------------------------- ### Pygwalker Config Command Help Output Source: https://github.com/kanaries/pygwalker/wiki/How-to-set-your-privacy-configuration? This output details the usage and options for the 'pygwalker config' command, including available configurations like privacy and kanaries_token. ```bash $ pygwalker config --help usage: pygwalker config [-h] [--set [key=value ...]] [--reset [key ...]] [--reset-all] [--list] Modify configuration file. (default: /Users/douding/Library/Application Support/pygwalker/config.json) Available configurations: - privacy ['offline', 'update-only', 'events'] (default: events). "offline": fully offline, no data is send or api is requested "update-only": only check whether this is a new version of pygwalker to update "events": share which events about which feature is used in pygwalker, it only contains events data about which feature you arrive for product optimization. No DATA YOU ANALYSIS IS SEND. - kanaries_token ['your kanaries token'] (default: empty string). your kanaries token, you can get it from https://kanaries.net. refer: https://space.kanaries.net/t/how-to-get-api-key-of-kanaries. by kanaries token, you can use kanaries service in pygwalker, such as share chart, share config. options: -h, --help show this help message and exit --set [key=value ...] Set configuration. e.g. "pygwalker config --set privacy=update-only" --reset [key ...] Reset user configuration and use default values instead. e.g. "pygwalker config --reset privacy" --reset-all Reset all user configuration and use default values instead. e.g. "pygwalker config --reset-all" --list List current used configuration. ``` -------------------------------- ### Sample Data for Faster Exploration Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Creates a smaller sample of the original DataFrame for quicker analysis and to prevent browser freezing with large datasets. Use `df.sample()`. ```python # โœ… Solution 1: Sample your data df_sample = df.sample(min(10000, len(df))) pyg.walk(df_sample) ``` -------------------------------- ### Login via Pygwalker CLI Source: https://github.com/kanaries/pygwalker/wiki/How-to-set-your-kanaries-token-in-pygwalker? Use this command to log in locally and set your Kanaries token. Requires Pygwalker version 0.3.20 or higher. This is a recommended method. ```bash pygwalker login ``` -------------------------------- ### Initialize PygWalker with Custom Field Specifications Source: https://github.com/kanaries/pygwalker/blob/main/tests/field-spec.ipynb Import `FieldSpec` and create a list of custom field specifications to define semantic and analytic types for specific columns before calling `pyg.walk`. ```python from pygwalker import FieldSpec field_specs = [ FieldSpec(fname="date", semantic_type="temporal", analytic_type="dimension"), FieldSpec(fname="hour", semantic_type="ordinal", analytic_type="dimension"), ] pyg.walk(df, field_specs=field_specs) ``` -------------------------------- ### Initialize Pygwalker with Specification Code Source: https://github.com/kanaries/pygwalker/wiki/How-to-save-charts-in-juypter-cell-and-share-it-with-others? Initialize Pygwalker using a visualization specification directly embedded as a string. This method avoids the need for external files. ```python import pygwalker as pyg walker = pyg.walk(df=df, spec="") ``` -------------------------------- ### Clean Rebuild for Troubleshooting Source: https://github.com/kanaries/pygwalker/blob/main/docs/DEVELOPMENT.md If encountering issues like React version errors, perform a clean rebuild by removing `node_modules`, reinstalling dependencies, and rebuilding the frontend. ```bash cd app rm -rf node_modules yarn install yarn build ``` -------------------------------- ### Create Customer Dataset for Segmentation Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Generates a synthetic customer dataset with various attributes like subscription details, spending, engagement, and derived metrics such as lifetime value and churn risk. Use this for simulating customer behavior and segmentation. ```python # Create customer dataset np.random.seed(123) n_customers = 500 customer_data = pd.DataFrame({ 'customer_id': range(1, n_customers + 1), 'age': np.random.randint(18, 70, n_customers), 'subscription_months': np.random.randint(1, 36, n_customers), 'monthly_spend': np.random.uniform(10, 200, n_customers).round(2), 'login_frequency': np.random.randint(1, 30, n_customers), 'support_tickets': np.random.randint(0, 10, n_customers), 'referrals': np.random.randint(0, 5, n_customers), }) # Calculate lifetime value customer_data['lifetime_value'] = customer_data['subscription_months'] * customer_data['monthly_spend'] # Create engagement score customer_data['engagement_score'] = (customer_data['login_frequency'] * 2) + (customer_data['referrals'] * 10) - (customer_data['support_tickets'] * 3) # Create segments based on lifetime value customer_data['value_segment'] = pd.cut( customer_data['lifetime_value'], bins=[0, 500, 2000, 10000], labels=['Low Value', 'Medium Value', 'High Value'] ) # Create age groups customer_data['age_group'] = pd.cut( customer_data['age'], bins=[0, 25, 35, 50, 100], labels=['18-25', '26-35', '36-50', '50+'] ) # Churn prediction (synthetic) customer_data['churn_risk'] = np.where( (customer_data['engagement_score'] < 20) & (customer_data['subscription_months'] < 6), 'High Risk', np.where( (customer_data['engagement_score'] < 40) & (customer_data['subscription_months'] < 12), 'Medium Risk', 'Low Risk' ) ) print("๐Ÿ‘ฅ Customer dataset created!") print(f"๐Ÿ“Š Total Customers: {len(customer_data):,}") print(f"๐Ÿ’ฐ Average Lifetime Value: ${customer_data['lifetime_value'].mean():,.2f}") print(f"โš ๏ธ High Churn Risk: {(customer_data['churn_risk'] == 'High Risk').sum()} customers") print("\n" + "="*60) customer_data.head(10) ``` -------------------------------- ### Plotly vs PygWalker for Visualization Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Illustrates creating individual charts with Plotly using Python code, compared to PygWalker's approach of generating multiple interactive visualizations from a single call. ```python # Plotly - still requires code for each chart import plotly.express as px fig = px.scatter(df, x='x', y='y', color='category', size='value') fig.show() # Different chart? New code! fig = px.bar(df, x='category', y='value') fig.show() ``` ```python # Switch between chart types with clicks! pyg.walk(df) ``` -------------------------------- ### Load Data into Pandas DataFrame Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Demonstrates loading data into a pandas DataFrame from different sources including URLs, local files, dictionaries, and Excel. Ensure data is in a DataFrame format for PygWalker. ```python df_from_url = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv") print("โœ… Method 1: Loaded from URL") ``` ```python data_dict = { 'species': ['Adelie', 'Gentoo', 'Chinstrap'], 'avg_mass': [3700, 5076, 3733], 'count': [152, 124, 68] } df_from_dict = pd.DataFrame(data_dict) print("โœ… Method 2: Created from dictionary") ``` -------------------------------- ### Customer Segmentation Overview Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Prints a summary of customer segmentation based on value, churn risk, and age groups. Use this to quickly understand the distribution of customers across different segments. ```python # Customer segments overview print("๐Ÿ“Š Customer Segmentation Overview:\n") print("By Value Segment:") print(customer_data['value_segment'].value_counts()) print("\nBy Churn Risk:") print(customer_data['churn_risk'].value_counts()) print("\nBy Age Group:") print(customer_data['age_group'].value_counts()) ``` -------------------------------- ### Create Rectangular Bin Plot with Layout Source: https://github.com/kanaries/pygwalker/blob/main/examples/component_demo.ipynb Create a rectangular plot by binning 'feeling_temp' and 'temperature' into 6 bins each, coloring by the mean of 'humidity'. The layout is set with a specific height and width. ```python (component.rect().encode(x='bin("feeling_temp", 6)', y='bin("temperature", 6)', color="MEAN(humidity)").layout(height=400, width=460)) ``` -------------------------------- ### Initialize Pygwalker with JSON Spec Source: https://github.com/kanaries/pygwalker/wiki/How-to-save-charts-in-juypter-cell-and-share-it-with-others? Initialize Pygwalker by pointing it to a local JSON file where chart configurations will be saved. This method is recommended for portability. ```python import pygwalker as pyg walker = pyg.walk(df=df, spec="./my_charts.json") ``` -------------------------------- ### Pre-Aggregate Data for Time Trends Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Shows how to aggregate large datasets by date and event type before visualization to drastically improve performance when analyzing time-based trends. This reduces the number of records to process. ```python # โœ… Good Practice: Aggregate first! daily_summary = large_dataset.groupby([ large_dataset['date'].dt.date, 'event_type' ]).agg({ 'user_id': 'nunique', # Unique users 'value': ['sum', 'mean'], 'session_duration': 'mean' }).reset_index() daily_summary.columns = ['date', 'event_type', 'unique_users', 'total_value', 'avg_value', 'avg_duration'] print(f"โœ… Aggregated from {len(large_dataset):,} โ†’ {len(daily_summary):,} rows") print(f"โšก That's a {len(large_dataset)//len(daily_summary)}x reduction!") print("\nNow this will be lightning fast! โšก") daily_summary.head() ``` ```python # Super fast visualization of aggregated data pyg.walk(daily_summary, hide_data_source_config=True, kernel_computation=True) ``` -------------------------------- ### Initialize PyGWalker App with Event Listener Source: https://github.com/kanaries/pygwalker/blob/main/app/index.html This snippet shows how to import the PyGWalkerApp and set up an event listener to receive pyg_props. It's used to initialize the GWalker component when the 'pyg_props' message type is received. ```javascript import PyGWalkerApp from './src/index'; window.addEventListener('message', function(event) { if (event.data.type == "pyg_props") { PyGWalkerApp.GWalker(event.data.data, "pygwalker-app"); } }, false); ``` -------------------------------- ### Streamlit Renderer with Pre-filters using PyGWalker Source: https://github.com/kanaries/pygwalker/wiki/About-PyGWalker-0.3.8 Shows how to use the `PreFilter` class to pre-filter data in a Streamlit PyGWalker renderer. Global pre-filters can be set, and specific charts can override these with their own pre-filters. ```python from pygwalker.api.streamlit import PreFilter pre_filters = [] pre_filters.append(PreFilter( field="country", op="one of", value=["CN", "US", "EN"] )) # set global pre-filters in renderer renderer.set_global_pre_filters(pre_filters) # Set a pre-filter for a certain chart, it will overwrite global pre-filters renderer.render_pure_chart(0, pre_filters=pre_filters) ``` -------------------------------- ### Create Gradio Demo with Pygwalker Integration Source: https://github.com/kanaries/pygwalker/wiki/Use-pygwalker-in-graido This Python script sets up a Gradio interface that displays a Pygwalker visualization. It reads data from a CSV, generates an HTML representation of the Pygwalker chart, and embeds it into a Gradio HTML component. Ensure 'xxx.csv' and 'gw_config.json' exist in the same directory. ```python 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("xxx.csv") pyg_html = get_html_on_gradio(df, spec="gw_config.json", debug=True) gr.HTML(pyg_html) app = demo.launch(app_kwargs={ "routes": [PYGWALKER_ROUTE] }) ``` -------------------------------- ### View All Pygwalker Configurations via CLI Source: https://github.com/kanaries/pygwalker/wiki/How-to-set-your-privacy-configuration? This command lists all current configurations, including the privacy level, stored in the Pygwalker configuration file. ```bash pygwalker config --list ``` -------------------------------- ### Initialize PygWalker with DataFrame and Spec Source: https://github.com/kanaries/pygwalker/blob/main/tests/main.ipynb Use this snippet to initialize PygWalker with a Pandas DataFrame and a predefined visualization specification. Ensure the 'df' DataFrame and 'vis_spec' are correctly defined before execution. ```python walker = pyg.walk(df, spec=vis_spec) ``` -------------------------------- ### Load Data and Basic Dataframe Overview Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Loads data from a CSV file and prints its shape, data types, and counts of missing values. Essential for initial data understanding. ```python # 1. Load your data df = pd.read_csv("your_data.csv") # 2. Quick overview print(f"Shape: {df.shape}") print(f"\nData types:\n{df.dtypes}") print(f"\nMissing values:\n{df.isnull().sum()}") ``` -------------------------------- ### Explore Heatmaps with PyGWalker Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Launches PyGWalker with the summary statistics DataFrame to create and explore heatmap visualizations. ```python # Heatmap exploration pyg.walk(summary_df, hide_data_source_config=True) ``` -------------------------------- ### Explore Line Charts with PyGWalker Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Launches PyGWalker to visualize the created time-series data, enabling interactive exploration of trends. ```python # Line chart exploration pyg.walk(penguin_trends_long, hide_data_source_config=True) ``` -------------------------------- ### Quick A/B Test Summary and Lift Calculation Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Calculates and prints a summary of A/B test results, including conversion rates, average time on page, pages viewed, and order value. It also computes and displays the lift in conversion rate from variant B to variant A. ```python # Quick A/B test summary print("๐Ÿ“Š A/B Test Results Summary:\n") summary = ab_test_data.groupby('variant').agg({ 'converted': ['sum', 'mean'], 'time_on_page': 'mean', 'pages_viewed': 'mean', 'order_value': 'mean' }).round(3) summary.columns = ['Total Conversions', 'Conversion Rate', 'Avg Time (sec)', 'Avg Pages', 'Avg Order Value'] print(summary) # Calculate lift conv_rate_a = ab_test_data[ab_test_data['variant'] == 'A']['converted'].mean() conv_rate_b = ab_test_data[ab_test_data['variant'] == 'B']['converted'].mean() lift = ((conv_rate_b - conv_rate_a) / conv_rate_a * 100) print(f"\n๐Ÿš€ Lift (B vs A): {lift:.1f}%") print(f"{ '๐ŸŽ‰ Variant B wins!' if lift > 0 else '๐Ÿ“‰ Variant A is better'}") ``` -------------------------------- ### Configure Kanaries Token via Pygwalker CLI Source: https://github.com/kanaries/pygwalker/wiki/How-to-set-your-kanaries-token-in-pygwalker? Set your Kanaries API key using the `pygwalker config` command. This is a recommended method for configuring your token. ```bash pygwalker config --set kanaries_token=your_api_key ``` -------------------------------- ### Create Large Dataset for Demonstration Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Generates a large Pandas DataFrame with various data types for performance testing. This is useful for simulating real-world large datasets. ```python import pandas as pd import numpy as np np.random.seed(42) large_dataset = pd.DataFrame({ 'date': pd.date_range('2020-01-01', periods=500000, freq='min'), 'user_id': np.random.randint(1, 10000, 500000), 'event_type': np.random.choice(['click', 'view', 'purchase', 'cart'], 500000), 'value': np.random.uniform(0, 100, 500000), 'session_duration': np.random.randint(10, 3600, 500000) }) print(f"๐Ÿ“Š Large dataset created: {len(large_dataset):,} rows") print(f"๐Ÿ’พ Memory usage: {large_dataset.memory_usage(deep=True).sum() / 1024**2:.2f} MB") ``` -------------------------------- ### Upload Database Connector to Kanaries Cloud Source: https://github.com/kanaries/pygwalker/wiki/How-to-upload-your-dataset-to-kanaries-cloud? Uploads only your database meta information to Kanaries Cloud. Requires a Connector object configured with your database credentials and query. ```python conn = Connector( "postgresql+psycopg2://user_name:password@host:port/database", "SELECT * FROM table_name" ) create_cloud_dataset(conn) ``` -------------------------------- ### Set Kanaries API Key Environment Variable (Windows) Source: https://github.com/kanaries/pygwalker/wiki/How-to-set-your-kanaries-token-in-pygwalker? Set the `KANARIES_API_KEY` environment variable on Windows systems using the `setx` command. Replace `` with your actual API key. ```bash setx KANARIES_API_KEY โ€œโ€ ``` -------------------------------- ### Initialize PygWalker with DataFrame Source: https://github.com/kanaries/pygwalker/blob/main/tests/field-spec.ipynb Call the `pyg.walk` function with a pandas DataFrame to launch the PygWalker interface for data visualization. ```python pyg.walk(df) ``` -------------------------------- ### pandas.plot() vs PygWalker for Quick Visualization Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Shows the basic usage of pandas.plot() for quick, limited visualizations, contrasted with PygWalker's capability for more powerful and interactive data exploration with minimal code. ```python # Quick but limited df['value'].plot(kind='hist') df.groupby('category')['value'].mean().plot(kind='bar') ``` ```python # Quick AND powerful pyg.walk(df) ``` -------------------------------- ### Visualize Customer Segments with PygWalker Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Launches PygWalker to visually explore the customer segmentation data. This is the primary step for interactive analysis and discovering insights from the generated dataset. ```python # Analyze customer segments! ๐ŸŽฏ pyg.walk(customer_data, hide_data_source_config=True) ``` ```python # Analyze customer segments! ๐ŸŽฏ pyg.walk(customer_data, hide_data_source_config=True) ``` -------------------------------- ### Create Time-Series Data for Line Charts Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Generates a synthetic time-series dataset simulating penguin population counts over months, reshaping it for PyGWalker compatibility. ```python # Let's create a time-series dataset for demonstration import numpy as np # Simulate penguin population monitoring over months months = pd.date_range('2023-01-01', periods=12, freq='M') penguin_trends = pd.DataFrame({ 'month': months, 'Adelie_count': np.random.randint(45, 55, 12), 'Gentoo_count': np.random.randint(35, 45, 12), 'Chinstrap_count': np.random.randint(20, 30, 12) }) # Reshape for PyGWalker penguin_trends_long = penguin_trends.melt( id_vars=['month'], var_name='species', value_name='count' ) print("๐Ÿ“ˆ Time-series data created!") penguin_trends_long.head(10) ``` -------------------------------- ### Initialize PyGWalker with a DataFrame Source: https://github.com/kanaries/pygwalker/blob/main/README.md Load a CSV file into a pandas DataFrame and then initialize PyGWalker to create an interactive data visualization interface. ```python df = pd.read_csv('./bike_sharing_dc.csv') walker = pyg.walk(df) ``` -------------------------------- ### List Seaborn Datasets and URLs Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb This snippet lists available seaborn datasets and their corresponding CSV file URLs. It's useful for quickly accessing sample data for PygWalker demonstrations. ```python datasets = ['penguins', 'diamonds', 'titanic', 'taxis', 'iris', 'tips', 'flights'] print("๐Ÿ“Š Available seaborn datasets:") for dataset in datasets: url = f"https://raw.githubusercontent.com/mwaskom/seaborn-data/master/{dataset}.csv" print(f" โ€ข {dataset}: {url}") ``` -------------------------------- ### Initialize PyGWalker with a DataFrame Source: https://github.com/kanaries/pygwalker/wiki/Data-Painter-in-PyGWalker Use `pyg.walk()` to load a pandas DataFrame into PyGWalker for visual exploration and data manipulation. ```python import pygwalker as pyg import pandas as pd df = pd.read_csv("your data") df.head() walker = pyg.walk(df) ``` -------------------------------- ### Matplotlib/Seaborn vs PygWalker for Visualization Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Demonstrates the traditional approach using Matplotlib/Seaborn requiring multiple lines of code for various plots, contrasted with PygWalker's single line for interactive exploration. ```python # Traditional approach - multiple lines of code import matplotlib.pyplot as plt import seaborn as sns fig, axes = plt.subplots(2, 2, figsize=(12, 10)) # Plot 1: Scatter axes[0, 0].scatter(df['x'], df['y']) axes[0, 0].set_title('X vs Y') # Plot 2: Histogram axes[0, 1].hist(df['value'], bins=20) axes[0, 1].set_title('Value Distribution') # Plot 3: Box plot sns.boxplot(data=df, x='category', y='value', ax=axes[1, 0]) axes[1, 0].set_title('Value by Category') # Plot 4: Line chart df.groupby('date')['value'].mean().plot(ax=axes[1, 1]) axes[1, 1].set_title('Trend Over Time') plt.tight_layout() plt.show() ``` ```python # One line! ๐ŸŽ‰ pyg.walk(df) # Then drag and drop to create all 4 visualizations interactively! ``` -------------------------------- ### PygWalker Performance Optimization with Sampling Source: https://github.com/kanaries/pygwalker/blob/main/tutorials/pygwalker_complete_tutorial.ipynb Illustrates how to optimize PygWalker performance on large datasets by sampling a subset of the data. This is particularly useful when dealing with datasets that might cause performance issues. ```python # Sample large datasets df_sample = df.sample(10000) pyg.walk(df_sample, kernel_computation=True) ```