-
-
Notifications
You must be signed in to change notification settings - Fork 477
/
tables.py
1994 lines (1767 loc) · 79.6 KB
/
tables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import datetime as dt
import uuid
from functools import partial
from types import FunctionType, MethodType
from typing import (
TYPE_CHECKING, Any, Callable, ClassVar, Dict, List, Mapping, Optional,
Tuple, Type,
)
import numpy as np
import param
from bokeh.model import Model
from bokeh.models import ColumnDataSource, ImportedStyleSheet
from bokeh.models.widgets.tables import (
AvgAggregator, CellEditor, CellFormatter, CheckboxEditor, DataCube,
DataTable, DateEditor, DateFormatter, GroupingInfo, IntEditor,
MaxAggregator, MinAggregator, NumberEditor, NumberFormatter, RowAggregator,
StringEditor, StringFormatter, SumAggregator, TableColumn,
)
from bokeh.util.serialization import convert_datetime_array
from pyviz_comms import JupyterComm
from ..depends import transform_reference
from ..io.resources import CDN_DIST, CSS_URLS
from ..io.state import state
from ..reactive import Reactive, ReactiveData
from ..util import (
BOKEH_JS_NAT, clone_model, datetime_as_utctimestamp, isdatetime, lazy_load,
styler_update, updating,
)
from ..util.warnings import warn
from .base import Widget
from .button import Button
from .input import TextInput
if TYPE_CHECKING:
import pandas as pd
from bokeh.document import Document
from bokeh.models.sources import DataDict
from pyviz_comms import Comm
from ..models.tabulator import (
CellClickEvent, SelectionEvent, TableEditEvent,
)
def _convert_datetime_array_ignore_list(v):
if isinstance(v, np.ndarray):
return convert_datetime_array(v)
return v
class BaseTable(ReactiveData, Widget):
aggregators = param.Dict(default={}, nested_refs=True, doc="""
A dictionary mapping from index name to an aggregator to
be used for hierarchical multi-indexes (valid aggregators
include 'min', 'max', 'mean' and 'sum'). If separate
aggregators for different columns are required the dictionary
may be nested as `{index_name: {column_name: aggregator}}`""")
editors = param.Dict(default={}, nested_refs=True, doc="""
Bokeh CellEditor to use for a particular column
(overrides the default chosen based on the type).""")
formatters = param.Dict(default={}, nested_refs=True, doc="""
Bokeh CellFormatter to use for a particular column
(overrides the default chosen based on the type).""")
hierarchical = param.Boolean(default=False, constant=True, doc="""
Whether to generate a hierarchical index.""")
row_height = param.Integer(default=40, doc="""
The height of each table row.""")
selection = param.List(default=[], doc="""
The currently selected rows of the table.""")
show_index = param.Boolean(default=True, doc="""
Whether to show the index column.""")
sorters = param.List(default=[], doc="""
A list of sorters to apply during pagination.""")
text_align = param.ClassSelector(default={}, nested_refs=True, class_=(dict, str), doc="""
A mapping from column name to alignment or a fixed column
alignment, which should be one of 'left', 'center', 'right'.""")
titles = param.Dict(default={}, nested_refs=True, doc="""
A mapping from column name to a title to override the name with.""")
widths = param.ClassSelector(default={}, nested_refs=True, class_=(dict, int), doc="""
A mapping from column name to column width or a fixed column
width.""")
value = param.Parameter(default=None)
_data_params: ClassVar[List[str]] = ['value']
_manual_params: ClassVar[List[str]] = [
'formatters', 'editors', 'widths', 'titles', 'value', 'show_index'
]
_rename: ClassVar[Mapping[str, str | None]] = {
'hierarchical': None, 'name': None, 'selection': None
}
__abstract = True
def __init__(self, value=None, **params):
self._renamed_cols = {}
self._filters = []
self._index_mapping = {}
self._edited_indexes = []
super().__init__(value=value, **params)
self.param.watch(self._setup_on_change, ['editors', 'formatters'])
self.param.trigger('editors')
self.param.trigger('formatters')
@param.depends('value', watch=True, on_init=True)
def _compute_renamed_cols(self):
if self.value is None:
self._renamed_cols.clear()
return
self._renamed_cols = {
str(col) if str(col) != col else col: col for col in self._get_fields()
}
@property
def _length(self):
return len(self._processed)
def _validate(self, *events: param.parameterized.Event):
if self.value is None:
return
cols = self.value.columns
if len(cols) != len(cols.drop_duplicates()):
raise ValueError('Cannot display a pandas.DataFrame with '
'duplicate column names.')
def _get_fields(self) -> List[str]:
indexes = self.indexes
col_names = list(self.value.columns)
if not self.hierarchical or len(indexes) == 1:
col_names = indexes + col_names
else:
col_names = indexes[-1:] + col_names
return col_names
def _get_columns(self) -> List[TableColumn]:
if self.value is None:
return []
indexes = self.indexes
fields = self._get_fields()
df = self.value.reset_index() if len(indexes) > 1 else self.value
return self._get_column_definitions(fields, df)
def _get_column_definitions(self, col_names: List[str], df: pd.DataFrame) -> List[TableColumn]:
import pandas as pd
indexes = self.indexes
columns = []
for col in col_names:
if col in df.columns:
data = df[col]
elif col in self.indexes:
if len(self.indexes) == 1:
data = df.index
else:
data = df.index.get_level_values(self.indexes.index(col))
if isinstance(data, pd.DataFrame):
raise ValueError("DataFrame contains duplicate column names.")
col_kwargs = {}
kind = data.dtype.kind
editor: CellEditor
formatter: CellFormatter | None = self.formatters.get(col)
if kind == 'i':
editor = IntEditor()
elif kind == 'b':
editor = CheckboxEditor()
elif kind == 'f':
editor = NumberEditor()
elif isdatetime(data) or kind == 'M':
editor = DateEditor()
else:
editor = StringEditor()
if col in self.editors and not isinstance(self.editors[col], (dict, str)):
editor = self.editors[col]
if isinstance(editor, CellEditor):
editor = clone_model(editor)
if col in indexes or editor is None:
editor = CellEditor()
if formatter is None or isinstance(formatter, (dict, str)):
if kind == 'i':
formatter = NumberFormatter(text_align='right')
elif kind == 'b':
formatter = StringFormatter(text_align='center')
elif kind == 'f':
formatter = NumberFormatter(format='0,0.0[00000]', text_align='right')
elif isdatetime(data) or kind == 'M':
if len(data) and isinstance(data.values[0], dt.date):
date_format = '%Y-%m-%d'
else:
date_format = '%Y-%m-%d %H:%M:%S'
formatter = DateFormatter(format=date_format, text_align='right')
else:
formatter = StringFormatter()
default_text_align = True
else:
if isinstance(formatter, CellFormatter):
formatter = clone_model(formatter)
if hasattr(formatter, 'text_align'):
default_text_align = type(formatter).text_align.class_default(formatter) == formatter.text_align
else:
default_text_align = True
if not hasattr(formatter, 'text_align'):
pass
elif isinstance(self.text_align, str):
formatter.text_align = self.text_align
if not default_text_align:
msg = f"The 'text_align' in Tabulator.formatters[{col!r}] is overridden by Tabulator.text_align"
warn(msg, RuntimeWarning)
elif col in self.text_align:
formatter.text_align = self.text_align[col]
if not default_text_align:
msg = f"The 'text_align' in Tabulator.formatters[{col!r}] is overridden by Tabulator.text_align[{col!r}]"
warn(msg, RuntimeWarning)
elif col in self.indexes:
formatter.text_align = 'left'
if isinstance(self.widths, int):
col_kwargs['width'] = self.widths
elif str(col) in self.widths and isinstance(self.widths.get(str(col)), int):
col_kwargs['width'] = self.widths.get(str(col))
else:
col_kwargs['width'] = 0
title = self.titles.get(col, str(col))
if col in indexes and len(indexes) > 1 and self.hierarchical:
title = 'Index: %s' % ' | '.join(indexes)
elif col in self.indexes and col.startswith('level_'):
title = ''
column = TableColumn(field=str(col), title=title,
editor=editor, formatter=formatter,
**col_kwargs)
columns.append(column)
return columns
def _setup_on_change(self, *events: param.parameterized.Event):
for event in events:
self._process_on_change(event)
def _process_on_change(self, event: param.parameterized.Event):
old, new = event.old, event.new
for model in (old if isinstance(old, dict) else {}).values():
if not isinstance(model, (CellEditor, CellFormatter)):
continue
change_fn = self._editor_change if isinstance(model, CellEditor) else self._formatter_change
for prop in (model.properties() - Model.properties()):
try:
model.remove_on_change(prop, change_fn)
except ValueError:
pass
for model in (new if isinstance(new, dict) else {}).values():
if not isinstance(model, (CellEditor, CellFormatter)):
continue
change_fn = self._editor_change if isinstance(model, CellEditor) else self._formatter_change
for prop in (model.properties() - Model.properties()):
model.on_change(prop, change_fn)
def _editor_change(self, attr: str, new: Any, old: Any):
self.param.trigger('editors')
def _formatter_change(self, attr: str, new: Any, old: Any):
self.param.trigger('formatters')
def _update_index_mapping(self):
if self._processed is None or isinstance(self._processed, list) and not self._processed:
self._index_mapping = {}
return
self._index_mapping = {
i: index
for i, index in enumerate(self._processed.index)
}
@updating
def _update_cds(self, *events: param.parameterized.Event):
old_processed = self._processed
self._processed, data = self._get_data()
self._update_index_mapping()
# If there is a selection we have to compute new index
if self.selection and old_processed is not None:
indexes = list(self._processed.index)
selection = []
for sel in self.selection:
try:
iv = old_processed.index[sel]
idx = indexes.index(iv)
selection.append(idx)
except Exception:
continue
self.selection = selection
self._data = {k: _convert_datetime_array_ignore_list(v) for k, v in data.items()}
msg = {'data': self._data}
for ref, (m, _) in self._models.items():
self._apply_update(events, msg, m.source, ref)
def _process_param_change(self, params):
if 'disabled' in params:
params['editable'] = not params.pop('disabled') and len(self.indexes) <= 1
params = super()._process_param_change(params)
return params
def _get_properties(self, doc: Document) -> Dict[str, Any]:
properties = super()._get_properties(doc)
properties['columns'] = self._get_columns()
properties['source'] = cds = ColumnDataSource(data=self._data)
cds.selected.indices = self.selection
return properties
def _get_model(
self, doc: Document, root: Optional[Model] = None,
parent: Optional[Model] = None, comm: Optional[Comm] = None
) -> Model:
properties = self._get_properties(doc)
model = self._widget_type(**properties)
root = root or model
self._link_props(model.source, ['data'], doc, root, comm)
self._link_props(model.source.selected, ['indices'], doc, root, comm)
self._models[root.ref['id']] = (model, parent)
return model
def _update_columns(self, event: param.parameterized.Event, model: Model):
if event.name == 'value' and [c.field for c in model.columns] == self._get_fields():
# Skip column update if the data has changed but the columns
# have not
return
model.columns = self._get_columns()
def _manual_update(
self, events: Tuple[param.parameterized.Event, ...], model: Model, doc: Document,
root: Model, parent: Optional[Model], comm: Optional[Comm]
) -> None:
for event in events:
if event.type == 'triggered' and self._updating:
continue
elif event.name in ('value', 'show_index'):
self._update_columns(event, model)
if isinstance(model, DataCube):
model.groupings = self._get_groupings()
elif hasattr(self, '_update_' + event.name):
getattr(self, '_update_' + event.name)(model)
else:
self._update_columns(event, model)
def _sort_df(self, df: pd.DataFrame) -> pd.DataFrame:
if not self.sorters:
return df
fields = [self._renamed_cols.get(s['field'], s['field']) for s in self.sorters]
ascending = [s['dir'] == 'asc' for s in self.sorters]
# Temporarily add _index_ column because Tabulator uses internal _index
# as additional sorter to break ties
df['_index_'] = np.arange(len(df)).astype(str)
fields.append('_index_')
ascending.append(True)
# Handle sort on index column if show_index=True
if self.show_index:
rename = 'index' in fields and df.index.name is None
if rename:
df.index.name = 'index'
else:
rename = False
def tabulator_sorter(col):
# Tabulator JS defines its own sorting algorithm:
# - strings's case isn't taken into account
if col.dtype.kind not in 'SUO':
return col
try:
return col.fillna("").str.lower()
except Exception:
return col
df_sorted = df.sort_values(fields, ascending=ascending, kind='mergesort',
key=tabulator_sorter)
# Revert temporary changes to DataFrames
if rename:
df.index.name = None
df_sorted.index.name = None
df.drop(columns=['_index_'], inplace=True)
df_sorted.drop(columns=['_index_'], inplace=True)
return df_sorted
def _filter_dataframe(self, df: pd.DataFrame) -> pd.DataFrame:
"""
Filter the DataFrame.
Parameters
----------
df : DataFrame
The DataFrame to filter
Returns
-------
DataFrame
The filtered DataFrame
"""
filters = []
for col_name, filt in self._filters:
if col_name is not None and col_name not in df.columns:
continue
if isinstance(filt, (FunctionType, MethodType, partial)):
df = filt(df)
continue
if isinstance(filt, param.Parameter):
val = getattr(filt.owner, filt.name)
else:
val = filt
column = df[col_name]
if val is None:
continue
elif np.isscalar(val):
mask = column == val
elif isinstance(val, (list, set)):
if not val:
continue
mask = column.isin(val)
elif isinstance(val, tuple):
start, end = val
if start is None and end is None:
continue
elif start is None:
mask = column<=end
elif end is None:
mask = column>=start
else:
mask = (column>=start) & (column<=end)
else:
raise ValueError(f"'{col_name} filter value not "
"understood. Must be either a scalar, "
"tuple or list.")
filters.append(mask)
filters.extend(self._get_header_filters(df))
if filters:
mask = filters[0]
for f in filters:
mask &= f
if self._edited_indexes:
edited_mask = (df.index.isin(self._edited_indexes))
mask = mask | edited_mask
df = df[mask]
return df
def _get_header_filters(self, df):
filters = []
for filt in getattr(self, 'filters', []):
col_name = filt['field']
op = filt['type']
val = filt['value']
filt_def = getattr(self, 'header_filters', {}) or {}
if col_name in df.columns:
col = df[col_name]
elif col_name in self.indexes:
if len(self.indexes) == 1:
col = df.index
else:
col = df.index.get_level_values(self.indexes.index(col_name))
else:
continue
# Sometimes Tabulator will provide a zero/single element list
if isinstance(val, list):
if len(val) == 1:
val = val[0]
elif not val:
continue
val = col.dtype.type(val)
if op == '=':
filters.append(col == val)
elif op == '!=':
filters.append(col != val)
elif op == '<':
filters.append(col < val)
elif op == '>':
filters.append(col > val)
elif op == '>=':
filters.append(col >= val)
elif op == '<=':
filters.append(col <= val)
elif op == 'in':
if not isinstance(val, (list, np.ndarray)): val = [val]
filters.append(col.isin(val))
elif op == 'like':
filters.append(col.str.contains(val, case=False, regex=False))
elif op == 'starts':
filters.append(col.str.startsWith(val))
elif op == 'ends':
filters.append(col.str.endsWith(val))
elif op == 'keywords':
match_all = filt_def.get(col_name, {}).get('matchAll', False)
sep = filt_def.get(col_name, {}).get('separator', ' ')
matches = val.split(sep)
if match_all:
for match in matches:
filters.append(col.str.contains(match, case=False, regex=False))
else:
filt = col.str.contains(matches[0], case=False, regex=False)
for match in matches[1:]:
filt |= col.str.contains(match, case=False, regex=False)
filters.append(filt)
elif op == 'regex':
raise ValueError("Regex filtering not supported.")
else:
raise ValueError(f"Filter type {op!r} not recognized.")
return filters
def add_filter(self, filter, column=None):
"""
Adds a filter to the table which can be a static value or
dynamic parameter based object which will automatically
update the table when changed..
When a static value, widget or parameter is supplied the
filtering will follow a few well defined behaviors:
* scalar: Filters by checking for equality
* tuple: A tuple will be interpreted as range.
* list: A list will be interpreted as a set of discrete
scalars and the filter will check if the values
in the column match any of the items in the list.
Arguments
---------
filter: Widget, param.Parameter or FunctionType
The value by which to filter the DataFrame along the
declared column, or a function accepting the DataFrame to
be filtered and returning a filtered copy of the DataFrame.
column: str or None
Column to which the filter will be applied, if the filter
is a constant value, widget or parameter.
Raises
------
ValueError: If the filter type is not supported or no column
was declared.
"""
if isinstance(filter, (tuple, list, set)) or np.isscalar(filter):
deps = []
elif isinstance(filter, (FunctionType, MethodType, partial)):
deps = list(filter._dinfo['kw'].values()) if hasattr(filter, '_dinfo') else []
else:
filter = transform_reference(filter)
if not isinstance(filter, param.Parameter):
raise ValueError(f'{type(self).__name__} filter must be '
'a constant value, parameter, widget '
'or function.')
elif column is None:
raise ValueError('When filtering with a parameter or '
'widget, a column to filter on must be '
'declared.')
deps = [filter]
for dep in deps:
dep.owner.param.watch(self._update_cds, dep.name)
self._filters.append((column, filter))
self._update_cds()
def remove_filter(self, filter):
"""
Removes a filter which was previously added.
"""
self._filters = [(column, filt) for (column, filt) in self._filters
if filt is not filter]
self._update_cds()
def _process_column(self, values):
if not isinstance(values, (list, np.ndarray)):
return [str(v) for v in values]
if isinstance(values, np.ndarray) and values.dtype.kind == "b":
# Workaround for https://github.com/bokeh/bokeh/issues/12776
return values.tolist()
return values
def _get_data(self) -> Tuple[pd.DataFrame, DataDict]:
return self._process_df_and_convert_to_cds(self.value)
def _process_df_and_convert_to_cds(self, df: pd.DataFrame) -> Tuple[pd.DataFrame, DataDict]:
import pandas as pd
df = self._filter_dataframe(df)
if df is None:
return [], {}
if isinstance(self.value.index, pd.MultiIndex):
indexes = [
f'level_{i}' if n is None else n
for i, n in enumerate(df.index.names)
]
else:
default_index = ('level_0' if 'index' in df.columns else 'index')
indexes = [df.index.name or default_index]
if len(indexes) > 1:
df = df.reset_index()
data = ColumnDataSource.from_df(df)
if not self.show_index and len(indexes) > 1:
data = {k: v for k, v in data.items() if k not in indexes}
return df, {k if isinstance(k, str) else str(k): self._process_column(v) for k, v in data.items()}
def _update_column(self, column, array):
import pandas as pd
self.value[column] = array
if self._processed is not None and self.value is not self._processed:
with pd.option_context('mode.chained_assignment', None):
self._processed[column] = array
#----------------------------------------------------------------
# Public API
#----------------------------------------------------------------
@property
def indexes(self):
import pandas as pd
if self.value is None or not self.show_index:
return []
elif isinstance(self.value.index, pd.MultiIndex):
return [
f'level_{i}' if n is None else n
for i, n in enumerate(self.value.index.names)
]
default_index = ('level_0' if 'index' in self.value.columns else 'index')
return [self.value.index.name or default_index]
def stream(self, stream_value, rollover=None, reset_index=True):
"""
Streams (appends) the `stream_value` provided to the existing
value in an efficient manner.
Arguments
---------
stream_value: (pd.DataFrame | pd.Series | Dict)
The new value(s) to append to the existing value.
rollover: int
A maximum column size, above which data from the start of
the column begins to be discarded. If None, then columns
will continue to grow unbounded.
reset_index: (bool, default=True)
If True and the stream_value is a DataFrame,
then its index is reset. Helps to keep the
index unique and named `index`
Raises
------
ValueError: Raised if the stream_value is not a supported type.
Examples
--------
Stream a Series to a DataFrame
>>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
>>> tabulator = Tabulator(value=value)
>>> stream_value = pd.Series({"x": 4, "y": "d"})
>>> tabulator.stream(stream_value)
>>> tabulator.value.to_dict("list")
{'x': [1, 2, 4], 'y': ['a', 'b', 'd']}
Stream a Dataframe to a Dataframe
>>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
>>> tabulator = Tabulator(value=value)
>>> stream_value = pd.DataFrame({"x": [3, 4], "y": ["c", "d"]})
>>> tabulator.stream(stream_value)
>>> tabulator.value.to_dict("list")
{'x': [1, 2, 3, 4], 'y': ['a', 'b', 'c', 'd']}
Stream a Dictionary row to a DataFrame
>>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
>>> tabulator = Tabulator(value=value)
>>> stream_value = {"x": 4, "y": "d"}
>>> tabulator.stream(stream_value)
>>> tabulator.value.to_dict("list")
{'x': [1, 2, 4], 'y': ['a', 'b', 'd']}
Stream a Dictionary of Columns to a Dataframe
>>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
>>> tabulator = Tabulator(value=value)
>>> stream_value = {"x": [3, 4], "y": ["c", "d"]}
>>> tabulator.stream(stream_value)
>>> tabulator.value.to_dict("list")
{'x': [1, 2, 3, 4], 'y': ['a', 'b', 'c', 'd']}
"""
import pandas as pd
if not np.isfinite(self.value.index.max()):
value_index_start = 1
else:
value_index_start = self.value.index.max() + 1
if isinstance(stream_value, pd.DataFrame):
if reset_index:
stream_value = stream_value.reset_index(drop=True)
stream_value.index += value_index_start
combined = pd.concat([self.value, stream_value])
if rollover is not None:
combined = combined.iloc[-rollover:]
with param.discard_events(self):
self.value = combined
try:
self._updating = True
self.param.trigger('value')
finally:
self._updating = False
stream_value, stream_data = self._process_df_and_convert_to_cds(stream_value)
try:
self._updating = True
self._stream(stream_data, rollover)
finally:
self._updating = False
elif isinstance(stream_value, pd.Series):
self.value.loc[value_index_start] = stream_value
if rollover is not None and len(self.value) > rollover:
with param.discard_events(self):
self.value = self.value.iloc[-rollover:]
stream_value, stream_data = self._process_df_and_convert_to_cds(self.value.iloc[-1:])
try:
self._updating = True
self._stream(stream_data, rollover)
finally:
self._updating = False
elif isinstance(stream_value, dict):
if stream_value:
try:
stream_value = pd.DataFrame(stream_value)
except ValueError:
stream_value = pd.Series(stream_value)
self.stream(stream_value, rollover)
else:
raise ValueError("The stream value provided is not a DataFrame, Series or Dict!")
def patch(self, patch_value, as_index=True):
"""
Efficiently patches (updates) the existing value with the `patch_value`.
Arguments
---------
patch_value: (pd.DataFrame | pd.Series | Dict)
The value(s) to patch the existing value with.
as_index: boolean
Whether to treat the patch index as DataFrame indexes (True)
or as simple integer index.
Raises
------
ValueError: Raised if the patch_value is not a supported type.
Examples
--------
Patch a DataFrame with a Dictionary row.
>>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
>>> tabulator = Tabulator(value=value)
>>> patch_value = {"x": [(0, 3)]}
>>> tabulator.patch(patch_value)
>>> tabulator.value.to_dict("list")
{'x': [3, 2], 'y': ['a', 'b']}
Patch a Dataframe with a Dictionary of Columns.
>>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
>>> tabulator = Tabulator(value=value)
>>> patch_value = {"x": [(slice(2), (3,4))], "y": [(1,'d')]}
>>> tabulator.patch(patch_value)
>>> tabulator.value.to_dict("list")
{'x': [3, 4], 'y': ['a', 'd']}
Patch a DataFrame with a Series. Please note the index is used in the update.
>>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
>>> tabulator = Tabulator(value=value)
>>> patch_value = pd.Series({"index": 1, "x": 4, "y": "d"})
>>> tabulator.patch(patch_value)
>>> tabulator.value.to_dict("list")
{'x': [1, 4], 'y': ['a', 'd']}
Patch a Dataframe with a Dataframe. Please note the index is used in the update.
>>> value = pd.DataFrame({"x": [1, 2], "y": ["a", "b"]})
>>> tabulator = Tabulator(value=value)
>>> patch_value = pd.DataFrame({"x": [3, 4], "y": ["c", "d"]})
>>> tabulator.patch(patch_value)
>>> tabulator.value.to_dict("list")
{'x': [3, 4], 'y': ['c', 'd']}
"""
if self.value is None:
raise ValueError(f"Cannot patch empty {type(self).__name__}.")
import pandas as pd
if not isinstance(self.value, pd.DataFrame):
raise ValueError(
f"Patching an object of type {type(self.value).__name__} "
"is not supported. Please provide a dict."
)
if isinstance(patch_value, pd.DataFrame):
patch_value_dict = {
column: list(patch_value[column].items()) for column in patch_value.columns
}
self.patch(patch_value_dict, as_index=as_index)
elif isinstance(patch_value, pd.Series):
if "index" in patch_value: # Series orient is row
patch_value_dict = {
k: [(patch_value["index"], v)] for k, v in patch_value.items()
}
patch_value_dict.pop("index")
else: # Series orient is column
patch_value_dict = {patch_value.name: list(patch_value.items())}
self.patch(patch_value_dict, as_index=as_index)
elif isinstance(patch_value, dict):
columns = list(self.value.columns)
patches = {}
for k, v in patch_value.items():
values = []
for (patch_ind, value) in v:
data_ind = patch_ind
if isinstance(patch_ind, slice):
data_ind = range(patch_ind.start, patch_ind.stop, patch_ind.step or 1)
if as_index:
if not isinstance(data_ind, range):
patch_ind = self.value.index.get_loc(patch_ind)
if not isinstance(patch_ind, int):
raise ValueError(
'Patching a table with duplicate index values is not supported. '
f'Found this duplicate index: {data_ind!r}'
)
self.value.loc[data_ind, k] = value
else:
self.value.iloc[data_ind, columns.index(k)] = value
if isinstance(value, pd.Timestamp):
value = datetime_as_utctimestamp(value)
elif value is pd.NaT:
value = BOKEH_JS_NAT
values.append((patch_ind, value))
patches[k] = values
self._patch(patches)
else:
raise ValueError(
f"Patching with a patch_value of type {type(patch_value).__name__} "
"is not supported. Please provide a DataFrame, Series or Dict."
)
@property
def current_view(self):
"""
Returns the current view of the table after filtering and
sorting are applied.
"""
df = self._processed
return self._sort_df(df)
@property
def selected_dataframe(self):
"""
Returns a DataFrame of the currently selected rows.
"""
if not self.selection:
return self.current_view.iloc[:0]
return self.current_view.iloc[self.selection]
class DataFrame(BaseTable):
"""
The `DataFrame` widget allows displaying and editing a pandas DataFrame.
Note that editing is not possible for multi-indexed DataFrames, in which
case you will need to reduce the DataFrame to a single index.
Also note that the `DataFrame` widget will eventually be replaced with the
`Tabulator` widget, and so new code should be written to use `Tabulator`
instead.
Reference: https://panel.holoviz.org/reference/widgets/DataFrame.html
:Example:
>>> DataFrame(df, name='DataFrame')
"""
auto_edit = param.Boolean(default=False, doc="""
Whether clicking on a table cell automatically starts edit mode.""")
autosize_mode = param.ObjectSelector(default='force_fit', objects=[
"none", "fit_columns", "fit_viewport", "force_fit"], doc="""
Determines the column autosizing mode, as one of the following options:
``"fit_columns"``
Compute column widths based on cell contents while ensuring the
table fits into the available viewport. This results in no
horizontal scrollbar showing up, but data can get unreadable
if there is not enough space available.
``"fit_viewport"``
Adjust the viewport size after computing column widths based
on cell contents.
``"force_fit"``
Fit columns into available space dividing the table width across
the columns equally (equivalent to `fit_columns=True`).
This results in no horizontal scrollbar showing up, but data
can get unreadable if there is not enough space available.
``"none"``
Do not automatically compute column widths.""")
fit_columns = param.Boolean(default=None, doc="""
Whether columns should expand to the available width. This
results in no horizontal scrollbar showing up, but data can
get unreadable if there is no enough space available.""")
frozen_columns = param.Integer(default=None, doc="""
Integer indicating the number of columns to freeze. If set, the
first N columns will be frozen, which prevents them from
scrolling out of frame.""")
frozen_rows = param.Integer(default=None, doc="""
Integer indicating the number of rows to freeze. If set, the
first N rows will be frozen, which prevents them from scrolling
out of frame; if set to a negative value the last N rows will be
frozen.""")
reorderable = param.Boolean(default=True, doc="""
Allows the reordering of a table's columns. To reorder a
column, click and drag a table's header to the desired
location in the table. The columns on either side will remain
in their previous order.""")
sortable = param.Boolean(default=True, doc="""
Allows to sort table's contents. By default natural order is
preserved. To sort a column, click on its header. Clicking
one more time changes sort direction. Use Ctrl + click to
return to natural order. Use Shift + click to sort multiple
columns simultaneously.""")
_manual_params: ClassVar[List[str]] = BaseTable._manual_params + ['aggregators']
_aggregators = {
'sum': SumAggregator, 'max': MaxAggregator,
'min': MinAggregator, 'mean': AvgAggregator
}
_source_transforms: ClassVar[Mapping[str, str | None]] = {'hierarchical': None}
_rename: ClassVar[Mapping[str, str | None]] = {
'selection': None, 'sorters': None, 'text_align': None
}
@property
def _widget_type(self) -> Type[Model]:
return DataCube if self.hierarchical else DataTable
def _get_columns(self):
if self.value is None:
return []
indexes = self.indexes
col_names = list(self.value.columns)
if not self.hierarchical or len(indexes) == 1:
col_names = indexes + col_names
else:
col_names = indexes[-1:] + col_names
df = self.value.reset_index() if len(indexes) > 1 else self.value
return self._get_column_definitions(col_names, df)
def _get_groupings(self):
if self.value is None:
return []
groups = []
for group, agg_group in zip(self.indexes[:-1], self.indexes[1:]):
if str(group) != group:
self._renamed_cols[str(group)] = group
aggs = self._get_aggregators(agg_group)
groups.append(GroupingInfo(getter=str(group), aggregators=aggs))
return groups
def _get_aggregators(self, group):
numeric_cols = list(self.value.select_dtypes(include='number').columns)
aggs = self.aggregators.get(group, [])
if not isinstance(aggs, list):
aggs = [aggs]