PyGWalker Component API
ドラッグアンドドロップ explorer だけでなく、Python のメソッドチェーンで PyGWalker チャートを構築したい場合は pygwalker.component(...) を使います。
import pygwalker as pyg
chart = (
pyg.component(df, spec_path="./gw_config.json", computation="browser")
.bar()
.encode(x="category", y="sum(revenue)", color="region")
.layout(width=720, height=420)
)
html = chart.to_html()Factory
pyg.component(
dataset,
*,
field_specs=None,
spec="",
spec_path=None,
spec_io_mode="rw",
theme_key="vega",
appearance="media",
show_cloud_tool=False,
computation=None,
kernel_computation=None,
kanaries_api_key="",
**kwargs,
) -> Componentdataset には pandas DataFrame、polars DataFrame、pyarrow Table、データベース Connector、または Connector 形式文字列を渡せます。
ローカルチャート状態ファイルには spec_path を使います。新しいコードでは computation を使ってください。kernel_computation はレガシーで、PyGWalker 0.7.0 で削除予定です。
Mark メソッド
Mark メソッドはコピーされた Component を返すため、チェーンを安全に再利用できます。
| メソッド | チャート mark |
|---|---|
.bar() | Bar |
.line() | Line |
.area() | Area |
.trail() | Trail |
.scatter() | Point / scatter |
.circle() | Circle |
.tick() | Tick |
.rect() | Rect |
.arc() | Arc |
.text() | Text |
.box() | Box plot |
.table() | Table |
.poi() | 地理的な関心地点 |
Encodings
フィールドを視覚チャンネルへ対応付けるには .encode(...) を使います。
chart = (
pyg.component(df)
.scatter()
.encode(
x="ad_spend",
y="revenue",
color="channel",
size="sum(orders)",
)
)対応チャンネル:
encode(
x="",
y="",
color="",
opacity="",
size="",
shape="",
radius="",
theta="",
longitude="",
latitude="",
geoid="",
details="",
text="",
)x と y にはフィールドのリストも渡せます。sum(revenue) のような集計式は Graphic Walker の集計モデルを使います。
Layout と mode
.layout(...) でチャートサイズと layout オプションを設定します。
chart = chart.layout(mode="fixed", width=640, height=360)レンダリングモードは次のメソッドで切り替えます。
| メソッド | 用途 |
|---|---|
.profiling() | profiling / table モードをレンダリングします。 |
.explorer() | 現在のチャート状態を囲むフル explorer をレンダリングします。 |
.copy() | チャートチェーンを分岐する前に明示的なコピーを作成します。 |
.to_html() | 現在の component の HTML を返します。 |
静的エクスポートの制限
Component .to_html() は静的 HTML です。Explorer と profiling の component エクスポートはライブ kernel / cloud computation を拒否します。
# Use browser computation when exporting component HTML.
component = pyg.component(df, computation="browser")
html = component.explorer().to_html()チャートに kernel や cloud computation が必要な場合は、ノートブック API、Streamlit、Gradio などのライブアダプターを使います。