1+ import { IVariableInspector } from './tokens' ;
2+
13export namespace Languages {
24 export type LanguageModel = {
35 initScript : string ;
46 queryCommand : string ;
57 matrixQueryCommand : string ;
68 widgetQueryCommand : string ;
79 deleteCommand : string ;
10+ changeSettingsCommand ?: ( settings : IVariableInspector . ISettings ) => string ;
811 } ;
912}
1013
@@ -16,6 +19,8 @@ export abstract class Languages {
1619 static py_script = `import json
1720import sys
1821from importlib import __import__
22+ from itertools import islice
23+ import collections
1924from IPython import get_ipython
2025from IPython.core.magics.namespace import NamespaceMagics
2126
@@ -24,6 +29,8 @@ _jupyterlab_variableinspector_nms = NamespaceMagics()
2429_jupyterlab_variableinspector_Jupyter = get_ipython()
2530_jupyterlab_variableinspector_nms.shell = _jupyterlab_variableinspector_Jupyter.kernel.shell
2631
32+ _jupyterlab_variableinspector_maxitems = 10
33+
2734__np = None
2835__pd = None
2936__pyspark = None
@@ -54,6 +61,12 @@ def _check_imported():
5461 __xr = _attempt_import('xarray')
5562
5663
64+ def _jupyterlab_variableinspector_changesettings(maxitems, **kwargs):
65+ global _jupyterlab_variableinspector_maxitems
66+
67+ _jupyterlab_variableinspector_maxitems = maxitems
68+
69+
5770def _jupyterlab_variableinspector_getsizeof(x):
5871 if type(x).__name__ in ['ndarray', 'Series']:
5972 return x.nbytes
@@ -101,7 +114,28 @@ def _jupyterlab_variableinspector_getshapeof(x):
101114def _jupyterlab_variableinspector_getcontentof(x):
102115 # returns content in a friendly way for python variables
103116 # pandas and numpy
104- if __pd and isinstance(x, __pd.DataFrame):
117+ if isinstance(x, (bool, str, int, float, type(None))):
118+ content = str(x)
119+ elif isinstance(x, (list, tuple)):
120+ if len(x) <= _jupyterlab_variableinspector_maxitems:
121+ content = str(x)
122+ else:
123+ content = "["
124+ for i in range(_jupyterlab_variableinspector_maxitems):
125+ content += f"{x[i]}, "
126+ content += "...]"
127+ elif isinstance(x, collections.abc.Mapping):
128+ if len(x.keys()) <= _jupyterlab_variableinspector_maxitems:
129+ content = str(x)
130+ else:
131+ first_ten_keys = list(islice(x.keys(), _jupyterlab_variableinspector_maxitems))
132+ content = "{"
133+ for idx, key in enumerate(first_ten_keys):
134+ if idx > 0:
135+ content += ", "
136+ content += f'"{key}": {x[key]}'
137+ content += ", ...}"
138+ elif __pd and isinstance(x, __pd.DataFrame):
105139 colnames = ', '.join(x.columns.map(str))
106140 content = "Columns: %s" % colnames
107141 elif __pd and isinstance(x, __pd.Series):
@@ -152,7 +186,7 @@ def _jupyterlab_variableinspector_dict_list():
152186 def keep_cond(v):
153187 try:
154188 obj = eval(v)
155- if isinstance(obj, (bool, str, list, int, float, type(None))):
189+ if isinstance(obj, (bool, str, list, tuple, collections.abc.Mapping, int, float, type(None))):
156190 return True
157191 if __tf and isinstance(obj, __tf.Variable):
158192 return True
@@ -311,21 +345,27 @@ def _jupyterlab_variableinspector_deletevariable(x):
311345 queryCommand : '_jupyterlab_variableinspector_dict_list()' ,
312346 matrixQueryCommand : '_jupyterlab_variableinspector_getmatrixcontent' ,
313347 widgetQueryCommand : '_jupyterlab_variableinspector_displaywidget' ,
314- deleteCommand : '_jupyterlab_variableinspector_deletevariable'
348+ deleteCommand : '_jupyterlab_variableinspector_deletevariable' ,
349+ changeSettingsCommand : ( settings : IVariableInspector . ISettings ) =>
350+ `_jupyterlab_variableinspector_changesettings(maxitems=${ settings . maxItems } )`
315351 } ,
316352 python2 : {
317353 initScript : Languages . py_script ,
318354 queryCommand : '_jupyterlab_variableinspector_dict_list()' ,
319355 matrixQueryCommand : '_jupyterlab_variableinspector_getmatrixcontent' ,
320356 widgetQueryCommand : '_jupyterlab_variableinspector_displaywidget' ,
321- deleteCommand : '_jupyterlab_variableinspector_deletevariable'
357+ deleteCommand : '_jupyterlab_variableinspector_deletevariable' ,
358+ changeSettingsCommand : ( settings : IVariableInspector . ISettings ) =>
359+ `_jupyterlab_variableinspector_changesettings(maxitems=${ settings . maxItems } )`
322360 } ,
323361 python : {
324362 initScript : Languages . py_script ,
325363 queryCommand : '_jupyterlab_variableinspector_dict_list()' ,
326364 matrixQueryCommand : '_jupyterlab_variableinspector_getmatrixcontent' ,
327365 widgetQueryCommand : '_jupyterlab_variableinspector_displaywidget' ,
328- deleteCommand : '_jupyterlab_variableinspector_deletevariable'
366+ deleteCommand : '_jupyterlab_variableinspector_deletevariable' ,
367+ changeSettingsCommand : ( settings : IVariableInspector . ISettings ) =>
368+ `_jupyterlab_variableinspector_changesettings(maxitems=${ settings . maxItems } )`
329369 } ,
330370 R : {
331371 initScript : Languages . r_script ,
0 commit comments