Streamlit API
StreamlitRenderer
StreamlitRenderer是一个用于可视化呈现数据的类。
from pygwalker.api.streamlit import StreamlitRenderer
renderer = StreamlitRenderer(df, spec="./gw_config.json")
参数
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
dataset | Union[DataFrame, Connector] | - | 要使用的数据帧或连接器。参考图形漫步数据集。 |
gid | Union[int, str] | None | GraphicWalker容器div的ID,格式为'gwalker-{gid}'。如果gid为None,则将自动生成。 |
field_specs | Optional[Dict[str, FieldSpec]] | None | 字段规格。如果未指定,将从datase t自动推断。 |
theme_key | Literal['vega', 'g2'] | 'g2' | GraphicWalker的主题类型。 |
appearance | Literal['media', 'light', 'dark'] | 'media' | 主题设置。'media'将自动检测OS主题。 |
spec | str | "" | 图表配置数据。可以是配置ID、JSON或远程文件URL。 |
spec_io_mode | Literal["r", "rw"] | "r" | spec io模式,默认为"r","r"表示只读,"rw"表示读写。 |
kernel_computation | bool | None | 如果为True,将使用核计算数据,支持在较大的数据集中高性能。默认为None,自动确定是否使用核计算。 |
kanaries_api_key | str | "" | kanaries api密钥,默认为""。 |
default_tab | Literal["data", "vis"] | "vis" | 要显示的默认选项卡。默认为"vis"。 |
**kwargs | Any | - | 其他关键字参数。 |
StreamlitRenderer.explorer
StreamlitRenderer.explorer是一个用于浏览数据的方法。
renderer.explorer()
参数
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
width | int | None | UI宽度,默认将适应页面宽度。 |
height | int | 1000 | UI高度。 |
scrolling | bool | False | 滚动设置。 |
default_tab | Literal["data", "vis"] | "vis" | 要显示的默认选项卡。默认为"vis"。 |
StreamlitRenderer.chart
StreamlitRenderer.chart是一个用于显示图表的方法。
renderer.chart(0)
参数
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
index | int | None | 图表的索引。 |
width | int | None | UI宽度,默认将适应页面宽度。 |
height | int | 1000 | UI高度。 |
scrolling | bool | False | 滚动设置。 |
StreamlitRenderer.viewer
StreamlitRenderer.viewer是一个预览全部图表的方法。
renderer.viewer()
参数
参数 | 类型 | 默认值 | 描述 |
---|---|---|---|
width | int | None | UI宽度,默认将适应页面宽度。 |
height | int | 1000 | UI高度。 |
scrolling | bool | False | 滚动设置。 |
kernel_computation启用的示例(推荐)
在线演示:pygwalker演示 (opens in a new tab)
这是一个在Streamlit应用中使用StreamlitRenderer的示例。
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("data.csv")
# If you want to use feature of saving chart config, set `spec_io_mode="rw"`
return StreamlitRenderer(df, spec="./gw_config.json")
renderer = get_pyg_renderer()
st.subheader("Display Explore UI")
tab1, tab2, tab3, tab4 = st.tabs(
["graphic walker", "data profiling", "graphic renderer", "pure chart"]
)
with tab1:
renderer.explorer()
with tab2:
renderer.explorer(default_tab="data")
with tab3:
renderer.viewer()
with tab4:
st.markdown("### registered per weekday")
renderer.chart(0)
st.markdown("### registered per day")
renderer.chart(1)
禁用kernel_computation的示例
这是一个在Streamlit应用中使用Pygwalker的示例。
import pygwalker as pyg
import pandas as pd
import streamlit.components.v1 as components
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")
# Import your data
df = pd.read_csv("/bike_sharing_dc.csv")
# Paste the copied Pygwalker chart code here
vis_spec = """[{"visId":"gw_rZy5","name":"Chart 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":{}}}]"""
# Generate the HTML using Pygwalker
pyg_html = pyg.to_html(df, spec=vis_spec)
# Embed the HTML into the Streamlit app
components.html(pyg_html, height=1000, scrolling=True)
相关问答
如何构建一个在线开源替代Tableau/PowerBI的工具,可以处理大量数据?
答:您可以使用pygwalker + streamlit + snowflake来构建一个在线开源替代Tableau/PowerBI的工具,可以处理大量数据。