Skip to content
PYGWALKER
Streamlit related APIs

Streamlit API

pygwalker.api.streamlit

  • init_streamlit_comm: pygwalkerのコミュニケーション(フロントエンド -- バックエンド)を初期化します。use_kernel_calc=Trueを使用する場合に必要です。
  • get_streamlit_html: Pygwalkerの埋め込み可能なStreamlitコードを取得します。

use_kernel_calcを有効にする例

import pandas as pd
import streamlit.components.v1 as components
import streamlit as st
from pygwalker.api.streamlit import init_streamlit_comm, get_streamlit_html
 
st.set_page_config(
    page_title="StreamlitでPygwalkerを使用する",
    layout="wide"
)
 
st.title("StreamlitでPygwalkerを使用する(コミュニケーションをサポート)")
 
# Pygwalkerのコミュニケーションを初期化
init_streamlit_comm()
 
# `use_kernel_calc=True`を使用する場合は、pygwalkerのHTMLをキャッシュする必要があります(メモリが爆発しないようにするため)
@st.cache_resource
def get_pyg_html(df: pd.DataFrame) -> str:
    # アプリケーションを公開する必要がある場合、`debug=False`に設定し、他のユーザーが設定ファイルを書き換えるのを防ぎます。
    # チャートの設定を保存する機能を使用したい場合は、`debug=True`に設定します。
    html = get_streamlit_html(df, spec="./gw0.json", use_kernel_calc=True, debug=False)
    return html
 
@st.cache_data
def get_df() -> pd.DataFrame:
    return pd.read_csv("/bike_sharing_dc.csv")
 
df = get_df()
 
components.html(get_pyg_html(df), width=1300, height=1000, scrolling=True)

use_kernel_calcを無効にする例

import pygwalker as pyg
import pandas as pd
import streamlit.components.v1 as components
import streamlit as st
 
# Streamlitページの幅を調整する
st.set_page_config(
    page_title="StreamlitでPygwalkerを使用する",
    layout="wide"
)
 
# タイトルを追加する
st.title("StreamlitでPygwalkerを使用する")
 
# データをインポートする
df = pd.read_csv("/bike_sharing_dc.csv")
# コピーしたPygwalkerのチャートコードをここに貼り付ける
vis_spec = """[{"visId":"gw_rZy5","name":"チャート 1","encodings":{"dimensions":[{"dragId":"gw_BUE2","fid":"ZGF0ZV8x","name":"date","semanticType":"nominal","analyticType":"dimension"},{"dragId":"gw_x1ug","fid":"bW9udGhfMg==","name":"month","semanticType":"ordinal","analyticType":"dimension"},{"dragId":"gw_zRAa","fid":"c2Vhc29uXzM=","name":"season","semanticType":"nominal","analyticType":"dimension"},{"dragId":"gw_ZeVh","fid":"eWVhcl81","name":"year","semanticType":"nominal","analyticType":"dimension"},{"dragId":"gw_JqXv","fid":"aG9saWRheV82","name":"holiday","semanticType":"nominal","analyticType":"dimension"},{"dragId":"gw_OD2F","fid":"d29yayB5ZXMgb3Igbm90XzE0","name":"work yes or not","semanticType":"nominal","analyticType":"dimension"},{"dragId":"gw_KgQu","fid":"YW0gb3IgcG1fMTU=","name":"am or pm","semanticType":"nominal","analyticType":"dimension"},{"dragId":"gw_PqvI","fid":"RGF5IG9mIHRoZSB3ZWVrXzE2","name":"Day of the week","semanticType":"ordinal","analyticType":"dimension"}],"measures":[{"dragId":"gw_7JNg","fid":"aW5kZXhfMA==","name":"index","analyticType":"measure","semanticType":"quantitative","aggName":"sum"},{"dragId":"gw_IYM_","fid":"aG91cl80","name":"hour","analyticType":"measure","semanticType":"quantitative","aggName":"sum"},{"dragId":"gw_ofd8","fid":"dGVtcGVyYXR1cmVfNw==","name":"temperature","analyticType":"measure","semanticType":"quantitative","aggName":"sum"},{"dragId":"gw_sGlX","fid":"ZmVlbGluZ190ZW1wXzg=","name":"feeling_temp","analyticType":"measure","semanticType":"quantitative","aggName":"sum"},{"dragId":"gw_674M","fid":"aHVtaWRpdHlfOQ==","name":"humidity","analyticType":"measure","semanticType":"quantitative","aggName":"sum"},{"dragId":"gw_AxQD","fid":"d2luc3BlZWRfMTA=","name":"winspeed","analyticType":"measure","semanticType":"quantitative","aggName":"sum"},{"dragId":"gw_iy94","fid":"Y2FzdWFsXzEx","name":"casual","analyticType":"measure","semanticType":"quantitative","aggName":"sum"},{"dragId":"gw_9J2u","fid":"cmVnaXN0ZXJlZF8xMg==","name":"registered","analyticType":"measure","semanticType":"quantitative","aggName":"sum"},{"dragId":"gw_WzEF","fid":"Y291bnRfMTM=","name":"count","analyticType":"measure","semanticType":"quantitative","aggName":"sum"},{"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"}}],"rows":[{"dragId":"gw_gJqj","fid":"cmVnaXN0ZXJlZF8xMg==","name":"registered","analyticType":"measure","semanticType":"quantitative","aggName":"sum"}],"columns":[{"dragId":"gw_uZ9C","fid":"RGF5IG9mIHRoZSB3ZWVrXzE2","name":"Day of the week","semanticType":"ordinal","analyticType":"dimension"}],"color":[{"dragId":"gw_04s5","fid":"c2Vhc29uXzM=","name":"season","semanticType":"nominal","analyticType":"dimension"}],"opacity":[],"size":[],"shape":[],"radius":[],"theta":[],"details":[],"filters":[],"text":[]},"config":{"defaultAggregated":true,"geoms":["auto"],"stack":"stack","showActions":false,"interactiveScale":false,"sorted":"none","zeroScale":true,"size":{"mode":"auto","width":320,"height":200},"format":{}}}]"""
 
# Pygwalkerを使用してHTMLを生成する
pyg_html = pyg.to_html(df, spec=vis_spec)
 
# StreamlitアプリにHTMLを埋め込む
components.html(pyg_html, height=1000, scrolling=True)

関連するQ&A

pygwalkerを他の人と共有するためにウェブアプリケーション化する方法はありますか?

回答: pygwalker.to_html() APIを使用してHTMLコードを生成し、そのコードをウェブサーバーにホストすることができます。推奨される方法は、pygwalkerコードをホストするためにStreamlitを使用することです。

import pygwalker as pyg
import pandas as pd
import streamlit.components.v1 as components
# データをインポートする
df = pd.read_csv("/<your_data>.csv")
# Pygwalkerを使用してHTMLを生成する
pyg_html = pyg.to_html(df)
 
# StreamlitアプリにHTMLを埋め込む
components.html(pyg_html, height=1000, scrolling=True)

大量のデータを処理できるTableau/PowerBIのオンラインオープンソース代替を構築する方法はありますか?

回答: pygwalker + Streamlit + Snowflakeを使用して、大量のデータを処理できるTableau/PowerBIのオンラインオープンソース代替を構築することができます。