[7f9fb8]: / mne / viz / backends / _notebook.py

Download this file

1620 lines (1356 with data), 51.5 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
"""Notebook implementation of _Renderer and GUI."""
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import os
import os.path as op
import re
from contextlib import contextmanager, nullcontext
from ipyevents import Event
from IPython.display import clear_output, display
from ipywidgets import (
HTML,
Accordion,
BoundedFloatText,
Button,
Checkbox,
Dropdown,
# non-object-based-abstraction-only widgets, remove
FloatSlider,
GridBox,
HBox,
IntProgress,
IntSlider,
IntText,
Label,
Layout,
Play,
RadioButtons,
Select,
Text,
VBox,
Widget,
jsdlink,
link,
)
from ...utils import _soft_import, check_version
from ._abstract import (
_AbstractAction,
_AbstractAppWindow,
_AbstractBrainMplCanvas,
_AbstractButton,
_AbstractCanvas,
_AbstractCheckBox,
_AbstractComboBox,
_AbstractDialog,
_AbstractDock,
_AbstractFileButton,
_AbstractGridLayout,
_AbstractGroupBox,
_AbstractHBoxLayout,
_AbstractKeyPress,
_AbstractLabel,
_AbstractLayout,
_AbstractMenuBar,
_AbstractMplCanvas,
_AbstractMplInterface,
_AbstractPlayback,
_AbstractPlayMenu,
_AbstractPopup,
_AbstractProgressBar,
_AbstractRadioButtons,
_AbstractSlider,
_AbstractSpinBox,
_AbstractStatusBar,
_AbstractText,
_AbstractToolBar,
_AbstractVBoxLayout,
_AbstractWdgt,
_AbstractWidget,
_AbstractWidgetList,
_AbstractWindow,
)
from ._pyvista import (
Plotter,
_check_3d_figure, # noqa: F401
_close_3d_figure, # noqa: F401
_close_all, # noqa: F401
_PyVistaRenderer,
_set_3d_title, # noqa: F401
_set_3d_view, # noqa: F401
_take_3d_screenshot, # noqa: F401
)
from ._utils import _notebook_vtk_works
from .renderer import _TimeInteraction
# dict values are icon names from: https://fontawesome.com/icons
_ICON_LUT = dict(
help="question",
play="play",
pause="pause",
reset="history",
scale="magic",
clear="trash",
movie="video-camera",
restore="replay",
screenshot="camera",
visibility_on="eye",
visibility_off="eye",
folder="folder",
question="question",
information="info",
warning="triangle-exclamation",
critical="exclamation",
)
_BASE_MIN_SIZE = "20px"
_BASE_KWARGS = dict(layout=Layout(min_width=_BASE_MIN_SIZE, min_height=_BASE_MIN_SIZE))
# TODO: We can drop ipyvtklink once we support PyVista 0.38.1+
if check_version("pyvista", "0.38.1"):
_JUPYTER_BACKEND = "trame"
else:
_JUPYTER_BACKEND = "ipyvtklink"
# %%
# Widgets
# -------
# The metaclasses need to share a base class in order for the inheritance
# not to conflict, http://www.phyast.pitt.edu/~micheles/python/metatype.html
# https://stackoverflow.com/questions/28720217/multiple-inheritance-metaclass-conflict
class _BaseWidget(type(_AbstractWidget), type(Widget)):
pass
class _Widget(Widget, _AbstractWidget, metaclass=_BaseWidget):
tooltip = None
def __init__(self):
_AbstractWidget.__init__()
# Widget cannot init because the layouts (HBox, VBox and GridBox) don't
# inherit from Widget like they do analogously for Qt, this isn't an
# issue since each subclass __init__s it's own (e.g. Label)
# Widget.__init__(self)
def _set_range(self, rng):
self.min = rng[0]
self.max = rng[1]
def _show(self):
self.layout.visibility = "visible"
def _hide(self):
self.layout.visibility = "hidden"
def _set_enabled(self, state):
self.disabled = not state
def _is_enabled(self):
return not self.disabled
def _update(self, repaint=True):
pass
def _get_tooltip(self):
return self.tooltip
def _set_tooltip(self, tooltip):
self.tooltip = tooltip
def _set_style(self, style):
for key, val in style.items():
setattr(self.layout, key, val)
def _add_keypress(self, callback):
self._event_watcher = Event(source=self, watched_events=["keydown"])
self._event_watcher.on_dom_event(
lambda event: callback(event["key"].lower().replace("arrow", ""))
)
self._callback = callback
def _trigger_keypress(self, key):
# note: this doesn't actually simulate a keypress, it just calls the
# callback function directly because this is not yet possible
self._callback(key)
def _set_focus(self):
if hasattr(self, "focus"): # added in ipywidgets 8.0
self.focus()
def _set_layout(self, layout):
self.children = (layout,)
def _set_theme(self, theme):
pass
def _set_size(self, width=None, height=None):
if width:
self.layout.width = width
if height:
self.layout.height = height
class _Label(_Widget, _AbstractLabel, Label, metaclass=_BaseWidget):
def __init__(self, value, center=False, selectable=False):
_Widget.__init__(self)
_AbstractLabel.__init__(value, center=center, selectable=selectable)
kwargs = _BASE_KWARGS.copy()
if center:
kwargs["layout"].justify_content = "center"
Label.__init__(self, value=value, disabled=True, **kwargs)
class _Text(_AbstractText, _Widget, Text, metaclass=_BaseWidget):
def __init__(self, value=None, placeholder=None, callback=None):
_AbstractText.__init__(value=value, placeholder=placeholder, callback=callback)
_Widget.__init__(self)
Text.__init__(self, value=value, placeholder=placeholder, **_BASE_KWARGS)
if callback is not None:
self.observe(lambda x: callback(x["new"]), names="value")
def _set_value(self, value):
self.value = value
class _Button(_Widget, _AbstractButton, Button, metaclass=_BaseWidget):
def __init__(self, value, callback, icon=None):
_Widget.__init__(self)
_AbstractButton.__init__(value=value, callback=callback)
Button.__init__(self, description=value, **_BASE_KWARGS)
self.on_click(lambda x: callback())
if icon:
self.icon = _ICON_LUT[icon]
def _click(self):
self.click()
def _set_icon(self, icon):
self.icon = _ICON_LUT[icon]
class _Slider(_Widget, _AbstractSlider, IntSlider, metaclass=_BaseWidget):
def __init__(self, value, rng, callback, horizontal=True):
_Widget.__init__(self)
_AbstractSlider.__init__(
value=value, rng=rng, callback=callback, horizontal=horizontal
)
IntSlider.__init__(
self,
value=int(value),
min=int(rng[0]),
max=int(rng[1]),
readout=False,
orientation="horizontal" if horizontal else "vertical",
**_BASE_KWARGS,
)
self.observe(lambda x: callback(x["new"]), names="value")
def _set_value(self, value):
self.value = value
def _get_value(self):
return self.value
def set_range(self, rng):
self.min = int(rng[0])
self.max = int(rng[1])
class _ProgressBar(_AbstractProgressBar, _Widget, IntProgress, metaclass=_BaseWidget):
def __init__(self, count):
_AbstractProgressBar.__init__(count=count)
_Widget.__init__(self)
IntProgress.__init__(self, max=count, **_BASE_KWARGS)
def _increment(self):
if self.value + 1 > self.max:
return
self.value += 1
return self.value
class _CheckBox(_Widget, _AbstractCheckBox, Checkbox, metaclass=_BaseWidget):
def __init__(self, value, callback):
_Widget.__init__(self)
_AbstractCheckBox.__init__(value=value, callback=callback)
Checkbox.__init__(self, value=value, **_BASE_KWARGS)
self.observe(lambda x: callback(x["new"]), names="value")
def _set_checked(self, checked):
self.value = checked
def _get_checked(self):
return self.value
class _SpinBox(_Widget, _AbstractSpinBox, IntText, metaclass=_BaseWidget):
def __init__(self, value, rng, callback, step=None):
_Widget.__init__(self)
_AbstractSpinBox.__init__(value=value, rng=rng, callback=callback, step=step)
IntText.__init__(self, value=value, min=rng[0], max=rng[1], **_BASE_KWARGS)
if step is not None:
self.step = step
self.observe(lambda x: callback(x["new"]), names="value")
def _set_value(self, value):
self.value = value
def _get_value(self):
return self.value
class _ComboBox(_AbstractComboBox, _Widget, Dropdown, metaclass=_BaseWidget):
def __init__(self, value, items, callback):
_AbstractComboBox.__init__(value=value, items=items, callback=callback)
_Widget.__init__(self)
Dropdown.__init__(self, value=value, options=items, **_BASE_KWARGS)
self.observe(lambda x: callback(x["new"]), names="value")
def _set_value(self, value):
self.value = value
def _get_value(self):
return self.value
class _RadioButtons(
_AbstractRadioButtons, _Widget, RadioButtons, metaclass=_BaseWidget
):
def __init__(self, value, items, callback):
_AbstractRadioButtons.__init__(value=value, items=items, callback=callback)
_Widget.__init__(self)
RadioButtons.__init__(
self, value=value, options=items, disabled=False, **_BASE_KWARGS
)
self.observe(lambda x: callback(x["new"]), names="value")
def _set_value(self, value):
self.value = value
def _get_value(self):
return self.value
class _GroupBox(_AbstractGroupBox, _Widget, Accordion, metaclass=_BaseWidget):
def __init__(self, name, items):
_AbstractGroupBox.__init__(name=name, items=items)
_Widget.__init__(self)
kwargs = _BASE_KWARGS.copy()
kwargs["layout"].min_height = f"{100 * len(items)}px"
self._layout = VBox(**kwargs)
for item in items:
self._layout.children = self._layout.children + (item,)
Accordion.__init__(self, children=[self._layout])
self.set_title(0, name)
self.selected_index = 0
def _set_enabled(self, value):
super()._set_enabled(value)
for child in self._layout.children:
child._set_enabled(value)
# modified from:
# https://gist.github.com/elkhadiy/284900b3ea8a13ed7b777ab93a691719
class _FilePicker:
def __init__(self, rows=20, directory_only=False, ignore_dotfiles=True):
self._callback = None
self._directory_only = directory_only
self._ignore_dotfiles = ignore_dotfiles
self._empty_selection = True
self._selected_dir = os.getcwd()
self._item_layout = Layout(width="auto")
self._nb_rows = rows
self._file_selector = Select(
options=self._get_selector_options(),
rows=min(len(os.listdir(self._selected_dir)), self._nb_rows),
layout=self._item_layout,
)
self._open_button = Button(
description="Open", layout=Layout(flex="auto 1 auto", width="auto")
)
self._select_button = Button(
description="Select", layout=Layout(flex="auto 1 auto", width="auto")
)
self._cancel_button = Button(
description="Cancel", layout=Layout(flex="auto 1 auto", width="auto")
)
self._parent_button = Button(
icon="chevron-up", layout=Layout(flex="auto 1 auto", width="auto")
)
self._selection = Text(
value=op.join(self._selected_dir, self._file_selector.value),
disabled=True,
layout=Layout(flex="1 1 auto", width="auto"),
)
self._filename = Text(value="", layout=Layout(flex="1 1 auto", width="auto"))
self._parent_button.on_click(self._parent_button_clicked)
self._open_button.on_click(self._open_button_clicked)
self._select_button.on_click(self._select_button_clicked)
self._cancel_button.on_click(self._cancel_button_clicked)
self._file_selector.observe(self._update_path)
self._widget = VBox(
[
HBox(
[
self._parent_button,
HTML(value="Look in:"),
self._selection,
]
),
self._file_selector,
HBox(
[
HTML(value="File name"),
self._filename,
self._open_button,
self._select_button,
self._cancel_button,
]
),
]
)
def _get_selector_options(self):
options = os.listdir(self._selected_dir)
if self._ignore_dotfiles:
tmp = list()
for el in options:
if el[0] != ".":
tmp.append(el)
options = tmp
if self._directory_only:
tmp = list()
for el in options:
if op.isdir(op.join(self._selected_dir, el)):
tmp.append(el)
options = tmp
if not options:
options = [""]
self._empty_selection = True
else:
self._empty_selection = False
return options
def _update_selector_options(self):
self._file_selector.options = self._get_selector_options()
self._file_selector.rows = min(
len(os.listdir(self._selected_dir)), self._nb_rows
)
self._selection.value = op.join(self._selected_dir, self._file_selector.value)
self._filename.value = self._file_selector.value
def show(self):
self._update_selector_options()
self._widget.layout.display = "block"
display(self._widget)
def hide(self):
self._widget.layout.display = "none"
def set_directory_only(self, state):
self._directory_only = state
def set_ignore_dotfiles(self, state):
self._ignore_dotfiles = state
def connect(self, callback):
self._callback = callback
def _open_button_clicked(self, button):
if self._empty_selection:
return
if op.isdir(self._selection.value):
self._selected_dir = self._selection.value
self._file_selector.options = self._get_selector_options()
self._file_selector.rows = min(
len(os.listdir(self._selected_dir)), self._nb_rows
)
def _select_button_clicked(self, button):
if self._empty_selection:
return
result = op.join(self._selected_dir, self._filename.value)
if self._callback is not None:
self._callback(result)
# the picker is shared so only one connection is allowed at a time
self._callback = None # reset the callback
self.hide()
def _cancel_button_clicked(self, button):
self._callback = None # reset the callback
self.hide()
def _parent_button_clicked(self, button):
self._selected_dir, _ = op.split(self._selected_dir)
self._update_selector_options()
def _update_path(self, change):
self._selection.value = op.join(self._selected_dir, self._file_selector.value)
self._filename.value = self._file_selector.value
class _FileButton(_AbstractFileButton, _Widget, Button, metaclass=_BaseWidget):
def __init__(
self,
callback,
content_filter=None,
initial_directory=None,
save=False,
is_directory=False,
icon="folder",
window=None,
):
_AbstractFileButton.__init__(
callback=callback,
content_filter=content_filter,
initial_directory=initial_directory,
save=save,
is_directory=is_directory,
)
_Widget.__init__(self)
self._file_picker = _FilePicker()
def fp_callback(x=None):
# Note, in order to display the file picker where the button was,
# the output must be cleared and then redrawn when finished
if window is not None:
clear_output()
self._file_picker.set_directory_only(is_directory)
def callback_with_show(name):
window._show()
callback(name)
self._file_picker.connect(
callback if window is None else callback_with_show
)
self._file_picker.show()
Button.__init__(self, **_BASE_KWARGS)
self.on_click(fp_callback)
self.icon = _ICON_LUT[icon]
class _PlayMenu(_AbstractPlayMenu, _Widget, VBox, metaclass=_BaseWidget):
def __init__(self, value, rng, callback):
_AbstractPlayMenu.__init__(value=value, rng=rng, callback=callback)
_Widget.__init__(self)
kwargs = _BASE_KWARGS.copy()
kwargs["layout"].align_items = "center"
kwargs["layout"].min_height = "100px"
VBox.__init__(self, **kwargs)
self._slider = IntSlider(
value=value, min=rng[0], max=rng[1], readout=False, continuous_update=False
)
self._play_widget = Play(value=value, min=rng[0], max=rng[1], interval=250)
self.children = (self._slider, self._play_widget)
link((self._play_widget, "value"), (self._slider, "value"))
self._slider.observe(lambda x: callback(x["new"]), names="value")
# play, pause, reset and loop require ipywidgets v8.0+ and so are
# not currently tested, will be added upon release
def _play(self):
self.playing = True
def _pause(self):
self.playing = True
def _reset(self):
self.playing = True
self.value = self.min
def _loop(self):
self.repeat = not self.repeat
def _set_value(self, value):
self._slider.value = value
class _Popup(_AbstractPopup, _Widget, VBox, metaclass=_BaseWidget):
def __init__(
self,
title,
text,
info_text=None,
callback=None,
icon="warning",
buttons=None,
window=None,
):
_AbstractPopup.__init__(
self,
title=title,
text=text,
info_text=info_text,
callback=callback,
icon=icon,
buttons=buttons,
window=window,
)
_Widget.__init__(self)
VBox.__init__(self, **_BASE_KWARGS)
if window is not None:
clear_output()
title_label = _Label(title)
title_label._set_style(dict(fontsize="28"))
text_label = _Label(text)
text_label._set_style(dict(fontsize="18"))
self.children = (title_label, text_label)
if info_text:
info_text_label = _Label(info_text)
info_text_label._set_style(dict(fontsize="12"))
self.children += (info_text_label,)
self.icon = _ICON_LUT[icon]
if buttons is None:
buttons = ["Ok"]
hbox = HBox()
self._buttons = dict()
for button in buttons:
def callback_with_show(x):
if window is not None:
clear_output()
window._show()
if callback:
callback(button)
button_widget = Button(description=button)
self._buttons[button] = button_widget
button_widget.on_click(callback_with_show)
hbox.children += (button_widget,)
self.children += (hbox,)
display(self)
def _click(self, value):
self._buttons[value].click()
class _BoxLayout:
def _handle_scroll(self, scroll=None):
kwargs = _BASE_KWARGS.copy()
if scroll is not None:
kwargs["layout"].width = f"{scroll[0]}px"
kwargs["layout"].height = f"{scroll[1]}px"
kwargs["overflow_x"] = "scroll"
kwargs["overflow_y"] = "scroll"
return kwargs
def _add_widget(self, widget):
# if pyvista plotter, needs to be shown
if isinstance(widget, Plotter):
widget = widget.show(jupyter_backend=_JUPYTER_BACKEND, return_viewer=True)
if hasattr(widget, "layout"):
widget.layout.width = None # unlock the fixed layout
widget.layout.margin = "2px 0px 2px 0px"
if not isinstance(widget, Play):
widget.layout.min_width = "0px"
self.children += (widget,)
class _HBoxLayout(
_AbstractHBoxLayout, _BoxLayout, _Widget, HBox, metaclass=_BaseWidget
):
def __init__(self, height=None, scroll=None):
_Widget.__init__(self)
_BoxLayout.__init__(self)
_AbstractHBoxLayout.__init__(self, height=height, scroll=scroll)
HBox.__init__(self, **self._handle_scroll(scroll=scroll))
self._height = height
def _add_widget(self, widget):
_BoxLayout._add_widget(self, widget)
if self._height is not None:
for child in self.children:
child.layout.height = f"{int(self._height / len(self.children))}px"
def _add_stretch(self, amount=1):
self.children += (
self,
_Label(" " * 4),
)
class _VBoxLayout(
_AbstractVBoxLayout, _BoxLayout, _Widget, VBox, metaclass=_BaseWidget
):
def __init__(self, width=None, scroll=None):
_Widget.__init__(self)
_BoxLayout.__init__(self)
_AbstractVBoxLayout.__init__(self, width=width, scroll=scroll)
VBox.__init__(self, **self._handle_scroll(scroll=scroll))
self._width = width
def _add_widget(self, widget):
_BoxLayout._add_widget(self, widget)
if self._width is not None:
for child in self.children:
child.layout.width = f"{int(self._width / len(self.children))}px"
def _add_stretch(self, amount=1):
self.children += (
self,
_Label(" " * 4),
)
class _GridLayout(_AbstractGridLayout, _Widget, GridBox, metaclass=_BaseWidget):
def __init__(self, height=None, width=None):
_Widget.__init__(self)
_AbstractVBoxLayout.__init__(height=height, width=width)
GridBox.__init__(self, **_BASE_KWARGS)
def _add_widget(self, widget, row=None, col=None):
_BoxLayout._add_widget(self, widget)
class _Canvas(_AbstractCanvas, _Widget, HBox, metaclass=_BaseWidget):
def __init__(self, width, height, dpi):
import matplotlib.pyplot as plt
_Widget.__init__(self)
_AbstractCanvas.__init__(self, width=width, height=height, dpi=dpi)
HBox.__init__(self, **_BASE_KWARGS)
with plt.ioff():
self.fig, self.ax = plt.subplots(dpi=dpi)
self.children = (self.fig.canvas,)
def _set_size(self, width=None, height=None):
if width:
self.layout.width = width
if height:
self.layout.height = height
class _AppWindow(_AbstractAppWindow, _Widget, VBox, metaclass=_BaseWidget):
def __init__(self, size=None, fullscreen=False):
_AbstractAppWindow.__init__(self)
_Widget.__init__(self)
VBox.__init__(self, **_BASE_KWARGS)
def _set_central_layout(self, central_layout):
self.children = (central_layout,)
def _close_connect(self, func, *, after=True):
pass
def _close_disconnect(self, after=True):
pass
def _clean(self):
pass
def _get_dpi(self):
return 96
def _get_size(self):
# CSS objects don't have explicit widths and heights
# https://github.com/jupyter-widgets/ipywidgets/issues/1639
return (256, 256)
def _get_cursor(self):
pass
def _set_cursor(self, cursor):
pass
def _new_cursor(self, name):
pass
def _show(self, block=False):
display(self)
def _close(self):
clear_output()
class _3DRenderer(_PyVistaRenderer):
_kind = "notebook"
def __init__(self, *args, **kwargs):
kwargs["notebook"] = True
super().__init__(*args, **kwargs)
if "show" in kwargs and kwargs["show"]:
self.show()
def _update(self):
if _JUPYTER_BACKEND == "ipyvtklink":
if self.figure.display is not None:
self.figure.display.update_canvas()
else:
super()._update()
@contextmanager
def _ensure_minimum_sizes(self):
yield
def show(self):
viewer = self.plotter.show(jupyter_backend=_JUPYTER_BACKEND, return_viewer=True)
viewer.layout.width = None # unlock the fixed layout
display(viewer)
# ------------------------------------
# Non-object-based Widget Abstractions
# ------------------------------------
# These are planned to be removed in favor of the simpler, object-
# oriented abstractions above when time allows.
# modified from:
# https://gist.github.com/elkhadiy/284900b3ea8a13ed7b777ab93a691719
class _FilePckr:
def __init__(self, rows=20, directory_only=False, ignore_dotfiles=True):
self._callback = None
self._directory_only = directory_only
self._ignore_dotfiles = ignore_dotfiles
self._empty_selection = True
self._selected_dir = os.getcwd()
self._item_layout = Layout(width="auto")
self._nb_rows = rows
self._file_selector = Select(
options=self._get_selector_options(),
rows=min(len(os.listdir(self._selected_dir)), self._nb_rows),
layout=self._item_layout,
)
self._open_button = Button(
description="Open", layout=Layout(flex="auto 1 auto", width="auto")
)
self._select_button = Button(
description="Select", layout=Layout(flex="auto 1 auto", width="auto")
)
self._cancel_button = Button(
description="Cancel", layout=Layout(flex="auto 1 auto", width="auto")
)
self._parent_button = Button(
icon="chevron-up", layout=Layout(flex="auto 1 auto", width="auto")
)
self._selection = Text(
value=os.path.join(self._selected_dir, self._file_selector.value),
disabled=True,
layout=Layout(flex="1 1 auto", width="auto"),
)
self._filename = Text(value="", layout=Layout(flex="1 1 auto", width="auto"))
self._parent_button.on_click(self._parent_button_clicked)
self._open_button.on_click(self._open_button_clicked)
self._select_button.on_click(self._select_button_clicked)
self._cancel_button.on_click(self._cancel_button_clicked)
self._file_selector.observe(self._update_path)
self._widget = VBox(
[
HBox(
[
self._parent_button,
HTML(value="Look in:"),
self._selection,
]
),
self._file_selector,
HBox(
[
HTML(value="File name"),
self._filename,
self._open_button,
self._select_button,
self._cancel_button,
]
),
]
)
def _get_selector_options(self):
options = os.listdir(self._selected_dir)
if self._ignore_dotfiles:
tmp = list()
for el in options:
if el[0] != ".":
tmp.append(el)
options = tmp
if self._directory_only:
tmp = list()
for el in options:
if os.path.isdir(os.path.join(self._selected_dir, el)):
tmp.append(el)
options = tmp
if not options:
options = [""]
self._empty_selection = True
else:
self._empty_selection = False
return options
def _update_selector_options(self):
self._file_selector.options = self._get_selector_options()
self._file_selector.rows = min(
len(os.listdir(self._selected_dir)), self._nb_rows
)
self._selection.value = os.path.join(
self._selected_dir, self._file_selector.value
)
self._filename.value = self._file_selector.value
def show(self):
self._update_selector_options()
self._widget.layout.display = "block"
def hide(self):
self._widget.layout.display = "none"
def set_directory_only(self, state):
self._directory_only = state
def set_ignore_dotfiles(self, state):
self._ignore_dotfiles = state
def connect(self, callback):
self._callback = callback
def _open_button_clicked(self, button):
if self._empty_selection:
return
if os.path.isdir(self._selection.value):
self._selected_dir = self._selection.value
self._file_selector.options = self._get_selector_options()
self._file_selector.rows = min(
len(os.listdir(self._selected_dir)), self._nb_rows
)
def _select_button_clicked(self, button):
if self._empty_selection:
return
result = os.path.join(self._selected_dir, self._filename.value)
if self._callback is not None:
self._callback(result)
# the picker is shared so only one connection is allowed at a time
self._callback = None # reset the callback
self.hide()
def _cancel_button_clicked(self, button):
self._callback = None # reset the callback
self.hide()
def _parent_button_clicked(self, button):
self._selected_dir, _ = os.path.split(self._selected_dir)
self._update_selector_options()
def _update_path(self, change):
self._selection.value = os.path.join(
self._selected_dir, self._file_selector.value
)
self._filename.value = self._file_selector.value
class _IpyKeyPress(_AbstractKeyPress):
def _keypress_initialize(self, widget=None):
pass
def _keypress_add(self, shortcut, callback):
pass
def _keypress_trigger(self, shortcut):
pass
class _IpyDialog(_AbstractDialog):
def _dialog_create(
self,
title,
text,
info_text,
callback,
*,
icon="Warning",
buttons=(),
modal=True,
window=None,
):
pass
class _IpyLayout(_AbstractLayout):
def _layout_initialize(self, max_width):
self._layout_max_width = max_width
def _layout_add_widget(self, layout, widget, stretch=0, *, row=None, col=None):
widget.layout.margin = "2px 0px 2px 0px"
if not isinstance(widget, Play):
widget.layout.min_width = "0px"
if isinstance(layout, Accordion):
box = layout.children[0]
else:
box = layout
children = list(box.children)
children.append(widget)
box.children = tuple(children)
# Fix columns
if self._layout_max_width is not None and isinstance(widget, HBox):
children = widget.children
if len(children) > 0:
width = int(self._layout_max_width / len(children))
for child in children:
child.layout.width = f"{width}px"
def _layout_create(self, orientation="vertical"):
if orientation == "vertical":
layout = VBox()
elif orientation == "horizontal":
layout = HBox()
else:
assert orientation == "grid"
layout = GridBox()
return layout
class _IpyDock(_AbstractDock, _IpyLayout):
def _dock_initialize(
self, window=None, name="Controls", area="left", max_width=None
):
if self._docks is None:
self._docks = dict()
current_dock = VBox()
self._dock_width = 302
self._dock = self._dock_layout = current_dock
self._dock.layout.width = f"{self._dock_width}px"
self._layout_initialize(self._dock_width)
self._docks[area] = (self._dock, self._dock_layout)
def _dock_finalize(self):
pass
def _dock_show(self):
self._dock_layout.layout.visibility = "visible"
def _dock_hide(self):
self._dock_layout.layout.visibility = "hidden"
def _dock_add_stretch(self, layout=None):
layout = self._dock_layout if layout is None else layout
widget = HTML(value="", disabled=True)
widget.layout.width = "100%"
self._layout_add_widget(layout, widget)
return _IpyWidget(widget)
def _dock_add_layout(self, vertical=True):
return VBox() if vertical else HBox()
def _dock_add_label(self, value, *, align=False, layout=None, selectable=False):
layout = self._dock_layout if layout is None else layout
widget = HTML(value=value, disabled=True)
widget.layout.width = "100px"
self._layout_add_widget(layout, widget)
return _IpyWidget(widget)
def _dock_add_button(
self,
name,
callback,
*,
style="pushbutton",
icon=None,
tooltip=None,
layout=None,
):
layout = self._dock_layout if layout is None else layout
kwargs = dict()
if style == "pushbutton":
kwargs["description"] = name
if tooltip is not None:
kwargs["tooltip"] = tooltip
widget = Button(**kwargs)
widget.on_click(lambda x: callback())
if icon is not None:
widget.icon = icon
self._layout_add_widget(layout, widget)
return _IpyWidget(widget)
def _dock_named_layout(self, name, *, layout=None, compact=True):
layout = self._dock_layout if layout is None else layout
if name is not None:
hlayout = self._dock_add_layout(not compact)
self._dock_add_label(value=name, align=not compact, layout=hlayout)
self._layout_add_widget(layout, hlayout)
layout = hlayout
return layout
def _dock_add_slider(
self,
name,
value,
rng,
callback,
*,
compact=True,
double=False,
tooltip=None,
layout=None,
):
layout = self._dock_named_layout(name=name, layout=layout, compact=compact)
klass = FloatSlider if double else IntSlider
widget = klass(
value=value,
min=rng[0],
max=rng[1],
readout=False,
)
widget.observe(_generate_callback(callback), names="value")
self._layout_add_widget(layout, widget)
return _IpyWidget(widget)
def _dock_add_check_box(self, name, value, callback, *, tooltip=None, layout=None):
layout = self._dock_layout if layout is None else layout
widget = Checkbox(value=value, description=name, indent=False, disabled=False)
hbox = HBox([widget]) # fix stretching to the right
widget.observe(_generate_callback(callback), names="value")
self._layout_add_widget(layout, hbox)
return _IpyWidget(widget)
def _dock_add_spin_box(
self,
name,
value,
rng,
callback,
*,
compact=True,
double=True,
step=None,
tooltip=None,
layout=None,
):
layout = self._dock_named_layout(name=name, layout=layout, compact=compact)
klass = BoundedFloatText if double else IntText
widget = klass(
value=value,
min=rng[0],
max=rng[1],
)
if step is not None:
widget.step = step
widget.observe(_generate_callback(callback), names="value")
self._layout_add_widget(layout, widget)
return _IpyWidget(widget)
def _dock_add_combo_box(
self, name, value, rng, callback, *, compact=True, tooltip=None, layout=None
):
layout = self._dock_named_layout(name=name, layout=layout, compact=compact)
widget = Dropdown(
value=value,
options=rng,
)
widget.observe(_generate_callback(callback), names="value")
self._layout_add_widget(layout, widget)
return _IpyWidget(widget)
def _dock_add_radio_buttons(
self, value, rng, callback, *, vertical=True, layout=None
):
# XXX: vertical=False is not supported yet
layout = self._dock_layout if layout is None else layout
widget = RadioButtons(
options=rng,
value=value,
disabled=False,
)
widget.observe(_generate_callback(callback), names="value")
self._layout_add_widget(layout, widget)
return _IpyWidgetList(widget)
def _dock_add_group_box(self, name, *, collapse=None, layout=None):
layout = self._dock_layout if layout is None else layout
if collapse is None:
hlayout = VBox([HTML("<strong>" + name + "</strong>")])
else:
assert isinstance(collapse, bool)
vbox = VBox()
hlayout = Accordion([vbox])
hlayout.set_title(0, name)
if collapse:
hlayout.selected_index = None
else:
hlayout.selected_index = 0
self._layout_add_widget(layout, hlayout)
return hlayout
def _dock_add_text(self, name, value, placeholder, *, callback=None, layout=None):
layout = self._dock_layout if layout is None else layout
widget = Text(value=str(value), placeholder=placeholder)
if callback is not None:
widget.observe(_generate_callback(callback), names="value")
self._layout_add_widget(layout, widget)
return _IpyWidget(widget)
def _dock_add_file_button(
self,
name,
desc,
func,
*,
filter_=None,
initial_directory=None,
save=False,
is_directory=False,
icon=False,
tooltip=None,
layout=None,
):
layout = self._dock_layout if layout is None else layout
def callback():
self._file_picker.set_directory_only(is_directory)
self._file_picker.connect(func)
self._file_picker.show()
if icon:
kwargs = dict(style="toolbutton", icon="folder")
else:
kwargs = dict()
widget = self._dock_add_button(
name=desc, callback=callback, tooltip=tooltip, layout=layout, **kwargs
)
return widget
def _generate_callback(callback, to_float=False):
def func(data):
value = data["new"] if "new" in data else data["old"]
callback(float(value) if to_float else value)
return func
class _IpyToolBar(_AbstractToolBar, _IpyLayout):
def _tool_bar_initialize(self, name="default", window=None):
self.actions = dict()
self._tool_bar = self._tool_bar_layout = HBox()
self._layout_initialize(None)
def _tool_bar_add_button(self, name, desc, func, *, icon_name=None, shortcut=None):
icon_name = name if icon_name is None else icon_name
icon = self._icons[icon_name]
if icon is None:
return
widget = Button(tooltip=desc, icon=icon)
widget.layout.width = "50px"
widget.on_click(lambda x: func())
self._layout_add_widget(self._tool_bar_layout, widget)
self.actions[name] = _IpyAction(widget)
def _tool_bar_update_button_icon(self, name, icon_name):
self.actions[name].set_icon(self._icons[icon_name])
def _tool_bar_add_text(self, name, value, placeholder):
widget = Text(value=value, placeholder=placeholder)
self._layout_add_widget(self._tool_bar_layout, widget)
self.actions[name] = _IpyAction(widget)
def _tool_bar_add_spacer(self):
pass
def _tool_bar_add_file_button(self, name, desc, func, *, shortcut=None):
def callback(name=name):
fname = self.actions[f"{name}_field"]._action.value
func(None if len(fname) == 0 else fname)
self._tool_bar_add_text(
name=f"{name}_field",
value=None,
placeholder="Type a file name",
)
self._tool_bar_add_button(
name=name,
desc=desc,
func=callback,
)
def _tool_bar_add_play_button(self, name, desc, func, *, shortcut=None):
widget = Play(interval=500)
self._layout_add_widget(self._tool_bar_layout, widget)
self.actions[name] = _IpyAction(widget)
return _IpyWidget(widget)
class _IpyMenuBar(_AbstractMenuBar):
def _menu_initialize(self, window=None):
self._menus = dict()
self._menu_actions = dict()
self._menu_desc2button = dict() # only for notebook
self._menu_bar = self._menu_bar_layout = HBox()
self._layout_initialize(None)
def _menu_add_submenu(self, name, desc):
widget = Dropdown(value=desc, options=[desc])
self._menus[name] = widget
self._menu_actions[name] = dict()
def callback(input_desc):
if input_desc == desc:
return
button_name = self._menu_desc2button[input_desc]
if button_name in self._menu_actions[name]:
self._menu_actions[name][button_name].trigger()
widget.value = desc
widget.observe(_generate_callback(callback), names="value")
self._layout_add_widget(self._menu_bar_layout, widget)
def _menu_add_button(self, menu_name, name, desc, func):
menu = self._menus[menu_name]
options = list(menu.options)
options.append(desc)
menu.options = options
self._menu_actions[menu_name][name] = _IpyAction(func)
# associate the description with the name given by the user
self._menu_desc2button[desc] = name
class _IpyStatusBar(_AbstractStatusBar, _IpyLayout):
def _status_bar_initialize(self, window=None):
self._status_bar = HBox()
self._layout_initialize(None)
def _status_bar_add_label(self, value, *, stretch=0):
widget = Text(value=value, disabled=True)
self._layout_add_widget(self._status_bar, widget)
return _IpyWidget(widget)
def _status_bar_add_progress_bar(self, stretch=0):
widget = IntProgress()
self._layout_add_widget(self._status_bar, widget)
return _IpyWidget(widget)
def _status_bar_update(self):
pass
class _IpyPlayback(_AbstractPlayback):
def _playback_initialize(self, func, timeout, value, rng, time_widget, play_widget):
play = play_widget._widget
play.min = rng[0]
play.max = rng[1]
play.value = round(value)
slider = time_widget._widget
jsdlink((play, "value"), (slider, "value"))
class _IpyMplInterface(_AbstractMplInterface):
def _mpl_initialize(self):
ipympl = _soft_import("ipympl", "Drawing figures into a notebook.", strict=True)
self.canvas = ipympl.backend_nbagg.Canvas(self.fig)
self.manager = ipympl.backend_nbagg.FigureManager(self.canvas, 0)
class _IpyMplCanvas(_AbstractMplCanvas, _IpyMplInterface):
def __init__(self, width, height, dpi):
super().__init__(width, height, dpi)
self._mpl_initialize()
class _IpyBrainMplCanvas(_AbstractBrainMplCanvas, _IpyMplInterface):
def __init__(self, brain, width, height, dpi):
super().__init__(brain, width, height, dpi)
self._mpl_initialize()
self._connect()
class _IpyWindow(_AbstractWindow):
def _window_initialize(self, *, window=None, central_layout=None, fullscreen=False):
super()._window_initialize()
self._window_load_icons()
def _window_load_icons(self):
# from: https://fontawesome.com/icons
for key in (
"help",
"reset",
"scale",
"clear",
"movie",
"restore",
"screenshot",
"visibility_on",
"visibility_off",
"folder",
): # noqa: E501
self._icons[key] = _ICON_LUT[key]
self._icons["play"] = None
self._icons["pause"] = None
def _window_close_connect(self, func, *, after=True):
pass
def _window_close_disconnect(self, after=True):
pass
def _window_get_dpi(self):
return 96
def _window_get_size(self):
return self.figure.plotter.window_size
def _window_get_simple_canvas(self, width, height, dpi):
return _IpyMplCanvas(width, height, dpi)
def _window_get_mplcanvas(
self, brain, interactor_fraction, show_traces, separate_canvas
):
w, h = self._window_get_mplcanvas_size(interactor_fraction)
self._interactor_fraction = interactor_fraction
self._show_traces = show_traces
self._separate_canvas = separate_canvas
self._mplcanvas = _IpyBrainMplCanvas(brain, w, h, self._window_get_dpi())
return self._mplcanvas
def _window_adjust_mplcanvas_layout(self):
pass
def _window_get_cursor(self):
pass
def _window_set_cursor(self, cursor):
pass
def _window_new_cursor(self, name):
pass
@contextmanager
def _window_ensure_minimum_sizes(self):
yield
def _window_set_theme(self, theme):
pass
def _window_create(self):
pass
# XXX: this could be a VBox if _Renderer.show is refactored
class _IpyWidgetList(_AbstractWidgetList):
def __init__(self, src):
self._src = src
if isinstance(self._src, RadioButtons):
self._widgets = _IpyWidget(self._src)
else:
self._widgets = list()
for widget in self._src:
if not isinstance(widget, _IpyWidget):
widget = _IpyWidget(widget)
self._widgets.append(widget)
def set_enabled(self, state):
if isinstance(self._src, RadioButtons):
self._widgets.set_enabled(state)
else:
for widget in self._widgets:
widget.set_enabled(state)
def get_value(self, idx):
if isinstance(self._src, RadioButtons):
# for consistency, we do not use get_value()
return self._widgets._widget.options[idx]
else:
return self._widgets[idx].get_value()
def set_value(self, idx, value):
if isinstance(self._src, RadioButtons):
self._widgets.set_value(value)
else:
self._widgets[idx].set_value(value)
class _IpyWidget(_AbstractWdgt):
def set_value(self, value):
if isinstance(self._widget, Button):
self._widget.click()
else:
self._widget.value = value
def get_value(self):
return self._widget.value
def set_range(self, rng):
self._widget.min = rng[0]
self._widget.max = rng[1]
def show(self):
self._widget.layout.visibility = "visible"
def hide(self):
self._widget.layout.visibility = "hidden"
def set_enabled(self, state):
self._widget.disabled = not state
def is_enabled(self):
return not self._widget.disabled
def update(self, repaint=True):
pass
def get_tooltip(self):
assert hasattr(self._widget, "tooltip")
return self._widget.tooltip
def set_tooltip(self, tooltip):
assert hasattr(self._widget, "tooltip")
self._widget.tooltip = tooltip
def set_style(self, style):
for key, val in style.items():
setattr(self._widget.layout, key, val)
class _IpyAction(_AbstractAction):
def trigger(self):
if callable(self._action):
self._action()
else: # standard Button widget
self._action.click()
def set_icon(self, icon):
self._action.icon = icon
def set_shortcut(self, shortcut):
pass
class _Renderer(
_PyVistaRenderer,
_IpyDock,
_IpyToolBar,
_IpyMenuBar,
_IpyStatusBar,
_IpyWindow,
_IpyPlayback,
_IpyDialog,
_IpyKeyPress,
_TimeInteraction,
):
_kind = "notebook"
def __init__(self, *args, **kwargs):
self._docks = None
self._menu_bar = None
self._tool_bar = None
self._status_bar = None
self._file_picker = _FilePckr(rows=10)
kwargs["notebook"] = True
fullscreen = kwargs.pop("fullscreen", False)
if not _notebook_vtk_works():
raise RuntimeError(
"Using the notebook backend on Linux requires a compatible "
"VTK setup. Consider using Xfvb or xvfb-run to set up a "
"working virtual display, or install VTK with OSMesa enabled."
)
super().__init__(*args, **kwargs)
self._window_initialize(fullscreen=fullscreen)
def _update(self):
if _JUPYTER_BACKEND == "ipyvtklink":
if self.figure.display is not None:
self.figure.display.update_canvas()
else:
super()._update()
def _display_default_tool_bar(self):
self._tool_bar_initialize()
self._tool_bar_add_file_button(
name="screenshot",
desc="Take a screenshot",
func=self.screenshot,
)
display(self._tool_bar)
def show(self):
# menu bar
if self._menu_bar is not None:
display(self._menu_bar)
# tool bar
if self._tool_bar is not None:
display(self._tool_bar)
else:
self._display_default_tool_bar()
# viewer
viewer = self.plotter.show(jupyter_backend=_JUPYTER_BACKEND, return_viewer=True)
if _JUPYTER_BACKEND == "trame":
# Remove scrollbars, see https://github.com/pyvista/pyvista/pull/4847
# which adds this to the iframe PyVista creates. Once that's merged, this
# workaround just becomes a redundant but is still safe. And in a worst
# (realistic) case, this regex will fail to do any substitution and we just
# live with the ugly 90's-style borders. We can probably remove once we
# require PyVista 0.43 (assuming the above PR is merged).
viewer.value = re.sub(
r" style=[\"'](.+)[\"']></iframe>",
# value taken from matplotlib's widget
r" style='\1; border: 1px solid rgb(221,221,221);' scrolling='no'></iframe>", # noqa: E501
viewer.value,
)
rendering_row = list()
if self._docks is not None and "left" in self._docks:
rendering_row.append(self._docks["left"][0])
rendering_row.append(viewer)
if self._docks is not None and "right" in self._docks:
rendering_row.append(self._docks["right"][0])
display(HBox(rendering_row))
self.figure.display = viewer
# status bar
if self._status_bar is not None:
display(self._status_bar)
# file picker
self._file_picker.hide()
display(self._file_picker._widget)
return self.scene()
_testing_context = nullcontext