-
-
Notifications
You must be signed in to change notification settings - Fork 477
/
test_tabulator.py
3426 lines (2527 loc) · 102 KB
/
test_tabulator.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 time
import param
import pytest
from bokeh.models.widgets.tables import (
BooleanFormatter, CheckboxEditor, DateEditor, DateFormatter,
HTMLTemplateFormatter, IntEditor, NumberEditor, NumberFormatter,
ScientificFormatter, SelectEditor, StringEditor, StringFormatter,
TextEditor,
)
try:
from playwright.sync_api import expect
except ImportError:
pytestmark = pytest.mark.skip('playwright not available')
pytestmark = pytest.mark.ui
try:
import numpy as np
except ImportError:
pytestmark = pytest.mark.skip('numpy not available')
try:
import pandas as pd
except ImportError:
pytestmark = pytest.mark.skip('pandas not available')
from panel import state
from panel.io.server import serve
from panel.models.tabulator import _TABULATOR_THEMES_MAPPING
from panel.tests.util import get_ctrl_modifier, wait_until
from panel.widgets import Tabulator
@pytest.fixture
def df_mixed():
df = pd.DataFrame({
'int': [1, 2, 3, 4],
'float': [3.14, 6.28, 9.42, -2.45],
'str': ['A', 'B', 'C', 'D'],
'bool': [True, True, True, False],
'date': [dt.date(2019, 1, 1), dt.date(2020, 1, 1), dt.date(2020, 1, 10), dt.date(2019, 1, 10)],
'datetime': [dt.datetime(2019, 1, 1, 10), dt.datetime(2020, 1, 1, 12), dt.datetime(2020, 1, 10, 13), dt.datetime(2020, 1, 15, 13)]
}, index=['idx0', 'idx1', 'idx2', 'idx3'])
return df
@pytest.fixture(scope='session')
def df_mixed_as_string():
return """
index
int
float
str
bool
date
datetime
idx0
1
3.14
A
true
2019-01-01
2019-01-01 10:00:00
idx1
2
6.28
B
true
2020-01-01
2020-01-01 12:00:00
idx2
3
9.42
C
true
2020-01-10
2020-01-10 13:00:00
idx3
4
-2.45
D
false
2019-01-10
2020-01-15 13:00:00
"""
@pytest.fixture
def df_multiindex(df_mixed):
df_mi = df_mixed.copy()
df_mi.index = pd.MultiIndex.from_tuples([
('group0', 'subgroup0'),
('group0', 'subgroup1'),
('group1', 'subgroup0'),
('group1', 'subgroup1'),
], names=['groups', 'subgroups'])
return df_mi
def count_per_page(count: int, page_size: int):
"""
>>> count_per_page(12, 7)
[7, 5]
"""
original_count = count
count_per_page = []
while True:
page_count = min(count, page_size)
count_per_page.append(page_count)
count -= page_count
if count == 0:
break
assert sum(count_per_page) == original_count
return count_per_page
def tabulator_column_values(page, col_name: str) -> list[str]:
"""Get the values of a column.
>>> tabulator_column_values(page, 'color')
['blue', 'red']
"""
cells = page.locator(f'[tabulator-field={col_name}][role=gridcell]')
return cells.all_inner_texts()
def test_tabulator_default(page, port, df_mixed, df_mixed_as_string):
nrows, ncols = df_mixed.shape
widget = Tabulator(df_mixed)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expected_ncols = ncols + 2 # _index + index + data columns
# Check that the whole table content is on the page
table = page.locator('.bk.pnx-tabulator.tabulator')
expect(table).to_have_text(
df_mixed_as_string,
use_inner_text=True
)
# Check that the default layout is fitDataTable
assert widget.layout == 'fit_data_table'
assert table.get_attribute('tabulator-layout') == 'fitDataTable'
# Check the table has the right number of rows
rows = page.locator('.tabulator-row')
assert rows.count() == nrows
# Check that the hidden _index column is added by Panel
cols = page.locator(".tabulator-col")
assert cols.count() == expected_ncols
assert cols.nth(0).get_attribute('tabulator-field') == '_index'
assert cols.nth(0).is_hidden()
# Check that the first visible is the index column
assert widget.show_index
assert page.locator('text="index"').is_visible()
assert cols.nth(1).is_visible()
# Check that the columns are sortable by default
assert page.locator(".tabulator-sortable").count() == expected_ncols
# And that none of them is sorted on start
for i in range(expected_ncols):
assert cols.nth(i).get_attribute('aria-sort') == 'none'
def test_tabulator_value_changed(page, port, df_mixed):
widget = Tabulator(df_mixed)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
df_mixed.loc['idx0', 'str'] = 'AA'
# Need to trigger the value as the dataframe was modified
# in place which is not detected.
widget.param.trigger('value')
changed_cell = page.locator('text="AA"')
expect(changed_cell).to_have_count(1)
def test_tabulator_disabled(page, port, df_mixed):
widget = Tabulator(df_mixed, disabled=True)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="A"')
cell.click()
# If the cell was editable then this input element should
# be found.
expect(page.locator('input[type="text"]')).to_have_count(0)
def test_tabulator_show_index_disabled(page, port, df_mixed):
widget = Tabulator(df_mixed, show_index=False)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expect(page.locator('text="index"')).to_have_count(0)
def test_tabulator_titles(page, port, df_mixed):
titles = {col: col.upper() for col in df_mixed.columns}
widget = Tabulator(df_mixed, titles=titles)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
for col in df_mixed.columns:
expected_title = titles[col]
expect(page.locator(f'text="{expected_title}"')).to_have_count(1)
def test_tabulator_hidden_columns(page, port, df_mixed):
widget = Tabulator(df_mixed, hidden_columns=['float', 'date', 'datetime'])
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expected_text = """
index
int
str
bool
idx0
1
A
true
idx1
2
B
true
idx2
3
C
true
idx3
4
D
false
"""
# Check that the whole table content is on the page
table = page.locator('.bk.pnx-tabulator.tabulator')
expect(table).to_have_text(expected_text, use_inner_text=True)
def test_tabulator_buttons_display(page, port, df_mixed):
nrows, ncols = df_mixed.shape
icon_text = 'icon'
widget = Tabulator(df_mixed, buttons={'Print': icon_text})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expected_ncols = ncols + 3 # _index + index + data columns + button col
# Check that an additional column has been added to the table
# with no header title
cols = page.locator(".tabulator-col")
expect(cols).to_have_count(expected_ncols)
button_col_idx = expected_ncols - 1
assert not cols.nth(button_col_idx).get_attribute('tabulator-field')
assert cols.nth(button_col_idx).inner_text() == '\xa0'
assert cols.nth(button_col_idx).is_visible()
# Check the button column has the right content
icons = page.locator(f'text="{icon_text}"')
assert icons.all_inner_texts() == [icon_text] * nrows
# Check the buttons are centered
for i in range(icons.count()):
assert 'text-align: center' in icons.nth(i).get_attribute('style')
def test_tabulator_buttons_event(page, port, df_mixed):
button_col_name = 'Print'
widget = Tabulator(df_mixed, buttons={button_col_name: 'icon'})
state = []
expected_state = [(button_col_name, 0, None)]
def cb(e):
state.append((e.column, e.row, e.value))
widget.on_click(cb)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
icon = page.locator("text=icon").first
icon.wait_for()
# Click on the first button
icon.click()
time.sleep(0.2)
assert state == expected_state
wait_until(lambda: state == expected_state, page)
def test_tabulator_formatters_bokeh_bool(page, port, df_mixed):
s = [True] * len(df_mixed)
s[-1] = False
df_mixed['bool'] = s
widget = Tabulator(df_mixed, formatters={'bool': BooleanFormatter()})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
# The BooleanFormatter renders with svg icons.
cells = page.locator(".tabulator-cell", has=page.locator("svg"))
expect(cells).to_have_count(len(df_mixed))
for i in range(len(df_mixed) - 1):
assert cells.nth(i).get_attribute('aria-checked') == 'true'
assert cells.last.get_attribute('aria-checked') == 'false'
def test_tabulator_formatters_bokeh_date(page, port, df_mixed):
widget = Tabulator(
df_mixed,
formatters={
'date': DateFormatter(format='COOKIE'),
'datetime': DateFormatter(format='%H:%M'),
},
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expect(page.locator('text="10:00"')).to_have_count(1)
assert page.locator('text="Tue, 01 Jan 2019"').count() == 1
@pytest.mark.xfail(
reason='NaNs not well handled by the DateFormatter with datetime.date objects.'
' See https://github.com/bokeh/bokeh/issues/12187'
)
def test_tabulator_formatters_bokeh_date_with_nan(page, port, df_mixed):
df_mixed.loc['idx1', 'date'] = np.nan
df_mixed.loc['idx1', 'datetime'] = np.nan
widget = Tabulator(
df_mixed,
formatters={
'date': DateFormatter(format='COOKIE', nan_format='nan-date'),
'datetime': DateFormatter(format='%H:%M', nan_format= 'nan-datetime'),
},
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expect(page.locator('text="10:00"')).to_have_count(1)
assert page.locator('text="Tue, 01 Jan 2019"').count() == 1 # This should fail
assert page.locator('text="nan-date"').count() == 1
assert page.locator('text="nan-datetime"').count() == 1
def test_tabulator_formatters_bokeh_number(page, port, df_mixed):
df_mixed.loc['idx1', 'int'] = np.nan
df_mixed.loc['idx1', 'float'] = np.nan
widget = Tabulator(
df_mixed,
formatters={
'int': NumberFormatter(format='0.000', nan_format='nan-int'),
'float': NumberFormatter(format='0.000', nan_format='nan-float'),
},
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expect(page.locator('text="1.000"')).to_have_count(1)
assert page.locator('text="3.140"').count() == 1
assert page.locator('text="nan-int"').count() == 1
assert page.locator('text="nan-float"').count() == 1
def test_tabulator_formatters_bokeh_string(page, port, df_mixed):
widget = Tabulator(
df_mixed,
formatters={
'str': StringFormatter(font_style='bold', text_align='center', text_color='red'),
},
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expect(page.locator('text="A"')).to_have_attribute(
"style",
"font-weight: bold; text-align: center; color: rgb(255, 0, 0);"
)
def test_tabulator_formatters_bokeh_html(page, port, df_mixed):
widget = Tabulator(
df_mixed,
formatters={
'str': HTMLTemplateFormatter(template='<p style="font-weight: bold;"><%= value %></p>'),
},
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expect(page.locator('text="A"')).to_have_attribute(
"style",
"font-weight: bold;"
)
def test_tabulator_formatters_bokeh_scientific(page, port, df_mixed):
df_mixed['float'] = df_mixed['float'] * 1e6
df_mixed.loc['idx1', 'float'] = np.nan
widget = Tabulator(
df_mixed,
formatters={
'float': ScientificFormatter(precision=3, nan_format='nan-float'),
},
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expect(page.locator('text="3.140e+6"')).to_have_count(1)
assert page.locator('text="nan-float"').count() == 1
def test_tabulator_formatters_tabulator_str(page, port, df_mixed):
widget = Tabulator(
df_mixed,
formatters={'int': 'star'},
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
# The star formatter renders with svg icons.
cells = page.locator(".tabulator-cell", has=page.locator("svg"))
expect(cells).to_have_count(len(df_mixed))
def test_tabulator_formatters_tabulator_dict(page, port, df_mixed):
nstars = 10
widget = Tabulator(
df_mixed,
formatters={'int': {'type': 'star', 'stars': nstars}},
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
# The star formatter renders with svg icons.
cells = page.locator(".tabulator-cell", has=page.locator("svg"))
expect(cells).to_have_count(len(df_mixed))
stars = page.locator('svg')
assert stars.count() == len(df_mixed) * nstars
def test_tabulator_formatters_after_init(page, port, df_mixed):
widget = Tabulator(df_mixed)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
# Wait until the table is rendered
expect(page.locator('.tabulator-row')).to_have_count(len(df_mixed))
# Formatters can be set after initialization, the table should be
# updated accordingly
widget.formatters = {
'str': HTMLTemplateFormatter(template='<p style="font-weight: bold;"><%= value %></p>'),
}
expect(page.locator('text="A"')).to_have_attribute(
"style",
"font-weight: bold;"
)
def test_tabulator_editors_bokeh_string(page, port, df_mixed):
widget = Tabulator(df_mixed, editors={'str': StringEditor()})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="A"')
cell.click()
# A StringEditor is turned into an input text tabulator editor
expect(page.locator('input[type="text"]')).to_have_count(1)
def test_tabulator_editors_bokeh_string_completions(page, port, df_mixed):
widget = Tabulator(df_mixed, editors={'str': StringEditor(completions=['AAA'])})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="A"')
cell.click()
# A StringEditor with completions is turned into an autocomplete
# tabulator editor.
expect(page.locator('text="AAA"')).to_have_count(1)
def test_tabulator_editors_bokeh_text(page, port, df_mixed):
widget = Tabulator(df_mixed, editors={'str': TextEditor()})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="A"')
cell.click()
# A TextEditor with completions is turned into a textarea
# tabulator editor.
expect(page.locator('textarea')).to_have_count(1)
def test_tabulator_editors_bokeh_int(page, port, df_mixed):
step = 2
widget = Tabulator(df_mixed, editors={'int': IntEditor(step=step)})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="1" >> visible=true')
cell.click()
# An IntEditor with step is turned into a number tabulator editor
# with step respected
input = page.locator('input[type="number"]')
expect(input).to_have_count(1)
assert int(input.get_attribute('step')) == step
def test_tabulator_editors_bokeh_number(page, port, df_mixed):
step = 0.1
widget = Tabulator(df_mixed, editors={'float': NumberEditor(step=step)})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="3.14"')
cell.click()
# A NumberEditor with step is turned into a number tabulator editor
# with step respected
input = page.locator('input[type="number"]')
expect(input).to_have_count(1)
assert input.get_attribute('step') == str(step)
def test_tabulator_editors_bokeh_checkbox(page, port, df_mixed):
widget = Tabulator(df_mixed, editors={'bool': CheckboxEditor()})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="true"').first
cell.click()
# A CheckboxEditor is turned into a tickCross tabulator editor
input = page.locator('input[type="checkbox"]')
expect(input).to_have_count(1)
assert input.get_attribute('value') == "true"
def test_tabulator_editors_bokeh_date(page, port, df_mixed):
widget = Tabulator(df_mixed, editors={'date': DateEditor()})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="2019-01-01"')
cell.click()
# A DateEditor is turned into a Panel date editor
expect(page.locator('input[type="date"]')).to_have_count(1)
def test_tabulator_editors_bokeh_select(page, port, df_mixed):
widget = Tabulator(df_mixed, editors={'str': SelectEditor(options=['option1'])})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="A"')
cell.click()
# A SelectEditor with options is turned into a select tabulator editor.
expect(page.locator('text="option1"')).to_have_count(1)
def test_tabulator_editors_panel_date(page, port, df_mixed):
widget = Tabulator(df_mixed, editors={'date': 'date'})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="2019-01-01"')
cell.click()
# A date editor is turned into an date input
cell_edit = page.locator('input[type="date"]')
new_date = "1980-01-01"
cell_edit.fill(new_date)
# Need to Enter to validate the change
page.locator('input[type="date"]').press('Enter')
expect(page.locator(f'text="{new_date}"')).to_have_count(1)
new_date = dt.datetime.strptime(new_date, '%Y-%m-%d').date()
assert new_date in widget.value['date'].tolist()
cell = page.locator(f'text="{new_date}"')
cell.click()
cell_edit = page.locator('input[type="date"]')
new_date2 = "1990-01-01"
cell_edit.fill(new_date2)
# Escape invalidates the change
page.locator('input[type="date"]').press('Escape')
expect(page.locator(f'text="{new_date2}"')).to_have_count(0)
new_date2 = dt.datetime.strptime(new_date2, '%Y-%m-%d').date()
assert new_date2 not in widget.value['date'].tolist()
def test_tabulator_editors_panel_datetime(page, port, df_mixed):
widget = Tabulator(df_mixed, editors={'datetime': 'datetime'})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="2019-01-01 10:00:00"')
cell.click()
# A date editor is turned into an date input
cell_edit = page.locator('input[type="datetime-local"]')
new_datetime = dt.datetime(1980, 11, 30, 4, 51, 0)
time_to_fill = new_datetime.isoformat()
# Somehow the seconds don't seem to be handled by datetime-local
time_to_fill = time_to_fill[:-3]
cell_edit.fill(time_to_fill)
# Need to Enter to validate the change
page.locator('input[type="datetime-local"]').press('Enter')
new_datetime_display = new_datetime.strftime('%Y-%m-%d %H:%M:%S')
expect(page.locator(f'text="{new_datetime_display}"')).to_have_count(1)
assert new_datetime in widget.value['datetime'].tolist()
cell = page.locator(f'text="{new_datetime_display}"')
cell.click()
cell_edit = page.locator('input[type="datetime-local"]')
new_datetime2 = dt.datetime(1990, 3, 31, 12, 45, 0)
time_to_fill2 = new_datetime2.isoformat()
time_to_fill2 = time_to_fill2[:-3]
cell_edit.fill(time_to_fill2)
# Escape invalidates the change
page.locator('input[type="datetime-local"]').press('Escape')
new_datetime_display2 = new_datetime2.strftime('%Y-%m-%d %H:%M:%S')
expect(page.locator(f'text="{new_datetime_display2}"')).to_have_count(0)
assert new_datetime2 not in widget.value['datetime'].tolist()
def test_tabulator_editors_tabulator_disable_one(page, port, df_mixed):
widget = Tabulator(
df_mixed,
editors={'float': None},
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
page.locator('text="3.14"').click()
page.wait_for_timeout(200)
expect(page.locator('input[type="number"]')).to_have_count(0)
def test_tabulator_editors_tabulator_str(page, port, df_mixed):
widget = Tabulator(df_mixed, editors={'str': 'textarea'})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="A"')
cell.click()
expect(page.locator('textarea')).to_have_count(1)
def test_tabulator_editors_tabulator_dict(page, port, df_mixed):
widget = Tabulator(
df_mixed,
editors={'str': {'type': 'textarea', 'elementAttributes': {'maxlength': '10'}}}
)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cell = page.locator('text="A"')
cell.click()
textarea = page.locator('textarea')
expect(textarea).to_have_count(1)
assert textarea.get_attribute('maxlength') == "10"
def test_tabulator_editors_tabulator_list_default(page, port):
df = pd.DataFrame({'values': ['A', 'B']})
widget = Tabulator(df, header_filters={'values': 'list'})
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
header = page.locator('input[type="search"]')
expect(header).to_have_count(1)
header.click()
# There should be a select element with the list of unique values
# found in the column.
expect(page.locator('.tabulator-edit-list')).to_have_text('AB')
@pytest.mark.parametrize('layout', Tabulator.param['layout'].objects)
def test_tabulator_column_layouts(page, port, df_mixed, layout):
widget = Tabulator(df_mixed, layout=layout)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
layout_mapping = {
"fit_data": "fitData",
"fit_data_fill": "fitDataFill",
"fit_data_stretch": "fitDataStretch",
"fit_data_table": "fitDataTable",
"fit_columns": "fitColumns",
}
expected_layout = layout_mapping[layout]
expect(page.locator('.pnx-tabulator')).to_have_attribute('tabulator-layout', expected_layout)
def test_tabulator_alignment_header_default(page, port, df_mixed):
widget = Tabulator(df_mixed)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
# The default header alignment is left
for col in df_mixed.columns:
expect(page.locator(f'text="{col}"')).to_have_css('text-align', 'left')
def test_tabulator_alignment_text_default(page, port, df_mixed):
widget = Tabulator(df_mixed)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
findex = df_mixed.index[0]
cell = page.locator(f'text="{findex}"')
# Indexes are left aligned
expect(cell).to_have_css('text-align', 'left')
val = df_mixed.at[findex, 'int']
# Selecting the visible 1 as there's a non displayed 1 in the hidden index
cell = page.locator(f'text="{val}" >> visible=true')
# Integers are right aligned
expect(cell).to_have_css('text-align', 'right')
val = df_mixed.at[findex, 'float']
cell = page.locator(f'text="{val}"')
# Floats are right aligned
expect(cell).to_have_css('text-align', 'right')
val = df_mixed.at[findex, 'bool']
val = 'true' if val else 'false'
cell = page.locator(f'text="{val}"').first
# Booleans are centered
expect(cell).to_have_css('text-align', 'center')
val = df_mixed.at[findex, 'datetime']
val = val.strftime('%Y-%m-%d %H:%M:%S')
cell = page.locator(f'text="{val}"')
# Datetimes are right aligned
expect(cell).to_have_css('text-align', 'right')
val = df_mixed.at[findex, 'str']
cell = page.locator(f'text="{val}"')
# Other types are left aligned
expect(cell).to_have_css('text-align', 'left')
def test_tabulator_alignment_header_str(page, port, df_mixed):
halign = 'center'
widget = Tabulator(df_mixed, header_align=halign)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
for col in df_mixed.columns:
expect(page.locator(f'text="{col}"')).to_have_css('text-align', halign)
def test_tabulator_alignment_header_dict(page, port, df_mixed):
halign = {'int': 'left'}
widget = Tabulator(df_mixed, header_align=halign)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
# for col in df_mixed.columns:
for col, align in halign.items():
expect(page.locator(f'text="{col}"')).to_have_css('text-align', align)
def test_tabulator_alignment_text_str(page, port, df_mixed):
talign = 'center'
widget = Tabulator(df_mixed, text_align=talign)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
cells = page.locator('.tabulator-cell:visible')
expect(cells).to_have_count(len(df_mixed) * (df_mixed.shape[1] + 1))
for i in range(cells.count()):
expect(cells.nth(i)).to_have_css('text-align', talign)
def test_tabulator_frozen_columns(page, port, df_mixed):
widths = 100
width = int(((df_mixed.shape[1] + 1) * widths) / 2)
frozen_cols = ['float', 'int']
widget = Tabulator(df_mixed, frozen_columns=frozen_cols, width=width, widths=widths)
serve(widget, port=port, threaded=True, show=False)
time.sleep(0.2)
page.goto(f"http://localhost:{port}")
expected_text = """
float
int
index
str
bool
date
datetime
3.14
1
idx0
A
true
2019-01-01
2019-01-01 10:00:00
6.28
2
idx1
B
true
2020-01-01
2020-01-01 12:00:00
9.42
3
idx2
C
true
2020-01-10
2020-01-10 13:00:00
-2.45
4
idx3
D
false
2019-01-10
2020-01-15 13:00:00
"""
# Check that the whole table content is on the page, it is not in the
# same order as if the table was displayed without frozen columns
table = page.locator('.bk.pnx-tabulator.tabulator')
expect(table).to_have_text(
expected_text,
use_inner_text=True
)