Skip to content

Commit 403c766

Browse files
committed
Added Shape as a different column
Simply Added it to the html with a check that the kernel is python. I don't know if R has shapes or anything of that sort so I add the column only if the kernel lang is python. If an object is not an nd.array it will print "None" which can be fixed by making the function _getshapeof(x) return "" instead of None.
1 parent 4430621 commit 403c766

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/jupyter_contrib_nbextensions/nbextensions/varInspector/main.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,30 @@ 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+
if(kernelLanguage === "python")
134+
{
135+
shape_str = '<th >Shape</th>';
136+
}
132137
var beg_table = '<div class=\"inspector\"><table class=\"table fixed table-condensed table-nonfluid \"><col /> \
133-
<col /><col /><thead><tr><th >X</th><th >Name</th><th >Type</th><th >Size</th><th >Value</th></tr></thead><tr><td> \
138+
<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> \
134139
</td></tr>'
135140
var nb_vars = varList.length;
141+
var shape_col_str;
136142
for (var i = 0; i < nb_vars; i++) {
143+
if(kernelLanguage === "python")
144+
{
145+
shape_col_str = '</td><td>' + varList[i].varShape + '</td><td>';
146+
}else
147+
{
148+
shape_col_str = '';
149+
}
137150
beg_table = beg_table +
138151
'<tr><td><a href=\"#\" onClick=\"Jupyter.notebook.kernel.execute(\'' +
139152
kernel_config.delete_cmd_prefix + varList[i].varName + kernel_config.delete_cmd_postfix + '\'' + '); ' +
140153
'Jupyter.notebook.events.trigger(\'varRefresh\'); \">x</a></td>' +
141154
'<td>' + _trunc(varList[i].varName, cfg.cols.lenName) + '</td><td>' + _trunc(varList[i].varType, cfg.cols.lenType) +
142-
'</td><td>' + varList[i].varSize + '</td><td>' + _trunc(varList[i].varContent, cfg.cols.lenVar) +
155+
'</td><td>' + varList[i].varSize + shape_col_str + _trunc(varList[i].varContent, cfg.cols.lenVar) +
143156
'</td></tr>'
144157
}
145158
var full_table = beg_table + '</table></div>'

src/jupyter_contrib_nbextensions/nbextensions/varInspector/var_list.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@
1212
def _getsizeof(x):
1313
# return the size of variable x. Amended version of sys.getsizeof
1414
# which also supports ndarray, Series and DataFrame
15-
if type(x) is np.ndarray:
16-
return x.shape
17-
elif type(x).__name__ in ['ndarray', 'Series']:
15+
if type(x).__name__ in ['ndarray', 'Series']:
1816
return x.nbytes
1917
elif type(x).__name__ == 'DataFrame':
2018
return x.memory_usage().sum()
2119
else:
2220
return getsizeof(x)
2321

22+
def _getshapeof(x):
23+
#returns the shape of x if it has one
24+
#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
2429

2530
def var_dic_list():
2631
types_to_exclude = ['module', 'function', 'builtin_function_or_method',
2732
'instance', '_Feature', 'type', 'ufunc']
2833
values = _nms.who_ls()
29-
vardic = [{'varName': v, 'varType': type(eval(v)).__name__, 'varSize': str(_getsizeof(eval(v))), 'varContent': str(eval(v))[:200]} # noqa
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
3035
for v in values if (v not in ['_html', '_nms', 'NamespaceMagics', '_Jupyter']) & (type(eval(v)).__name__ not in types_to_exclude)] # noqa
3136
return json.dumps(vardic)
3237

0 commit comments

Comments
 (0)