jupyter-widgets / ipywidgets

Interactive Widgets for the Jupyter Notebook
https://ipywidgets.readthedocs.io
BSD 3-Clause "New" or "Revised" License
3.13k stars 949 forks source link

VariableInspector example not working out of the box #2994

Open mocquin opened 3 years ago

mocquin commented 3 years ago

Hello there, I tried to run the variable inspector example widget in a jupyterlab notebook :

import ipywidgets as widgets # Loads the Widget framework.
from IPython.core.magics.namespace import NamespaceMagics # Used to query namespace.

# For this example, hide these names, just to avoid polluting the namespace further
get_ipython().user_ns_hidden['widgets'] = widgets
get_ipython().user_ns_hidden['NamespaceMagics'] = NamespaceMagics

class VariableInspectorWindow(object):
    instance = None

    def __init__(self, ipython):
        """Public constructor."""
        if VariableInspectorWindow.instance is not None:
            raise Exception("""Only one instance of the Variable Inspector can exist at a 
                time.  Call close() on the active instance before creating a new instance.
                If you have lost the handle to the active instance, you can re-obtain it
                via `VariableInspectorWindow.instance`.""")

        VariableInspectorWindow.instance = self
        self.closed = False
        self.namespace = NamespaceMagics()
        self.namespace.shell = ipython.kernel.shell

        self._box = widgets.Box()
        self._box.layout.overflow = 'visible scroll'
        self._table = widgets.HTML(value = 'Not hooked')
        self._box.children = [self._table]

        self._ipython = ipython
        self._ipython.events.register('post_run_cell', self._fill)

    def close(self):
        """Close and remove hooks."""
        if not self.closed:
            self._ipython.events.unregister('post_run_cell', self._fill)
            self._box.close()
            self.closed = True
            VariableInspectorWindow.instance = None

    def _fill(self):
        """Fill self with variable information."""
        values = self.namespace.who_ls()
        self._table.value = '<div class="rendered_html jp-RenderedHTMLCommon"><table><thead><tr><th>Name</th><th>Type</th><th>Value</th></tr></thead><tr><td>' + \
            '</td></tr><tr><td>'.join(['{0}</td><td>{1}</td><td>{2}'.format(v, type(eval(v)).__name__, str(eval(v))) for v in values]) + \
            '</td></tr></table></div>'

    def _repr_mimebundle_(self, **kwargs):
        return self._box._repr_mimebundle_(**kwargs)

a = 5
b = 3.0
c = a * b
d = "String"

del b

inspector = VariableInspectorWindow(get_ipython())
inspector

which raises :

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/opt/anaconda3/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj, include, exclude)
    968 
    969             if method is not None:
--> 970                 return method(include=include, exclude=exclude)
    971             return None
    972         else:

<ipython-input-130-40febb36ba04> in _repr_mimebundle_(self, **kwargs)
     46 
     47     def _repr_mimebundle_(self, **kwargs):
---> 48         return self._box._repr_mimebundle_(**kwargs)
     49 
     50 

AttributeError: 'Box' object has no attribute '_repr_mimebundle_'

Got it working by replacing

def _repr_mimebundle_(self, **kwargs):
        return self._box._repr_mimebundle_(**kwargs)

with

def _ipython_display_(self):
        return self._box._ipython_display_()

I guess something's wrong with my setup ?

python 3.7.4
IPython 7.18.1
ipywidgets 7.5.1

Cheers

willingc commented 3 years ago

@mocquin Try updating to 7.6.3. You can try it on binder too: Binder:7.x

Screen Shot 2021-02-07 at 9 43 27 PM
ianhi commented 3 years ago

re-opening: On binder on master it partially works. The variable inspection works, but inspector.close() throws an error: image