Variable Explorer in Jupyter Notebook
Asked Answered
A

7

111

Is there a variable explorer in Jupyter (IPython) like in Spyder? It is very uncomfortable having to print the list of variables all the time each time I run through the test code.

Has this feature been implemented yet? If so, how to enable it?

Aquiver answered 9/6, 2016 at 6:59 Comment(0)
I
115

UPDATE

Scroll down to the section labeled update for a much less convoluted method.

OLD ANSWER

Here is a notebook on how to make your own Variable Inspector. I think it was written back when jupyter notebook was called ipython notebook but it works on the latest version.

I'll post the code below just in case the link breaks.

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._dom_classes = ['inspector']
    self._box.background_color = '#fff'
    self._box.border_color = '#ccc'
    self._box.border_width = 1
    self._box.border_radius = 5

    self._modal_body = widgets.VBox()
    self._modal_body.overflow_y = 'scroll'

    self._modal_body_label = widgets.HTML(value = 'Not hooked')
    self._modal_body.children = [self._modal_body_label]

    self._box.children = [
        self._modal_body, 
    ]

    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._modal_body_label.value = '<table class="table table-bordered table-striped"><tr><th>Name</th><th>Type</th><th>Value</th></tr><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>'

def _ipython_display_(self):
    """Called when display() or pyout is used to display the Variable 
    Inspector."""
    self._box._ipython_display_()

Run inline with the following:

inspector = VariableInspectorWindow(get_ipython())
inspector

Make it a javascript pop out;

%%javascript
$('div.inspector')
    .detach()
    .prependTo($('body'))
    .css({
        'z-index': 999, 
        position: 'fixed',
        'box-shadow': '5px 5px 12px -3px black',
        opacity: 0.9
    })
    .draggable();

UPDATE

Date: May 17 2017

@jfbercher created a nbextension variable inspector. The source code can be seen here jupyter_contrib_nbextensions. For more information see the docs.

Install

User

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

Virtual environment

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --sys-prefix

Enable

jupyter nbextension enable varInspector/main

Here's a screen-shot;

enter image description here

Indescribable answered 27/12, 2016 at 2:13 Comment(6)
Great stuff, would be nice if you posted a snapshot here of how it looks. I believe tht would help spread the adoption of you nice extension!Jaine
@Jaine right on, I just added it. Also I just want to be clear I didn't write the extension someone else did. I'll try figure out who and post an attribution later.Indescribable
Following instructions after "Update May 17 2017" gives you an extra icon that looks like a gun sight (-:-) click on this and the Variable Explorer pops up.Nymphet
@Nymphet there is a little a button in the toolbar off to the right it looks like circle sort of click that if it's there.Indescribable
@JamesDraper I can only see the first few values of say a list and nothing at all for objects. Is there a way to expand this, so I can drill down through objects to monitor particular properties?Nymphet
@Nymphet This extension calls str(eval(obj_name)), so if you are using custom class you can add property monitoring to __str__ dunder method. If it's not, you can wrap variable with second "Monitoring" class. If you want specifics, create a new question, because there is not enough characters left here for the exact code.Ong
I
45

This might help you, though it's not exactly what Spyder offers and is much simpler:

To get a list of all currently defined variables, run who :

In [1]: foo = 'bar'

In [2]: who
foo

For more detail, run whos:

In [3]: whos
Variable   Type    Data/Info
----------------------------
foo        str     bar

For a complete list of built-in functions see Magic Commands

Incisor answered 9/6, 2016 at 7:32 Comment(2)
same as matlab/octave :PRheumatism
In web version those functions doesn't work as for 'who' and 'whos' I get: "Error in eval(expr, envir, enclos): object 'whos' not found Traceback:"Cottbus
A
43

If you use Jupyter Notebooks within Jupyter Lab there has been a lot of discussion about implementing a variable explorer/inspector. You can follow the issue here

As of right now there is one Jupyter Lab extension in the works that implements a Spyder-like variable explorer. It is based on the notebook extension that James mentioned in his answer. You can find the lab extension (with installation instructions) here: https://github.com/lckr/jupyterlab-variableInspector

enter image description here

Agentive answered 23/10, 2018 at 6:48 Comment(1)
If you are unsure how to install extensions in JupyterLab, follow these steps: jupyterlab.readthedocs.io/en/stable/user/extensions.htmlDiseuse
A
9

If you are using Visual Studio Code, it's already there:

enter image description here

Aquila answered 4/7, 2020 at 5:43 Comment(1)
Thanks. However, this icon is not in my vscode. Did you install any extension?Musk
M
1

Another solution would be to connect the spyder-console to the running kernel

On any platform it would be:

  1. In jupyter: run the following (from this source):
from jupyter_client import find_connection_file
print(find_connection_file()) #this will show which json-file is associated to your jupyter

it should give you something like: ~/.local/share/jupyter/runtime/kernel-<SOME-ID>.json'

  1. In your spyder console, right-click and select "Connect to an existing kernel", browse to that file you found previously.
    • tick the "Save connection settings" to make it easier to find it next time
    • un-tick the "This is a remote kernel (via SSH)" (except if you know what you're doing)
  2. You can then browse the variable with spyder that are in your jupyter instance

Perks of this method is that you don't need to install an aditionnal package you just need spyder & jupyter.

EDIT: I was too optimistic, but it seems that spyder also has a problem,I can use the variables in the console, but it doesn't show the variable in the actual GUI/browser. I'm leaving the answer in the hopes that this will be fixed in the future. (it may also work depending on the version you installed). Maybe a solution would be to connect the jupyter to an existing kernel, but I haven't been able to make it work. Any help is welcome !

Moil answered 3/11, 2021 at 15:13 Comment(0)
L
1

Just to piggyback off of the accepted answer, the best way to install in a VE is to run the following:

import sys
!{sys.executable} -m pip install jupyter_contrib_nbextensions
Leveroni answered 5/11, 2021 at 14:6 Comment(0)
A
0

A more manual but alternative way which can be complementary it to use https://github.com/google/etils

!pip install etils[ecolab]

Then

from etils import ecolab

ecolab.inspect(globals())

This display an interactive visualisation to recursively inspect arbitrary python objects (similarly to Chrome JS console).

For example:

inspect screenshot

There's also a ecolab.auto_inspect() which add an inspect button on all cells output:

screenshot inspect

Currently this only work on Google Colab but I'm working on adding support for arbitrary jupyter notebook (contributions welcome).

Ase answered 22/3, 2023 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.