Skip to content

Commit a41c4e8

Browse files
committed
Add checks for numpy
import might want to go into _getshapeof
1 parent 403c766 commit a41c4e8

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/jupyter_contrib_nbextensions/nbextensions/varInspector/main.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,20 @@ function html_table(jsonVars) {
130130
var varList = JSON.parse(String(jsonVars))
131131

132132
var shape_str;
133-
if(kernelLanguage === "python")
134-
{
133+
var has_shape = false;
134+
if (varList.some(list_var => "varShape" in list_var )) { //if any of them have a shape
135135
shape_str = '<th >Shape</th>';
136+
has_shape = true;
136137
}
137138
var beg_table = '<div class=\"inspector\"><table class=\"table fixed table-condensed table-nonfluid \"><col /> \
138139
<col /><col /><thead><tr><th >X</th><th >Name</th><th >Type</th><th >Size</th>' + shape_str + '<th >Value</th></tr></thead><tr><td> \
139140
</td></tr>'
140141
var nb_vars = varList.length;
141142
var shape_col_str;
142143
for (var i = 0; i < nb_vars; i++) {
143-
if(kernelLanguage === "python")
144-
{
144+
if (has_shape) {
145145
shape_col_str = '</td><td>' + varList[i].varShape + '</td><td>';
146-
}else
147-
{
146+
} else {
148147
shape_col_str = '';
149148
}
150149
beg_table = beg_table +

src/jupyter_contrib_nbextensions/nbextensions/varInspector/var_list.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33

44
from IPython import get_ipython
55
from IPython.core.magics.namespace import NamespaceMagics
6-
import numpy as np
76
_nms = NamespaceMagics()
87
_Jupyter = get_ipython()
98
_nms.shell = _Jupyter.kernel.shell
109

10+
has_numpy = False
11+
try:
12+
import numpy as np
13+
has_numpy = True
14+
except ImportError:
15+
has_numpy = False
1116

1217
def _getsizeof(x):
1318
# return the size of variable x. Amended version of sys.getsizeof
@@ -22,16 +27,21 @@ def _getsizeof(x):
2227
def _getshapeof(x):
2328
#returns the shape of x if it has one
2429
#returns None otherwise - might want to return an empty string for an empty collum
25-
try:
26-
return x.shape
27-
except AttributeError: #x does not have a shape
28-
return None
30+
global has_numpy
31+
if has_numpy:
32+
try:
33+
return x.shape
34+
except AttributeError: #x does not have a shape
35+
return None
2936

3037
def var_dic_list():
3138
types_to_exclude = ['module', 'function', 'builtin_function_or_method',
3239
'instance', '_Feature', 'type', 'ufunc']
3340
values = _nms.who_ls()
34-
vardic = [{'varName': v, 'varType': type(eval(v)).__name__, 'varSize': str(_getsizeof(eval(v))), 'varShape': str(_getshapeof(eval(v))), 'varContent': str(eval(v))[:200]} # noqa
41+
vardic = [{'varName': v, 'varType': type(eval(v)).__name__, 'varSize': str(_getsizeof(eval(v))), 'varContent': str(eval(v))[:200]} # noqa
42+
if has_numpy:
43+
vardic['varSize'] = str(eval(_getshapeof(v)))
44+
3545
for v in values if (v not in ['_html', '_nms', 'NamespaceMagics', '_Jupyter']) & (type(eval(v)).__name__ not in types_to_exclude)] # noqa
3646
return json.dumps(vardic)
3747

0 commit comments

Comments
 (0)