gpt4 book ai didi

python - Jupyter Notebook 中的变量资源管理器

转载 作者:IT老高 更新时间:2023-10-28 20:23:47 24 4
gpt4 key购买 nike

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

这个功能已经实现了吗?如果可以,如何开启?

最佳答案

更新

向下滚动到标记为更新的部分,以了解更简单的方法。

旧答案

这是一个关于如何制作自己的笔记本Variable Inspector .我认为它是在 jupyter notebook 被称为 ipython 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._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;

%%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 日

@jfbercher创建了一个 nbextension 变量检查器。源码可以看这里jupyter_contrib_nbextensions .如需更多信息,请参阅 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

这是一个屏幕截图;

enter image description here

关于python - Jupyter Notebook 中的变量资源管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37718907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com