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 pour | Exemple |
|---|---|---|
spec_path | Fichiers locaux d'état des graphiques | spec_path="./gw_config.json" |
spec | JSON inline, ID de configuration ou URL de spec distante | spec='[{"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) -> dictspec peut être :
| Entrée | Acceptée |
|---|---|
| Objet Python chargé | dict ou list |
| Chaîne JSON | Oui |
| Chemin de fichier local existant | Oui |
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ège | Correction |
|---|---|
Passer un fichier local via spec dans du nouveau code | Utilisez spec_path. |
S'attendre à ce que le HTML statique enregistre les modifications dans spec_path | Utilisez un backend notebook/app actif avec support d'enregistrement. |
Passer spec_path à un adaptateur qui a déjà reçu un Walker | Placez le chemin de spec sur le Walker d'origine. |
Migrer une URL de spec distante avec spec.migrate | Chargez d'abord l'objet spec, puis passez l'objet ou la chaîne JSON. |