def test_compressed_to_compressed_json(): data = np.zeros((4, 3), dtype=np.float32) dummy = Widget() dummy.compression_level = 6 json_data = array_to_compressed_json(data, dummy) assert tuple(sorted(json_data.keys())) == ('compressed_buffer', 'dtype', 'shape') assert json_data['shape'] == (4, 3) assert json_data['dtype'] == str(data.dtype) # Test that decompress doesn't raise: comp = json_data['compressed_buffer'] if six.PY2: comp = comp.tobytes() zlib.decompress(comp)
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_union_from_json_correct_widget_data(): dummy = Widget() json_data = widget_serialization['to_json'](dummy, None) data = data_union_from_json(json_data, None) assert data == dummy
def _link_widget_to_model( self, name: str, widget: widgets.Widget, model: traitlets.HasTraits) -> Iterator[traitlets.link]: """Link a single widget to a model. Respect read-only relationships, and delegate linking to widget if it implements the necessary interface. :param name: name of model field :param widget: widget instance :param model: model instance :return: """ # Allow widget to handle linking if hasattr(widget, "link_to_model"): yield from widget.link_to_model(model, self, name) return # Infer read-only state from initial widget disabled state is_read_only = getattr(widget, "disabled", False) link_factory = widgets.dlink if is_read_only else widgets.link # Allow widget to be disabled when container is disabled if hasattr(widget, "disabled") and not is_read_only: yield widgets.dlink((self, "disabled"), (widget, "disabled")) yield link_factory((model, name), (widget, "value"))
def get_ui( plot_wdgts: dict, tile_wdgts: dict, button_tiles: widgets.Widget, button_selection: widgets.Widget, button_clear: widgets.Widget, ): # Change layout: plot_wdgts['column'].layout = widgets.Layout(width='100%') plot_wdgts['bins'].layout = widgets.Layout(width='100%') plot_wdgts['x_range'].layout = widgets.Layout(width='100%') plot_wdgts['log_y'].layout = widgets.Layout(width='10%') tile_wdgts['nrows'].layout = widgets.Layout(width='100%') tile_wdgts['ncols'].layout = widgets.Layout(width='100%') tile_wdgts['px'].layout = widgets.Layout(width='100%') button_tiles.layout = widgets.Layout(width='100%') button_selection.layout = widgets.Layout(width='20%') button_clear.layout = widgets.Layout(width='20%') if plot_wdgts['dropdown'] is None: top = HBox([ plot_wdgts['column'], plot_wdgts['bins'] ]) del plot_wdgts['dropdown'] else: plot_wdgts['dropdown'].layout = widgets.Layout(width='50%') top = HBox([ plot_wdgts['dropdown'], plot_wdgts['column'], plot_wdgts['bins'] ]) middle = HBox([ plot_wdgts['x_range'], plot_wdgts['log_y'], button_selection, button_clear, ]) bottom_ui = HBox([ button_tiles, tile_wdgts['nrows'], tile_wdgts['ncols'], tile_wdgts['px'], ]) top_ui = VBox([top, middle]) return top_ui, bottom_ui
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 test_widgetsel_invalid_change(): w = WidgetDropdown(dict(A=A, B=B, C=C)) with pytest.raises(TraitError): w.value = Widget()
w.value = "B" assert w.value == "B" def test_stringsel_invalid_change(): w = StringDropdown(("A", "B", "C")) with pytest.raises(TraitError): w.value = "D" def test_widgetsel_creation_blank(): with pytest.raises(TypeError): WidgetDropdown() A = Widget() B = Widget() C = Widget() def test_widgetsel_creation(): # Works after python 3.7: # w = WidgetDropdown(dict(A=A, B=B, C=C)) w = WidgetDropdown(OrderedDict([("A", A), ("B", B), ("C", C)])) assert w.value == A def test_widgetsel_valid_change(): w = WidgetDropdown(dict(A=A, B=B, C=C)) w.value = B assert w.value == B
def _ipython_display_(self, **kwargs): return Widget._ipython_display_(self, **kwargs)
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>'