Ejemplo n.º 1
0
def test_ipywidget():
    pane = Str('A')
    widget = ipywidget(pane)

    assert widget._view_count == 0
    assert len(pane._models) == 1

    init_id = list(pane._models)[0]

    widget._view_count = 1

    assert widget._view_count == 1
    assert init_id in pane._models

    widget._view_count = 0

    assert len(pane._models) == 0

    widget._view_count = 1

    assert len(pane._models) == 1
    prev_id = list(pane._models)[0]

    widget.notify_change({'new': 1, 'old': 1, 'name': '_view_count',
                          'type': 'change', 'model': widget})
    assert prev_id in pane._models
    assert len(pane._models) == 1

    widget._view_count = 2

    assert prev_id in pane._models
    assert len(pane._models) == 1
Ejemplo n.º 2
0
 def _render_ipywidget(self, plot):
     # Handle rendering object as ipywidget
     widget = ipywidget(plot, combine_events=True)
     if hasattr(widget, '_repr_mimebundle_'):
         return widget._repr_mimebundle()
     plaintext = repr(widget)
     if len(plaintext) > 110:
         plaintext = plaintext[:110] + '…'
     data = {'text/plain': plaintext}
     if widget._view_name is not None:
         data['application/vnd.jupyter.widget-view+json'] = {
             'version_major': 2,
             'version_minor': 0,
             'model_id': widget._model_id
         }
     if config.comms == 'vscode':
         # Unfortunately VSCode does not yet handle _repr_mimebundle_
         from IPython.display import display
         display(data, raw=True)
         return {'text/html': '<div style="display: none"></div>'}, {}
     return data, {}
Ejemplo n.º 3
0
    def components(self, obj, fmt=None, comm=True, **kwargs):
        """
        Returns data and metadata dictionaries containing HTML and JS
        components to include render in app, notebook, or standalone
        document.
        """
        if isinstance(obj, Plot):
            plot = obj
        else:
            plot, fmt = self._validate(obj, fmt)

        data, metadata = {}, {}
        if isinstance(plot, Viewable):
            registry = list(Stream.registry.items())
            objects = plot.object.traverse(lambda x: x)
            dynamic, streams = False, False
            for source in objects:
                dynamic |= isinstance(source, DynamicMap)
                streams |= any(
                    src is source or (src._plot_id is not None and src._plot_id == source._plot_id)
                    for src, streams in registry for s in streams
                )
            embed = (not (dynamic or streams or self.widget_mode == 'live') or config.embed)

            # This part should be factored out in Panel and then imported
            # here for HoloViews 2.0, which will be able to require a
            # recent Panel version.
            if embed or config.comms == 'default':
                comm = self.comm_manager.get_server_comm() if comm else None
                doc = Document()
                with config.set(embed=embed):
                    model = plot.layout._render_model(doc, comm)
                if embed:
                    return render_model(model, comm)
                args = (model, doc, comm)
                if panel_version > '0.9.3':
                    from panel.models.comm_manager import CommManager
                    ref = model.ref['id']
                    manager = CommManager(comm_id=comm.id, plot_id=ref)
                    client_comm = self.comm_manager.get_client_comm(
                        on_msg=partial(plot._on_msg, ref, manager),
                        on_error=partial(plot._on_error, ref),
                        on_stdout=partial(plot._on_stdout, ref)
                    )
                    manager.client_comm_id = client_comm.id
                    args = args + (manager,)
                return render_mimebundle(*args)

            # Handle rendering object as ipywidget
            widget = ipywidget(plot, combine_events=True)
            if hasattr(widget, '_repr_mimebundle_'):
                return widget._repr_mimebundle()
            plaintext = repr(widget)
            if len(plaintext) > 110:
                plaintext = plaintext[:110] + '…'
            data = {
                'text/plain': plaintext,
            }
            if widget._view_name is not None:
                data['application/vnd.jupyter.widget-view+json'] = {
                    'version_major': 2,
                    'version_minor': 0,
                    'model_id': widget._model_id
                }
            if config.comms == 'vscode':
                # Unfortunately VSCode does not yet handle _repr_mimebundle_
                from IPython.display import display
                display(data, raw=True)
                return {'text/html': '<div style="display: none"></div>'}, {}
            return data, {}
        else:
            html = self._figure_data(plot, fmt, as_script=True, **kwargs)
        data['text/html'] = html

        return (data, {MIME_TYPES['jlab-hv-exec']: metadata})