Skip to content

Commit 7ce811a

Browse files
authored
Merge pull request #1411 from jfbercher/varInspector_content
[varInspector] updated var_list.py to address #1275
2 parents c406eb0 + ad3af66 commit 7ce811a

File tree

1 file changed

+25
-10
lines changed
  • src/jupyter_contrib_nbextensions/nbextensions/varInspector

1 file changed

+25
-10
lines changed

src/jupyter_contrib_nbextensions/nbextensions/varInspector/var_list.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33

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

1110
try:
12-
import numpy as np # noqa: F401
11+
import numpy as np
1312
except ImportError:
14-
pass
15-
13+
pass
1614

1715
def _getsizeof(x):
1816
# return the size of variable x. Amended version of sys.getsizeof
@@ -24,22 +22,39 @@ def _getsizeof(x):
2422
else:
2523
return getsizeof(x)
2624

27-
2825
def _getshapeof(x):
29-
# returns the shape of x if it has one
30-
# returns None otherwise - might want to return an empty string for an empty collum
26+
#returns the shape of x if it has one
27+
#returns None otherwise - might want to return an empty string for an empty column
3128
try:
3229
return x.shape
33-
except AttributeError: # x does not have a shape
30+
except AttributeError: #x does not have a shape
3431
return None
3532

33+
def _getcontentof(x):
34+
length = 150
35+
if type(x).__name__ == 'DataFrame':
36+
colnames = ', '.join(x.columns.map(str))
37+
content = "Column names: %s" % colnames
38+
elif type(x).__name__ == 'Series':
39+
content = "Series [%d rows]" % x.shape
40+
elif type(x).__name__ == 'ndarray':
41+
content = x.__repr__()
42+
else:
43+
if hasattr(x, '__len__'):
44+
if len(x) > length:
45+
content = str(x[:length])
46+
else:
47+
content = str(x)
48+
if len(content) > 150:
49+
return content[:150] + " ..."
50+
return content
3651

3752
def var_dic_list():
3853
types_to_exclude = ['module', 'function', 'builtin_function_or_method',
3954
'instance', '_Feature', 'type', 'ufunc']
4055
values = _nms.who_ls()
41-
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
42-
56+
vardic = [{'varName': v, 'varType': type(eval(v)).__name__, 'varSize': str(_getsizeof(eval(v))), 'varShape': str(_getshapeof(eval(v))) if _getshapeof(eval(v)) else '', 'varContent': _getcontentof(eval(v)) } # noqa
57+
4358
for v in values if (v not in ['_html', '_nms', 'NamespaceMagics', '_Jupyter']) & (type(eval(v)).__name__ not in types_to_exclude)] # noqa
4459
return json.dumps(vardic)
4560

0 commit comments

Comments
 (0)