Jupyter笔记本中的变量资源管理器

Ébe*_*aac 61 python facilities jupyter-notebook

Jupyter(IPython)中的变量浏览器是否像Spyder一样?每次运行测试代码时,必须始终打印变量列表,这是非常不舒服的.

这个功能已经实现了吗?如果是这样,如何启用它?

Jam*_*per 73

UPDATE

向下滚动到标记为"更新"的部分,以获得更少复杂的方法.

老答复

这是一个关于如何制作自己的Variable Inspector的笔记本.我认为它是在jupyter笔记本被称为ipython笔记本时写回来的,但它适用于最新版本.

我将发布以下代码,以防链接中断.

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 Code Online (Sandbox Code Playgroud)

使用以下内容运行内联:

inspector = VariableInspectorWindow(get_ipython())
inspector
Run Code Online (Sandbox Code Playgroud)

让它弹出一个javascript;

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

UPDATE

日期:2017年5月17日

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

安装

用户

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
Run Code Online (Sandbox Code Playgroud)

虚拟环境

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --sys-prefix
Run Code Online (Sandbox Code Playgroud)

启用

jupyter nbextension enable varInspector/main
Run Code Online (Sandbox Code Playgroud)

这是一个屏幕截图;

在此输入图像描述

  • 按照“更新2017年5月17日”之后的说明,您会看到一个额外的图标,看起来像是瞄准器(-:-),单击此图标,然后弹出变量浏览器。 (2认同)
  • @Robino在右侧的工具栏中有一个小按钮,看起来像是一圈单击,如果有的话。 (2认同)

Ami*_*aee 26

这可能对你有帮助,虽然它不是Spyder提供的并且更简单:

要获取所有当前定义的变量的列表,请运行who:

In [1]: foo = 'bar'

In [2]: who
foo
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请运行whos:

In [3]: whos
Variable   Type    Data/Info
----------------------------
foo        str     bar
Run Code Online (Sandbox Code Playgroud)

有关内置函数的完整列表,请参阅Magic Commands

  • 与matlab / octave相同:P (2认同)

Sim*_*mon 18

如果您在Jupyter实验室中使用Jupyter笔记本,那么有很多关于实现变量资源管理器/检查器的讨论.您可以在此处关注此问题

截至目前,有一个Jupyter Lab扩展正在实现一个类似Spyder的变量浏览器.它基于James在答案中提到的笔记本扩展.您可以在此处找到实验室扩展(包含安装说明):https://github.com/lckr/jupyterlab-variableInspector

在此输入图像描述

  • 正是我想要的 (2认同)
  • 如果您不确定如何在 JupyterLab 中安装扩展,请按照以下步骤操作:https://jupyterlab.readthedocs.io/en/stable/user/extensions.html (2认同)

Ala*_*bra 5

如果您使用的是 Visual Studio Code,它已经存在:

在此处输入图片说明