1
|
6
|
from __future__ import absolute_import, division, unicode_literals
|
2
|
|
|
3
|
6
|
import datetime as dt
|
4
|
6
|
import pytest
|
5
|
|
|
6
|
6
|
import numpy as np
|
7
|
|
|
8
|
6
|
try:
|
9
|
6
|
import pandas as pd
|
10
|
6
|
from pandas.util.testing import (
|
11
|
|
makeCustomDataframe, makeMixedDataFrame, makeTimeDataFrame
|
12
|
|
)
|
13
|
0
|
except ImportError:
|
14
|
0
|
pytestmark = pytest.mark.skip('pandas not available')
|
15
|
|
|
16
|
6
|
from bokeh.models.widgets.tables import (
|
17
|
|
NumberFormatter, IntEditor, NumberEditor, StringFormatter,
|
18
|
|
SelectEditor, DateFormatter, DataCube, CellEditor,
|
19
|
|
SumAggregator, AvgAggregator, MinAggregator
|
20
|
|
)
|
21
|
|
|
22
|
6
|
from panel.depends import bind
|
23
|
6
|
from panel.widgets import DataFrame, Tabulator, TextInput
|
24
|
|
|
25
|
|
|
26
|
6
|
def test_dataframe_widget(dataframe, document, comm):
|
27
|
|
|
28
|
6
|
table = DataFrame(dataframe)
|
29
|
|
|
30
|
6
|
model = table.get_root(document, comm)
|
31
|
|
|
32
|
6
|
index_col, int_col, float_col, str_col = model.columns
|
33
|
|
|
34
|
6
|
assert index_col.title == 'index'
|
35
|
6
|
assert isinstance(index_col.formatter, NumberFormatter)
|
36
|
6
|
assert isinstance(index_col.editor, CellEditor)
|
37
|
|
|
38
|
6
|
assert int_col.title == 'int'
|
39
|
6
|
assert isinstance(int_col.formatter, NumberFormatter)
|
40
|
6
|
assert isinstance(int_col.editor, IntEditor)
|
41
|
|
|
42
|
6
|
assert float_col.title == 'float'
|
43
|
6
|
assert isinstance(float_col.formatter, NumberFormatter)
|
44
|
6
|
assert isinstance(float_col.editor, NumberEditor)
|
45
|
|
|
46
|
6
|
assert str_col.title == 'str'
|
47
|
6
|
assert isinstance(float_col.formatter, StringFormatter)
|
48
|
6
|
assert isinstance(float_col.editor, NumberEditor)
|
49
|
|
|
50
|
|
|
51
|
6
|
def test_dataframe_widget_no_show_index(dataframe, document, comm):
|
52
|
6
|
table = DataFrame(dataframe, show_index=False)
|
53
|
|
|
54
|
6
|
model = table.get_root(document, comm)
|
55
|
|
|
56
|
6
|
assert len(model.columns) == 3
|
57
|
6
|
int_col, float_col, str_col = model.columns
|
58
|
6
|
assert int_col.title == 'int'
|
59
|
6
|
assert float_col.title == 'float'
|
60
|
6
|
assert str_col.title == 'str'
|
61
|
|
|
62
|
6
|
table.show_index = True
|
63
|
|
|
64
|
6
|
assert len(model.columns) == 4
|
65
|
6
|
index_col, int_col, float_col, str_col = model.columns
|
66
|
6
|
assert index_col.title == 'index'
|
67
|
6
|
assert int_col.title == 'int'
|
68
|
6
|
assert float_col.title == 'float'
|
69
|
6
|
assert str_col.title == 'str'
|
70
|
|
|
71
|
|
|
72
|
6
|
def test_dataframe_widget_datetimes(document, comm):
|
73
|
|
|
74
|
6
|
table = DataFrame(makeTimeDataFrame())
|
75
|
|
|
76
|
6
|
model = table.get_root(document, comm)
|
77
|
|
|
78
|
6
|
dt_col, _, _, _, _ = model.columns
|
79
|
|
|
80
|
6
|
assert dt_col.title == 'index'
|
81
|
6
|
assert isinstance(dt_col.formatter, DateFormatter)
|
82
|
6
|
assert isinstance(dt_col.editor, CellEditor)
|
83
|
|
|
84
|
|
|
85
|
6
|
def test_dataframe_editors(dataframe, document, comm):
|
86
|
6
|
editor = SelectEditor(options=['A', 'B', 'C'])
|
87
|
6
|
table = DataFrame(dataframe, editors={'str': editor})
|
88
|
6
|
model = table.get_root(document, comm)
|
89
|
|
|
90
|
6
|
assert model.columns[-1].editor is editor
|
91
|
|
|
92
|
|
|
93
|
6
|
def test_dataframe_formatter(dataframe, document, comm):
|
94
|
6
|
formatter = NumberFormatter(format='0.0000')
|
95
|
6
|
table = DataFrame(dataframe, formatters={'float': formatter})
|
96
|
6
|
model = table.get_root(document, comm)
|
97
|
6
|
assert model.columns[2].formatter is formatter
|
98
|
|
|
99
|
|
|
100
|
6
|
def test_dataframe_triggers(dataframe):
|
101
|
6
|
events = []
|
102
|
|
|
103
|
6
|
def increment(event, events=events):
|
104
|
6
|
events.append(event)
|
105
|
|
|
106
|
6
|
table = DataFrame(dataframe)
|
107
|
6
|
table.param.watch(increment, 'value')
|
108
|
6
|
table._process_events({'data': {'str': ['C', 'B', 'A']}})
|
109
|
6
|
assert len(events) == 1
|
110
|
|
|
111
|
|
|
112
|
6
|
def test_dataframe_does_not_trigger(dataframe):
|
113
|
6
|
events = []
|
114
|
|
|
115
|
6
|
def increment(event, events=events):
|
116
|
0
|
events.append(event)
|
117
|
|
|
118
|
6
|
table = DataFrame(dataframe)
|
119
|
6
|
table.param.watch(increment, 'value')
|
120
|
6
|
table._process_events({'data': {'str': ['A', 'B', 'C']}})
|
121
|
6
|
assert len(events) == 0
|
122
|
|
|
123
|
|
|
124
|
6
|
def test_dataframe_selected_dataframe(dataframe):
|
125
|
6
|
table = DataFrame(dataframe, selection=[0, 2])
|
126
|
6
|
pd.testing.assert_frame_equal(table.selected_dataframe, dataframe.iloc[[0, 2]])
|
127
|
|
|
128
|
|
|
129
|
6
|
def test_dataframe_process_selection_event(dataframe):
|
130
|
6
|
table = DataFrame(dataframe, selection=[0, 2])
|
131
|
6
|
table._process_events({'indices': [0, 2]})
|
132
|
6
|
pd.testing.assert_frame_equal(table.selected_dataframe, dataframe.iloc[[0, 2]])
|
133
|
|
|
134
|
|
|
135
|
6
|
def test_dataframe_process_data_event(dataframe):
|
136
|
6
|
df = dataframe.copy()
|
137
|
|
|
138
|
6
|
table = DataFrame(dataframe, selection=[0, 2])
|
139
|
6
|
table._process_events({'data': {'int': [5, 7, 9]}})
|
140
|
6
|
df['int'] = [5, 7, 9]
|
141
|
6
|
pd.testing.assert_frame_equal(table.value, df)
|
142
|
|
|
143
|
6
|
table._process_events({'data': {'int': {1: 3, 2: 4, 0: 1}}})
|
144
|
6
|
df['int'] = [1, 3, 4]
|
145
|
6
|
pd.testing.assert_frame_equal(table.value, df)
|
146
|
|
|
147
|
|
|
148
|
6
|
def test_dataframe_duplicate_column_name(document, comm):
|
149
|
6
|
df = pd.DataFrame([[1, 1], [2, 2]], columns=['col', 'col'])
|
150
|
6
|
with pytest.raises(ValueError):
|
151
|
6
|
table = DataFrame(df)
|
152
|
|
|
153
|
6
|
df = pd.DataFrame([[1, 1], [2, 2]], columns=['a', 'b'])
|
154
|
6
|
table = DataFrame(df)
|
155
|
6
|
with pytest.raises(ValueError):
|
156
|
6
|
table.value = table.value.rename(columns={'a': 'b'})
|
157
|
|
|
158
|
6
|
df = pd.DataFrame([[1, 1], [2, 2]], columns=['a', 'b'])
|
159
|
6
|
table = DataFrame(df)
|
160
|
6
|
table.get_root(document, comm)
|
161
|
6
|
with pytest.raises(ValueError):
|
162
|
6
|
table.value = table.value.rename(columns={'a': 'b'})
|
163
|
|
|
164
|
|
|
165
|
6
|
def test_hierarchical_index(document, comm):
|
166
|
6
|
df = pd.DataFrame([
|
167
|
|
('Germany', 2020, 9, 2.4, 'A'),
|
168
|
|
('Germany', 2021, 3, 7.3, 'C'),
|
169
|
|
('Germany', 2022, 6, 3.1, 'B'),
|
170
|
|
('UK', 2020, 5, 8.0, 'A'),
|
171
|
|
('UK', 2021, 1, 3.9, 'B'),
|
172
|
|
('UK', 2022, 9, 2.2, 'A')
|
173
|
|
], columns=['Country', 'Year', 'Int', 'Float', 'Str']).set_index(['Country', 'Year'])
|
174
|
|
|
175
|
6
|
table = DataFrame(value=df, hierarchical=True,
|
176
|
|
aggregators={'Year': {'Int': 'sum', 'Float': 'mean'}})
|
177
|
|
|
178
|
6
|
model = table.get_root(document, comm)
|
179
|
6
|
assert isinstance(model, DataCube)
|
180
|
6
|
assert len(model.grouping) == 1
|
181
|
6
|
grouping = model.grouping[0]
|
182
|
6
|
assert len(grouping.aggregators) == 2
|
183
|
6
|
agg1, agg2 = grouping.aggregators
|
184
|
6
|
assert agg1.field_ == 'Int'
|
185
|
6
|
assert isinstance(agg1, SumAggregator)
|
186
|
6
|
assert agg2.field_ == 'Float'
|
187
|
6
|
assert isinstance(agg2, AvgAggregator)
|
188
|
|
|
189
|
6
|
table.aggregators = {'Year': 'min'}
|
190
|
|
|
191
|
6
|
agg1, agg2 = grouping.aggregators
|
192
|
6
|
print(grouping)
|
193
|
6
|
assert agg1.field_ == 'Int'
|
194
|
6
|
assert isinstance(agg1, MinAggregator)
|
195
|
6
|
assert agg2.field_ == 'Float'
|
196
|
6
|
assert isinstance(agg2, MinAggregator)
|
197
|
|
|
198
|
|
|
199
|
6
|
def test_none_table(document, comm):
|
200
|
6
|
table = DataFrame(value=None)
|
201
|
6
|
assert table.indexes == []
|
202
|
|
|
203
|
6
|
model = table.get_root(document, comm)
|
204
|
|
|
205
|
6
|
assert model.source.data == {}
|
206
|
|
|
207
|
|
|
208
|
6
|
def test_tabulator_config_defaults(document, comm):
|
209
|
6
|
df = makeMixedDataFrame()
|
210
|
6
|
table = Tabulator(df)
|
211
|
|
|
212
|
6
|
model = table.get_root(document, comm)
|
213
|
|
|
214
|
6
|
assert model.configuration['columns'] == [
|
215
|
|
{'field': 'index'},
|
216
|
|
{'field': 'A'},
|
217
|
|
{'field': 'B'},
|
218
|
|
{'field': 'C'},
|
219
|
|
{'field': 'D'}
|
220
|
|
]
|
221
|
6
|
assert model.configuration['selectable'] == True
|
222
|
|
|
223
|
|
|
224
|
6
|
def test_tabulator_config_formatter_string(document, comm):
|
225
|
6
|
df = makeMixedDataFrame()
|
226
|
6
|
table = Tabulator(df, formatters={'B': 'tickCross'})
|
227
|
|
|
228
|
6
|
model = table.get_root(document, comm)
|
229
|
|
|
230
|
6
|
assert model.configuration['columns'][2] == {'field': 'B', 'formatter': 'tickCross'}
|
231
|
|
|
232
|
|
|
233
|
6
|
def test_tabulator_config_formatter_dict(document, comm):
|
234
|
6
|
df = makeMixedDataFrame()
|
235
|
6
|
table = Tabulator(df, formatters={'B': {'type': 'tickCross', 'tristate': True}})
|
236
|
|
|
237
|
6
|
model = table.get_root(document, comm)
|
238
|
|
|
239
|
6
|
assert model.configuration['columns'][2] == {'field': 'B', 'formatter': 'tickCross', 'formatterParams': {'tristate': True}}
|
240
|
|
|
241
|
|
|
242
|
6
|
def test_tabulator_config_editor_string(document, comm):
|
243
|
6
|
df = makeMixedDataFrame()
|
244
|
6
|
table = Tabulator(df, editors={'B': 'select'})
|
245
|
|
|
246
|
6
|
model = table.get_root(document, comm)
|
247
|
|
|
248
|
6
|
assert model.configuration['columns'][2] == {'field': 'B', 'editor': 'select'}
|
249
|
|
|
250
|
|
|
251
|
6
|
def test_tabulator_config_editor_dict(document, comm):
|
252
|
6
|
df = makeMixedDataFrame()
|
253
|
6
|
table = Tabulator(df, editors={'B': {'type': 'select', 'values': True}})
|
254
|
|
|
255
|
6
|
model = table.get_root(document, comm)
|
256
|
|
|
257
|
6
|
assert model.configuration['columns'][2] == {'field': 'B', 'editor': 'select', 'editorParams': {'values': True}}
|
258
|
|
|
259
|
|
|
260
|
6
|
def test_tabulator_groups(document, comm):
|
261
|
6
|
df = makeMixedDataFrame()
|
262
|
6
|
table = Tabulator(df, groups={'Number': ['A', 'B'], 'Other': ['C', 'D']})
|
263
|
|
|
264
|
6
|
model = table.get_root(document, comm)
|
265
|
|
|
266
|
6
|
assert model.configuration['columns'] == [
|
267
|
|
{'field': 'index'},
|
268
|
|
{'title': 'Number',
|
269
|
|
'columns': [
|
270
|
|
{'field': 'A'},
|
271
|
|
{'field': 'B'}
|
272
|
|
]},
|
273
|
|
{'title': 'Other',
|
274
|
|
'columns': [
|
275
|
|
{'field': 'C'},
|
276
|
|
{'field': 'D'}
|
277
|
|
]}
|
278
|
|
]
|
279
|
|
|
280
|
|
|
281
|
6
|
def test_tabulator_frozen_cols(document, comm):
|
282
|
6
|
df = makeMixedDataFrame()
|
283
|
6
|
table = Tabulator(df, frozen_columns=['index'])
|
284
|
|
|
285
|
6
|
model = table.get_root(document, comm)
|
286
|
|
|
287
|
6
|
assert model.configuration['columns'] == [
|
288
|
|
{'field': 'index', 'frozen': True},
|
289
|
|
{'field': 'A'},
|
290
|
|
{'field': 'B'},
|
291
|
|
{'field': 'C'},
|
292
|
|
{'field': 'D'}
|
293
|
|
]
|
294
|
|
|
295
|
|
|
296
|
6
|
def test_tabulator_frozen_rows(document, comm):
|
297
|
6
|
df = makeMixedDataFrame()
|
298
|
6
|
table = Tabulator(df, frozen_rows=[0, -1])
|
299
|
|
|
300
|
6
|
model = table.get_root(document, comm)
|
301
|
|
|
302
|
6
|
assert model.frozen_rows == [0, 4]
|
303
|
|
|
304
|
6
|
table.frozen_rows = [1, -2]
|
305
|
|
|
306
|
6
|
assert model.frozen_rows == [1, 3]
|
307
|
|
|
308
|
|
|
309
|
6
|
def test_tabulator_pagination(document, comm):
|
310
|
6
|
df = makeMixedDataFrame()
|
311
|
6
|
table = Tabulator(df, pagination='remote', page_size=2)
|
312
|
|
|
313
|
6
|
model = table.get_root(document, comm)
|
314
|
|
|
315
|
6
|
assert model.max_page == 3
|
316
|
6
|
assert model.page_size == 2
|
317
|
6
|
assert model.page == 1
|
318
|
|
|
319
|
6
|
expected = {
|
320
|
|
'index': np.array([0, 1]),
|
321
|
|
'A': np.array([0, 1]),
|
322
|
|
'B': np.array([0, 1]),
|
323
|
|
'C': np.array(['foo1', 'foo2']),
|
324
|
|
'D': np.array(['2009-01-01T00:00:00.000000000',
|
325
|
|
'2009-01-02T00:00:00.000000000'],
|
326
|
|
dtype='datetime64[ns]')
|
327
|
|
}
|
328
|
6
|
for col, values in model.source.data.items():
|
329
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
330
|
|
|
331
|
6
|
table.page = 2
|
332
|
|
|
333
|
6
|
expected = {
|
334
|
|
'index': np.array([2, 3]),
|
335
|
|
'A': np.array([2, 3]),
|
336
|
|
'B': np.array([0., 1.]),
|
337
|
|
'C': np.array(['foo3', 'foo4']),
|
338
|
|
'D': np.array(['2009-01-05T00:00:00.000000000',
|
339
|
|
'2009-01-06T00:00:00.000000000'],
|
340
|
|
dtype='datetime64[ns]')
|
341
|
|
}
|
342
|
6
|
for col, values in model.source.data.items():
|
343
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
344
|
|
|
345
|
6
|
table.page_size = 3
|
346
|
6
|
table.page = 1
|
347
|
|
|
348
|
6
|
assert model.max_page == 2
|
349
|
|
|
350
|
6
|
expected = {
|
351
|
|
'index': np.array([0, 1, 2]),
|
352
|
|
'A': np.array([0, 1, 2]),
|
353
|
|
'B': np.array([0, 1, 0]),
|
354
|
|
'C': np.array(['foo1', 'foo2', 'foo3']),
|
355
|
|
'D': np.array(['2009-01-01T00:00:00.000000000',
|
356
|
|
'2009-01-02T00:00:00.000000000',
|
357
|
|
'2009-01-05T00:00:00.000000000'],
|
358
|
|
dtype='datetime64[ns]')
|
359
|
|
}
|
360
|
6
|
for col, values in model.source.data.items():
|
361
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
362
|
|
|
363
|
|
|
364
|
6
|
def test_tabulator_pagination_selection(document, comm):
|
365
|
6
|
df = makeMixedDataFrame()
|
366
|
6
|
table = Tabulator(df, pagination='remote', page_size=2)
|
367
|
|
|
368
|
6
|
model = table.get_root(document, comm)
|
369
|
|
|
370
|
6
|
table.selection = [2, 3]
|
371
|
|
|
372
|
6
|
assert model.source.selected.indices == []
|
373
|
|
|
374
|
6
|
table.page = 2
|
375
|
|
|
376
|
6
|
assert model.source.selected.indices == [0, 1]
|
377
|
|
|
378
|
|
|
379
|
6
|
def test_tabulator_styling(document, comm):
|
380
|
6
|
df = makeMixedDataFrame()
|
381
|
6
|
table = Tabulator(df)
|
382
|
|
|
383
|
6
|
def high_red(value):
|
384
|
6
|
return 'color: red' if value > 2 else 'color: black'
|
385
|
|
|
386
|
6
|
table.style.applymap(high_red, subset=['A'])
|
387
|
|
|
388
|
6
|
model = table.get_root(document, comm)
|
389
|
|
|
390
|
6
|
assert model.styles == {
|
391
|
|
0: {1: ['color: black']},
|
392
|
|
1: {1: ['color: black']},
|
393
|
|
2: {1: ['color: black']},
|
394
|
|
3: {1: ['color: red']},
|
395
|
|
4: {1: ['color: red']}
|
396
|
|
}
|
397
|
|
|
398
|
6
|
def test_tabulator_stream_series(document, comm):
|
399
|
6
|
df = makeMixedDataFrame()
|
400
|
6
|
table = Tabulator(df)
|
401
|
|
|
402
|
6
|
model = table.get_root(document, comm)
|
403
|
|
|
404
|
6
|
stream_value = pd.Series({'A': 5, 'B': 1, 'C': 'foo6', 'D': dt.datetime(2009, 1, 8)})
|
405
|
|
|
406
|
6
|
table.stream(stream_value)
|
407
|
|
|
408
|
6
|
assert len(table.value) == 6
|
409
|
|
|
410
|
6
|
expected = {
|
411
|
|
'index': np.array([0, 1, 2, 3, 4, 5]),
|
412
|
|
'A': np.array([0, 1, 2, 3, 4, 5]),
|
413
|
|
'B': np.array([0, 1, 0, 1, 0, 1]),
|
414
|
|
'C': np.array(['foo1', 'foo2', 'foo3', 'foo4', 'foo5', 'foo6']),
|
415
|
|
'D': np.array(['2009-01-01T00:00:00.000000000',
|
416
|
|
'2009-01-02T00:00:00.000000000',
|
417
|
|
'2009-01-05T00:00:00.000000000',
|
418
|
|
'2009-01-06T00:00:00.000000000',
|
419
|
|
'2009-01-07T00:00:00.000000000',
|
420
|
|
'2009-01-08T00:00:00.000000000'],
|
421
|
|
dtype='datetime64[ns]')
|
422
|
|
}
|
423
|
6
|
for col, values in model.source.data.items():
|
424
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
425
|
|
|
426
|
|
|
427
|
6
|
def test_tabulator_stream_scalars(document, comm):
|
428
|
6
|
df = makeMixedDataFrame()
|
429
|
6
|
table = Tabulator(df)
|
430
|
|
|
431
|
6
|
model = table.get_root(document, comm)
|
432
|
|
|
433
|
6
|
table.patch({'A': [(0, 2), (4, 1)], 'C': [(0, 'foo0')]})
|
434
|
|
|
435
|
6
|
expected = {
|
436
|
|
'index': np.array([0, 1, 2, 3, 4]),
|
437
|
|
'A': np.array([2, 1, 2, 3, 1]),
|
438
|
|
'B': np.array([0, 1, 0, 1, 0]),
|
439
|
|
'C': np.array(['foo0', 'foo2', 'foo3', 'foo4', 'foo5']),
|
440
|
|
'D': np.array(['2009-01-01T00:00:00.000000000',
|
441
|
|
'2009-01-02T00:00:00.000000000',
|
442
|
|
'2009-01-05T00:00:00.000000000',
|
443
|
|
'2009-01-06T00:00:00.000000000',
|
444
|
|
'2009-01-07T00:00:00.000000000'],
|
445
|
|
dtype='datetime64[ns]')
|
446
|
|
}
|
447
|
6
|
for col, values in model.source.data.items():
|
448
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
449
|
|
|
450
|
|
|
451
|
6
|
def test_tabulator_stream_ranges(document, comm):
|
452
|
6
|
df = makeMixedDataFrame()
|
453
|
6
|
table = Tabulator(df)
|
454
|
|
|
455
|
6
|
model = table.get_root(document, comm)
|
456
|
|
|
457
|
6
|
table.patch({
|
458
|
|
'A': [(slice(0, 5), [5, 4, 3, 2, 1])],
|
459
|
|
'C': [(slice(0, 3), ['foo3', 'foo2', 'foo1'])]
|
460
|
|
})
|
461
|
|
|
462
|
6
|
expected = {
|
463
|
|
'index': np.array([0, 1, 2, 3, 4]),
|
464
|
|
'A': np.array([5, 4, 3, 2, 1]),
|
465
|
|
'B': np.array([0, 1, 0, 1, 0]),
|
466
|
|
'C': np.array(['foo3', 'foo2', 'foo1', 'foo4', 'foo5']),
|
467
|
|
'D': np.array(['2009-01-01T00:00:00.000000000',
|
468
|
|
'2009-01-02T00:00:00.000000000',
|
469
|
|
'2009-01-05T00:00:00.000000000',
|
470
|
|
'2009-01-06T00:00:00.000000000',
|
471
|
|
'2009-01-07T00:00:00.000000000'],
|
472
|
|
dtype='datetime64[ns]')
|
473
|
|
}
|
474
|
6
|
for col, values in model.source.data.items():
|
475
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
476
|
|
|
477
|
|
|
478
|
6
|
def test_tabulator_stream_series_paginated_not_follow(document, comm):
|
479
|
6
|
df = makeMixedDataFrame()
|
480
|
6
|
table = Tabulator(df, pagination='remote', page_size=2)
|
481
|
|
|
482
|
6
|
model = table.get_root(document, comm)
|
483
|
|
|
484
|
6
|
stream_value = pd.Series({'A': 5, 'B': 1, 'C': 'foo6', 'D': dt.datetime(2009, 1, 8)})
|
485
|
|
|
486
|
6
|
table.stream(stream_value, follow=False)
|
487
|
|
|
488
|
6
|
assert table.page == 1
|
489
|
6
|
assert len(table.value) == 6
|
490
|
|
|
491
|
6
|
expected = {
|
492
|
|
'index': np.array([0, 1]),
|
493
|
|
'A': np.array([0, 1]),
|
494
|
|
'B': np.array([0, 1]),
|
495
|
|
'C': np.array(['foo1', 'foo2']),
|
496
|
|
'D': np.array(['2009-01-01T00:00:00.000000000',
|
497
|
|
'2009-01-02T00:00:00.000000000'],
|
498
|
|
dtype='datetime64[ns]')
|
499
|
|
}
|
500
|
6
|
for col, values in model.source.data.items():
|
501
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
502
|
|
|
503
|
|
|
504
|
|
|
505
|
6
|
def test_tabulator_stream_series_paginated_follow(document, comm):
|
506
|
6
|
df = makeMixedDataFrame()
|
507
|
6
|
table = Tabulator(df, pagination='remote', page_size=2)
|
508
|
|
|
509
|
6
|
model = table.get_root(document, comm)
|
510
|
|
|
511
|
6
|
stream_value = pd.Series({'A': 5, 'B': 1, 'C': 'foo6', 'D': dt.datetime(2009, 1, 8)})
|
512
|
|
|
513
|
6
|
table.stream(stream_value, follow=True)
|
514
|
|
|
515
|
6
|
assert table.page == 3
|
516
|
6
|
assert len(table.value) == 6
|
517
|
|
|
518
|
6
|
expected = {
|
519
|
|
'index': np.array([4, 5]),
|
520
|
|
'A': np.array([4, 5]),
|
521
|
|
'B': np.array([0, 1]),
|
522
|
|
'C': np.array(['foo5', 'foo6']),
|
523
|
|
'D': np.array(['2009-01-07T00:00:00.000000000',
|
524
|
|
'2009-01-08T00:00:00.000000000'],
|
525
|
|
dtype='datetime64[ns]')
|
526
|
|
}
|
527
|
6
|
for col, values in model.source.data.items():
|
528
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
529
|
|
|
530
|
|
|
531
|
6
|
def test_tabulator_paginated_sorted_selection(document, comm):
|
532
|
6
|
df = makeMixedDataFrame()
|
533
|
6
|
table = Tabulator(df, pagination='remote', page_size=2)
|
534
|
|
|
535
|
6
|
table.sorters = [{'field': 'A', 'dir': 'dec'}]
|
536
|
|
|
537
|
6
|
model = table.get_root(document, comm)
|
538
|
|
|
539
|
6
|
table.selection = [3]
|
540
|
6
|
assert model.source.selected.indices == [1]
|
541
|
|
|
542
|
6
|
table.selection = [0, 1]
|
543
|
6
|
assert model.source.selected.indices == []
|
544
|
|
|
545
|
6
|
table.selection = [3, 4]
|
546
|
6
|
assert model.source.selected.indices == [1, 0]
|
547
|
|
|
548
|
6
|
table.selection = []
|
549
|
6
|
assert model.source.selected.indices == []
|
550
|
|
|
551
|
6
|
table._process_events({'indices': [0, 1]})
|
552
|
6
|
assert table.selection == [4, 3]
|
553
|
|
|
554
|
6
|
table._process_events({'indices': [1]})
|
555
|
6
|
assert table.selection == [3]
|
556
|
|
|
557
|
6
|
table.sorters = [{'field': 'A', 'dir': 'asc'}]
|
558
|
6
|
table._process_events({'indices': [1]})
|
559
|
6
|
assert table.selection == [1]
|
560
|
|
|
561
|
|
|
562
|
6
|
def test_tabulator_stream_dataframe(document, comm):
|
563
|
6
|
df = makeMixedDataFrame()
|
564
|
6
|
table = Tabulator(df)
|
565
|
|
|
566
|
6
|
model = table.get_root(document, comm)
|
567
|
|
|
568
|
6
|
stream_value = pd.DataFrame({
|
569
|
|
'A': [5, 6],
|
570
|
|
'B': [1, 0],
|
571
|
|
'C': ['foo6', 'foo7'],
|
572
|
|
'D': [dt.datetime(2009, 1, 8), dt.datetime(2009, 1, 9)]
|
573
|
|
})
|
574
|
|
|
575
|
6
|
table.stream(stream_value)
|
576
|
|
|
577
|
6
|
assert len(table.value) == 7
|
578
|
|
|
579
|
6
|
expected = {
|
580
|
|
'index': np.array([0, 1, 2, 3, 4, 5, 6]),
|
581
|
|
'A': np.array([0, 1, 2, 3, 4, 5, 6]),
|
582
|
|
'B': np.array([0, 1, 0, 1, 0, 1, 0]),
|
583
|
|
'C': np.array(['foo1', 'foo2', 'foo3', 'foo4', 'foo5', 'foo6', 'foo7']),
|
584
|
|
'D': np.array(['2009-01-01T00:00:00.000000000',
|
585
|
|
'2009-01-02T00:00:00.000000000',
|
586
|
|
'2009-01-05T00:00:00.000000000',
|
587
|
|
'2009-01-06T00:00:00.000000000',
|
588
|
|
'2009-01-07T00:00:00.000000000',
|
589
|
|
'2009-01-08T00:00:00.000000000',
|
590
|
|
'2009-01-09T00:00:00.000000000'],
|
591
|
|
dtype='datetime64[ns]')
|
592
|
|
}
|
593
|
6
|
for col, values in model.source.data.items():
|
594
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
595
|
|
|
596
|
|
|
597
|
6
|
def test_tabulator_constant_scalar_filter(document, comm):
|
598
|
6
|
df = makeMixedDataFrame()
|
599
|
6
|
table = Tabulator(df)
|
600
|
|
|
601
|
6
|
model = table.get_root(document, comm)
|
602
|
|
|
603
|
6
|
table.add_filter('foo3', 'C')
|
604
|
|
|
605
|
6
|
expected = {
|
606
|
|
'index': np.array([2]),
|
607
|
|
'A': np.array([2]),
|
608
|
|
'B': np.array([0]),
|
609
|
|
'C': np.array(['foo3']),
|
610
|
|
'D': np.array(['2009-01-05T00:00:00.000000000'],
|
611
|
|
dtype='datetime64[ns]')
|
612
|
|
}
|
613
|
6
|
for col, values in model.source.data.items():
|
614
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
615
|
|
|
616
|
|
|
617
|
6
|
def test_tabulator_constant_list_filter(document, comm):
|
618
|
6
|
df = makeMixedDataFrame()
|
619
|
6
|
table = Tabulator(df)
|
620
|
|
|
621
|
6
|
model = table.get_root(document, comm)
|
622
|
|
|
623
|
6
|
table.add_filter(['foo3', 'foo5'], 'C')
|
624
|
|
|
625
|
6
|
expected = {
|
626
|
|
'index': np.array([2, 4]),
|
627
|
|
'A': np.array([2, 4]),
|
628
|
|
'B': np.array([0, 0]),
|
629
|
|
'C': np.array(['foo3', 'foo5']),
|
630
|
|
'D': np.array(['2009-01-05T00:00:00.000000000',
|
631
|
|
'2009-01-07T00:00:00.000000000'],
|
632
|
|
dtype='datetime64[ns]')
|
633
|
|
}
|
634
|
6
|
for col, values in model.source.data.items():
|
635
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
636
|
|
|
637
|
|
|
638
|
6
|
def test_tabulator_widget_scalar_filter(document, comm):
|
639
|
6
|
df = makeMixedDataFrame()
|
640
|
6
|
table = Tabulator(df)
|
641
|
|
|
642
|
6
|
model = table.get_root(document, comm)
|
643
|
|
|
644
|
6
|
widget = TextInput(value='foo3')
|
645
|
6
|
table.add_filter(widget, 'C')
|
646
|
|
|
647
|
6
|
expected = {
|
648
|
|
'index': np.array([2]),
|
649
|
|
'A': np.array([2]),
|
650
|
|
'B': np.array([0]),
|
651
|
|
'C': np.array(['foo3']),
|
652
|
|
'D': np.array(['2009-01-05T00:00:00.000000000'],
|
653
|
|
dtype='datetime64[ns]')
|
654
|
|
}
|
655
|
6
|
for col, values in model.source.data.items():
|
656
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
657
|
|
|
658
|
6
|
widget.value = 'foo1'
|
659
|
|
|
660
|
6
|
expected = {
|
661
|
|
'index': np.array([0]),
|
662
|
|
'A': np.array([0]),
|
663
|
|
'B': np.array([0]),
|
664
|
|
'C': np.array(['foo1']),
|
665
|
|
'D': np.array(['2009-01-01T00:00:00.000000000'],
|
666
|
|
dtype='datetime64[ns]')
|
667
|
|
}
|
668
|
6
|
for col, values in model.source.data.items():
|
669
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
670
|
|
|
671
|
|
|
672
|
6
|
def test_tabulator_function_filter(document, comm):
|
673
|
6
|
df = makeMixedDataFrame()
|
674
|
6
|
table = Tabulator(df)
|
675
|
|
|
676
|
6
|
model = table.get_root(document, comm)
|
677
|
|
|
678
|
6
|
widget = TextInput(value='foo3')
|
679
|
|
|
680
|
6
|
def filter_c(df, value):
|
681
|
6
|
return df[df.C.str.contains(value)]
|
682
|
|
|
683
|
6
|
table.add_filter(bind(filter_c, value=widget), 'C')
|
684
|
|
|
685
|
6
|
expected = {
|
686
|
|
'index': np.array([2]),
|
687
|
|
'A': np.array([2]),
|
688
|
|
'B': np.array([0]),
|
689
|
|
'C': np.array(['foo3']),
|
690
|
|
'D': np.array(['2009-01-05T00:00:00.000000000'],
|
691
|
|
dtype='datetime64[ns]')
|
692
|
|
}
|
693
|
6
|
for col, values in model.source.data.items():
|
694
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
695
|
|
|
696
|
6
|
widget.value = 'foo1'
|
697
|
|
|
698
|
6
|
expected = {
|
699
|
|
'index': np.array([0]),
|
700
|
|
'A': np.array([0]),
|
701
|
|
'B': np.array([0]),
|
702
|
|
'C': np.array(['foo1']),
|
703
|
|
'D': np.array(['2009-01-01T00:00:00.000000000'],
|
704
|
|
dtype='datetime64[ns]')
|
705
|
|
}
|
706
|
6
|
for col, values in model.source.data.items():
|
707
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
708
|
|
|
709
|
|
|
710
|
6
|
def test_tabulator_constant_tuple_filter(document, comm):
|
711
|
6
|
df = makeMixedDataFrame()
|
712
|
6
|
table = Tabulator(df)
|
713
|
|
|
714
|
6
|
model = table.get_root(document, comm)
|
715
|
|
|
716
|
6
|
table.add_filter((2, 3), 'A')
|
717
|
|
|
718
|
6
|
expected = {
|
719
|
|
'index': np.array([2, 3]),
|
720
|
|
'A': np.array([2, 3]),
|
721
|
|
'B': np.array([0, 1]),
|
722
|
|
'C': np.array(['foo3', 'foo4']),
|
723
|
|
'D': np.array(['2009-01-05T00:00:00.000000000',
|
724
|
|
'2009-01-06T00:00:00.000000000'],
|
725
|
|
dtype='datetime64[ns]')
|
726
|
|
}
|
727
|
6
|
for col, values in model.source.data.items():
|
728
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
729
|
|
|
730
|
|
|
731
|
6
|
def test_tabulator_stream_dataframe_with_filter(document, comm):
|
732
|
6
|
df = makeMixedDataFrame()
|
733
|
6
|
table = Tabulator(df)
|
734
|
|
|
735
|
6
|
model = table.get_root(document, comm)
|
736
|
|
|
737
|
6
|
table.add_filter(['foo2', 'foo7'], 'C')
|
738
|
|
|
739
|
6
|
stream_value = pd.DataFrame({
|
740
|
|
'A': [5, 6],
|
741
|
|
'B': [1, 0],
|
742
|
|
'C': ['foo6', 'foo7'],
|
743
|
|
'D': [dt.datetime(2009, 1, 8), dt.datetime(2009, 1, 9)]
|
744
|
|
})
|
745
|
|
|
746
|
6
|
table.stream(stream_value)
|
747
|
|
|
748
|
6
|
assert len(table.value) == 7
|
749
|
|
|
750
|
6
|
expected = {
|
751
|
|
'index': np.array([1, 6]),
|
752
|
|
'A': np.array([1, 6]),
|
753
|
|
'B': np.array([1, 0]),
|
754
|
|
'C': np.array(['foo2', 'foo7']),
|
755
|
|
'D': np.array(['2009-01-02T00:00:00.000000000',
|
756
|
|
'2009-01-09T00:00:00.000000000'],
|
757
|
|
dtype='datetime64[ns]')
|
758
|
|
}
|
759
|
6
|
for col, values in model.source.data.items():
|
760
|
6
|
np.testing.assert_array_equal(values, expected[col])
|
761
|
|
|
762
|
|
|
763
|
6
|
def test_tabulator_dataframe_replace_data(document, comm):
|
764
|
6
|
df = makeMixedDataFrame()
|
765
|
6
|
table = Tabulator(df)
|
766
|
|
|
767
|
6
|
model = table.get_root(document, comm)
|
768
|
|
|
769
|
6
|
table.value = makeCustomDataframe(2, 2)
|
770
|
|
|
771
|
6
|
assert len(model.columns) == 3
|
772
|
6
|
c1, c2, c3 = model.columns
|
773
|
6
|
assert c1.field == 'R0'
|
774
|
6
|
assert c2.field == 'C_l0_g0'
|
775
|
6
|
assert c3.field == 'C_l0_g1'
|
776
|
6
|
assert model.configuration == {
|
777
|
|
'columns': [{'field': 'R0'}, {'field': 'C_l0_g0'}, {'field': 'C_l0_g1'}],
|
778
|
|
'selectable': True
|
779
|
|
}
|
780
|
6
|
expected = {
|
781
|
|
'C_l0_g0': np.array(['R0C0', 'R1C0'], dtype=object),
|
782
|
|
'C_l0_g1': np.array(['R0C1', 'R1C1'], dtype=object),
|
783
|
|
'R0': np.array(['R_l0_g0', 'R_l0_g1'], dtype=object)
|
784
|
|
}
|
785
|
6
|
for col, values in model.source.data.items():
|
786
|
6
|
np.testing.assert_array_equal(values, expected[col])
|