Skip to content

Commit 9bfe025

Browse files
committed
Hide empty shape column
Incoperates changes form the code review changed one C-style for to a modern forEach because it looks better and if Array.some is supported then so is forEach
1 parent bb02b6a commit 9bfe025

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

src/jupyter_contrib_nbextensions/nbextensions/varInspector/main.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,34 +129,29 @@ function html_table(jsonVars) {
129129
var kernel_config = cfg.kernels_config[kernelLanguage];
130130
var varList = JSON.parse(String(jsonVars))
131131

132-
var shape_str;
133-
var has_shape = false;
134-
if (varList.some(list_var => "varShape" in list_var )) { //if any of them have a shape
132+
var shape_str = '';
133+
if (varList.some(listVar => "varShape" in listVar && listVar.varShape !== '')) { //if any of them have a shape
135134
shape_str = '<th >Shape</th>';
136-
has_shape = true;
137135
}
138136
var beg_table = '<div class=\"inspector\"><table class=\"table fixed table-condensed table-nonfluid \"><col /> \
139137
<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> \
140-
</td></tr>'
141-
var nb_vars = varList.length;
142-
var shape_col_str;
143-
for (var i = 0; i < nb_vars; i++) {
144-
if (has_shape) {
145-
shape_col_str = '</td><td>' + varList[i].varShape + '</td><td>';
146-
} else {
147-
shape_col_str = '';
138+
</td></tr>';
139+
varList.forEach(listVar => {
140+
var shape_col_str = '';
141+
if ('varShape' in listVar) {
142+
shape_col_str = '</td><td>' + listVar.varShape + '</td><td>';
148143
}
149-
beg_table = beg_table +
144+
beg_table +=
150145
'<tr><td><a href=\"#\" onClick=\"Jupyter.notebook.kernel.execute(\'' +
151-
kernel_config.delete_cmd_prefix + varList[i].varName + kernel_config.delete_cmd_postfix + '\'' + '); ' +
146+
kernel_config.delete_cmd_prefix + listVar.varName + kernel_config.delete_cmd_postfix + '\'' + '); ' +
152147
'Jupyter.notebook.events.trigger(\'varRefresh\'); \">x</a></td>' +
153-
'<td>' + _trunc(varList[i].varName, cfg.cols.lenName) + '</td><td>' + _trunc(varList[i].varType, cfg.cols.lenType) +
154-
'</td><td>' + varList[i].varSize + shape_col_str + _trunc(varList[i].varContent, cfg.cols.lenVar) +
155-
'</td></tr>'
148+
'<td>' + _trunc(listVar.varName, cfg.cols.lenName) + '</td><td>' + _trunc(listVar.varType, cfg.cols.lenType) +
149+
'</td><td>' + listVar.varSize + shape_col_str + _trunc(listVar.varContent, cfg.cols.lenVar) +
150+
'</td></tr>';
151+
});
152+
var full_table = beg_table + '</table></div>';
153+
return full_table;
156154
}
157-
var full_table = beg_table + '</table></div>'
158-
return full_table
159-
}
160155

161156

162157

src/jupyter_contrib_nbextensions/nbextensions/varInspector/var_list.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
_Jupyter = get_ipython()
88
_nms.shell = _Jupyter.kernel.shell
99

10-
has_numpy = False
1110
try:
1211
import numpy as np
13-
has_numpy = True
1412
except ImportError:
15-
has_numpy = False
13+
pass
1614

1715
def _getsizeof(x):
1816
# return the size of variable x. Amended version of sys.getsizeof
@@ -27,20 +25,16 @@ def _getsizeof(x):
2725
def _getshapeof(x):
2826
#returns the shape of x if it has one
2927
#returns None otherwise - might want to return an empty string for an empty collum
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
28+
try:
29+
return x.shape
30+
except AttributeError: #x does not have a shape
31+
return None
3632

3733
def var_dic_list():
3834
types_to_exclude = ['module', 'function', 'builtin_function_or_method',
3935
'instance', '_Feature', 'type', 'ufunc']
4036
values = _nms.who_ls()
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)))
37+
vardic = [{'varName': v, 'varType': type(eval(v)).__name__, 'varSize': str(_getsizeof(eval(v))), 'varShape': str(_getshapeof(eval(v))) if _getshapeof(eval(v)) else '', 'varContent': str(eval(v))[:200]} # noqa
4438

4539
for v in values if (v not in ['_html', '_nms', 'NamespaceMagics', '_Jupyter']) & (type(eval(v)).__name__ not in types_to_exclude)] # noqa
4640
return json.dumps(vardic)

0 commit comments

Comments
 (0)