def __init__(self, widget, **kwargs): from ipywidgets import embed, Widget super().__init__(**kwargs) spec = widget.get_view_spec() state = Widget.get_manager_state(widgets=[]) state["state"] = embed.dependency_state([widget], drop_defaults=True) self.bundle = dict(spec=spec, state=state)
def test_state_schema(): from ipywidgets.widgets import IntSlider, Widget import json import jsonschema s = IntSlider() state = Widget.get_manager_state(drop_defaults=True) with open( os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../', 'state.schema.json')) as f: schema = json.load(f) jsonschema.validate(state, schema)
def ipywidget_state(widgets): from jinja2 import Markup from ipywidgets import embed as wembed drop_defaults = True state = wembed.dependency_state(widgets, drop_defaults=drop_defaults) from ipywidgets import Widget json_data = Widget.get_manager_state(widgets=[]) json_data['state'] = state json_data_str = json.dumps(json_data, indent=' ') snippet = wembed.snippet_template.format(load='', widget_views='', json_data=json_data_str) return Markup(snippet)
def add_widget_state(app, pagename, templatename, context, doctree): if 'body' in context and Widget.widgets: state_spec = json.dumps(Widget.get_manager_state(drop_defaults=True)) Widget.widgets = {} context[ 'body'] += '<script type="application/vnd.jupyter.widget-state+json">' + state_spec + '</script>'