Python Jupyter Notebook 中的变量资源管理器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37718907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Variable Explorer in Jupyter Notebook
提问by ébe Isaac
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.
Jupyter (IPython) 中是否有像 Spyder 一样的变量资源管理器?每次运行测试代码时都必须一直打印变量列表,这非常不舒服。
Has this feature been implemented yet? If so, how to enable it?
这个功能已经实现了吗?如果是这样,如何启用它?
回答by James Draper
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.
这是一个关于如何制作自己的变量检查器的笔记本。我认为它是在 jupyter notebook 被称为 ipython notebook 时写回的,但它适用于最新版本。
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;
%%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
日期:2017 年 5 月 17 日
@jfberchercreated a nbextension variable inspector. The source code can be seen here jupyter_contrib_nbextensions. For more information see the docs.
@jfbercher创建了一个 nbextension 变量检查器。源代码可以在这里看到jupyter_contrib_nbextensions。有关更多信息,请参阅文档。
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;
这是一个屏幕截图;
回答by Amin Alaee
This might help you, though it's not exactly what Spyder offers and is much simpler:
这可能对您有所帮助,尽管这不是 Spyder 提供的,而且要简单得多:
To get a list of all currently defined variables, run who:
要获取所有当前定义的变量的列表,请运行who:
In [1]: foo = 'bar'
In [2]: who
foo
For more detail, run whos:
有关更多详细信息,请运行whos:
In [3]: whos
Variable Type Data/Info
----------------------------
foo str bar
For a complete list of built-in functions see Magic Commands
有关内置函数的完整列表,请参阅Magic Commands
回答by Simon
If you use Jupyter Notebooks within Jupyter Labthere has been a lot of discussion about implementing a variable explorer/inspector. You can follow the issue here
如果您在Jupyter Lab 中使用 Jupyter Notebooks,那么已经有很多关于实现变量资源管理器/检查器的讨论。你可以在这里关注这个问题
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
截至目前,正在开发中的 Jupyter Lab 扩展实现了类似 Spyder 的变量资源管理器。它基于 James 在他的回答中提到的 notebook 扩展。您可以在此处找到实验室扩展(带有安装说明):https: //github.com/lckr/jupyterlab-variableInspector