Skip to content

Commit

Permalink
Add debug property and use this to enable printing VegaFusion messages
Browse files Browse the repository at this point in the history
(these will end up in the JupyterLab console)
  • Loading branch information
jonmmease committed Dec 13, 2023
1 parent 8689751 commit 893bc59
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion altair/jupyter/jupyter_chart.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import anywidget
import traitlets
import pathlib
Expand Down Expand Up @@ -107,6 +108,7 @@ class JupyterChart(anywidget.AnyWidget):
debounce_wait = traitlets.Float(default_value=10).tag(sync=True)
max_wait = traitlets.Bool(default_value=True).tag(sync=True)
local_tz = traitlets.Unicode(default_value=None, allow_none=True).tag(sync=True)
debug = traitlets.Bool(default_value=False)

# Internal selection traitlets
_selection_types = traitlets.Dict()
Expand All @@ -126,6 +128,7 @@ def __init__(
chart: TopLevelSpec,
debounce_wait: int = 10,
max_wait: bool = True,
debug: bool = False,
**kwargs: Any,
):
"""
Expand All @@ -143,11 +146,17 @@ def __init__(
If True (default), updates will be sent from the client to the kernel every debounce_wait
milliseconds even if there are ongoing chart interactions. If False, updates will not be
sent until chart interactions have completed.
debug: bool
If True, debug messages will be printed
"""
self.params = Params({})
self.selections = Selections({})
super().__init__(
chart=chart, debounce_wait=debounce_wait, max_wait=max_wait, **kwargs
chart=chart,
debounce_wait=debounce_wait,
max_wait=max_wait,
debug=debug,
**kwargs,
)

@traitlets.observe("chart")
Expand Down Expand Up @@ -263,7 +272,13 @@ def _init_with_vegafusion(self, local_tz: str):

# Callback to update chart state and send updates back to client
def on_js_to_py_updates(change):
if self.debug:
updates_str = json.dumps(change["new"], indent=2)
print(f"JavaScript to Python VegaFusion updates:\n {updates_str}")
updates = self._chart_state.update(change["new"])
if self.debug:
updates_str = json.dumps(updates, indent=2)
print(f"Python to JavaScript VegaFusion updates:\n {updates_str}")
self._py_to_js_updates = updates

self.observe(on_js_to_py_updates, ["_js_to_py_updates"])
Expand Down

0 comments on commit 893bc59

Please sign in to comment.