1
+ import { IVariableInspector } from './tokens' ;
2
+
1
3
export namespace Languages {
2
4
export type LanguageModel = {
3
5
initScript : string ;
4
6
queryCommand : string ;
5
7
matrixQueryCommand : string ;
6
8
widgetQueryCommand : string ;
7
9
deleteCommand : string ;
10
+ changeSettingsCommand ?: ( settings : IVariableInspector . ISettings ) => string ;
8
11
} ;
9
12
}
10
13
@@ -16,6 +19,8 @@ export abstract class Languages {
16
19
static py_script = `import json
17
20
import sys
18
21
from importlib import __import__
22
+ from itertools import islice
23
+ import collections
19
24
from IPython import get_ipython
20
25
from IPython.core.magics.namespace import NamespaceMagics
21
26
@@ -24,6 +29,8 @@ _jupyterlab_variableinspector_nms = NamespaceMagics()
24
29
_jupyterlab_variableinspector_Jupyter = get_ipython()
25
30
_jupyterlab_variableinspector_nms.shell = _jupyterlab_variableinspector_Jupyter.kernel.shell
26
31
32
+ _jupyterlab_variableinspector_maxitems = 10
33
+
27
34
__np = None
28
35
__pd = None
29
36
__pyspark = None
@@ -54,6 +61,12 @@ def _check_imported():
54
61
__xr = _attempt_import('xarray')
55
62
56
63
64
+ def _jupyterlab_variableinspector_changesettings(maxitems, **kwargs):
65
+ global _jupyterlab_variableinspector_maxitems
66
+
67
+ _jupyterlab_variableinspector_maxitems = maxitems
68
+
69
+
57
70
def _jupyterlab_variableinspector_getsizeof(x):
58
71
if type(x).__name__ in ['ndarray', 'Series']:
59
72
return x.nbytes
@@ -101,7 +114,28 @@ def _jupyterlab_variableinspector_getshapeof(x):
101
114
def _jupyterlab_variableinspector_getcontentof(x):
102
115
# returns content in a friendly way for python variables
103
116
# 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):
105
139
colnames = ', '.join(x.columns.map(str))
106
140
content = "Columns: %s" % colnames
107
141
elif __pd and isinstance(x, __pd.Series):
@@ -152,7 +186,7 @@ def _jupyterlab_variableinspector_dict_list():
152
186
def keep_cond(v):
153
187
try:
154
188
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))):
156
190
return True
157
191
if __tf and isinstance(obj, __tf.Variable):
158
192
return True
@@ -311,21 +345,27 @@ def _jupyterlab_variableinspector_deletevariable(x):
311
345
queryCommand : '_jupyterlab_variableinspector_dict_list()' ,
312
346
matrixQueryCommand : '_jupyterlab_variableinspector_getmatrixcontent' ,
313
347
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 } )`
315
351
} ,
316
352
python2 : {
317
353
initScript : Languages . py_script ,
318
354
queryCommand : '_jupyterlab_variableinspector_dict_list()' ,
319
355
matrixQueryCommand : '_jupyterlab_variableinspector_getmatrixcontent' ,
320
356
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 } )`
322
360
} ,
323
361
python : {
324
362
initScript : Languages . py_script ,
325
363
queryCommand : '_jupyterlab_variableinspector_dict_list()' ,
326
364
matrixQueryCommand : '_jupyterlab_variableinspector_getmatrixcontent' ,
327
365
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 } )`
329
369
} ,
330
370
R : {
331
371
initScript : Languages . r_script ,
0 commit comments