Jupyter Notebook中的变量浏览器 [英] Variable Explorer in Jupyter Notebook

查看:177
本文介绍了Jupyter Notebook中的变量浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jupyter(IPython)中是否有像Spyder中一样的变量资源管理器?每次我运行测试代码时,每次都要打印变量列表是非常不舒服的.

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?

推荐答案

UPDATE

向下滚动到标记为update的部分,以减少复杂的方法.

UPDATE

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

这是一个有关如何制作自己的

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_()

内联运行以下内容:

inspector = VariableInspectorWindow(get_ipython())
inspector

将其设为JavaScript弹出框;

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();

更新

日期:2017年5月17日

UPDATE

Date: May 17 2017

@jfbercher 创建了一个nbextension变量检查器.可以在 jupyter_contrib_nbextensions 中查看源代码.有关更多信息,请参见文档.

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

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

虚拟环境

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

启用

jupyter nbextension enable varInspector/main

这是一个屏幕截图;

这篇关于Jupyter Notebook中的变量浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆