Skip to content
PYGWALKER
Référence de l'API
Fichiers spec

Fichiers spec PyGWalker

Une spec PyGWalker stocke l'état des graphiques. Utilisez spec_path pour les fichiers locaux que PyGWalker doit charger ou enregistrer, et utilisez spec pour le JSON inline, les ID de configuration ou les specs distantes.

import pygwalker as pyg
 
walker = pyg.walk(
    df,
    spec_path="./gw_config.json",
    computation="browser",
)

spec_path ou spec

OptionÀ utiliser pourExemple
spec_pathFichiers locaux d'état des graphiquesspec_path="./gw_config.json"
specJSON inline, ID de configuration ou URL de spec distantespec='[{"name":"Chart 1", ...}]'

Pour les nouveaux exemples avec fichiers locaux, préférez spec_path. Cela rend la persistance de fichier explicite et évite toute ambiguïté avec le JSON inline ou les specs distantes.

Enregistrer et réutiliser l'état des graphiques

Utilisez le mode spec lecture-écriture lorsque l'adaptateur expose l'enregistrement.

import pygwalker as pyg
 
walker = pyg.Walker(
    df,
    spec_path="./gw_config.json",
    spec_io_mode="rw",
    computation="kernel",
)
 
walker.show()

Streamlit et Gradio prennent aussi en charge spec_io_mode :

from pygwalker.api.streamlit import StreamlitRenderer
 
renderer = StreamlitRenderer(
    df,
    spec_path="./gw_config.json",
    spec_io_mode="rw",
    computation="kernel",
)
renderer.explorer()

Rendre des graphiques enregistrés

Utilisez render lorsque vous voulez des graphiques enregistrés sans l'explorateur complet.

import pygwalker as pyg
 
pyg.render(df, spec_path="./gw_config.json", computation="browser")

Utilisez to_render_html pour un export renderer statique.

from pygwalker.api.html import to_render_html
 
html = to_render_html(df, spec_path="./gw_config.json", computation="browser")

Migrer d'anciennes specs

pygwalker.spec.migrate met à jour une spec PyGWalker ou Graphic Walker enregistrée vers la forme de schéma actuelle.

import json
import pygwalker as pyg
 
migrated = pyg.spec.migrate("./old_gw_config.json")
 
with open("./gw_config.json", "w", encoding="utf-8") as f:
    json.dump(migrated, f, ensure_ascii=False, indent=2)

Signature :

pygwalker.spec.migrate(spec, *, version=None) -> dict

spec peut être :

EntréeAcceptée
Objet Python chargédict ou list
Chaîne JSONOui
Chemin de fichier local existantOui

Si une chaîne ressemble à du JSON invalide ou si le chemin de fichier n'existe pas, migrate lève ValueError.

Pièges courants

PiègeCorrection
Passer un fichier local via spec dans du nouveau codeUtilisez spec_path.
S'attendre à ce que le HTML statique enregistre les modifications dans spec_pathUtilisez un backend notebook/app actif avec support d'enregistrement.
Passer spec_path à un adaptateur qui a déjà reçu un WalkerPlacez le chemin de spec sur le Walker d'origine.
Migrer une URL de spec distante avec spec.migrateChargez d'abord l'objet spec, puis passez l'objet ou la chaîne JSON.

Guides associés