-
Notifications
You must be signed in to change notification settings - Fork 225
/
dataframe_viewer.py
925 lines (755 loc) · 34.4 KB
/
dataframe_viewer.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
import sys
import threading
import numpy as np
import pandas as pd
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from pandasgui.utility import get_logger
logger = get_logger(__name__)
class DataFrameViewer(QtWidgets.QWidget):
def __init__(self, df, editable=True):
super().__init__()
self.editable = editable
# Indicates whether the widget has been shown yet. Set to True in
self._loaded = False
if not type(df) == pd.DataFrame:
orig_type = type(df)
df = df.to_frame()
logger.info(
f"DataFrame was automatically converted from {orig_type} to DataFrame"
" for viewing"
)
# Put the df in a wrapper
self.df = df
# Set up DataFrame TableView and Model
self.dataView = DataTableView(parent=self)
# Create headers
self.columnHeader = HeaderView(parent=self, orientation=Qt.Horizontal)
self.indexHeader = HeaderView(parent=self, orientation=Qt.Vertical)
# Set up layout
self.gridLayout = QtWidgets.QGridLayout()
self.setLayout(self.gridLayout)
# Link scrollbars
# Scrolling in data table also scrolls the headers
self.dataView.horizontalScrollBar().valueChanged.connect(
self.columnHeader.horizontalScrollBar().setValue
)
self.dataView.verticalScrollBar().valueChanged.connect(
self.indexHeader.verticalScrollBar().setValue
)
# Scrolling in headers also scrolls the data table
self.columnHeader.horizontalScrollBar().valueChanged.connect(
self.dataView.horizontalScrollBar().setValue
)
self.indexHeader.verticalScrollBar().valueChanged.connect(
self.dataView.verticalScrollBar().setValue
)
self.dataView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.dataView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# Disable scrolling on the headers. Even though the scrollbars are hidden, scrolling by dragging desyncs them
self.indexHeader.horizontalScrollBar().valueChanged.connect(lambda: None)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setSpacing(0)
# Toggle level names
if not (any(df.columns.names) or df.columns.name):
self.columnHeader.verticalHeader().setFixedWidth(0)
if not (any(df.index.names) or df.index.name):
self.indexHeader.horizontalHeader().setFixedHeight(0)
# Add items to layout
self.gridLayout.addWidget(self.columnHeader, 0, 1, 1, 2)
self.gridLayout.addWidget(self.indexHeader, 1, 0, 2, 2)
self.gridLayout.addWidget(self.dataView, 2, 2, 1, 1)
self.gridLayout.addWidget(self.dataView.horizontalScrollBar(), 3, 2, 1, 1)
self.gridLayout.addWidget(self.dataView.verticalScrollBar(), 2, 3, 1, 1)
# These expand when the window is enlarged instead of having the grid squares spread out
self.gridLayout.setColumnStretch(4, 1)
self.gridLayout.setRowStretch(4, 1)
# These placeholders will ensure the size of the blank spaces beside our headers
self.gridLayout.addWidget(
TrackingSpacer(ref_x=self.columnHeader.verticalHeader()), 3, 1, 1, 1
)
self.gridLayout.addWidget(
TrackingSpacer(ref_y=self.indexHeader.horizontalHeader()), 1, 2, 1, 1
)
self.gridLayout.addItem(
QtWidgets.QSpacerItem(
0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding
),
0,
0,
1,
1,
)
# Styling
for header in [self.indexHeader, self.columnHeader]:
header.setStyleSheet(
"background-color: white;"
"selection-color: black;"
"selection-background-color: #EAEAEA;"
)
self.dataView.setStyleSheet(
"background-color: white;"
"alternate-background-color: #F4F6F6;"
"selection-color: black;"
"selection-background-color: #BBDEFB;"
)
for item in [self.dataView, self.columnHeader, self.indexHeader]:
item.setContentsMargins(0, 0, 0, 0)
item.setStyleSheet(item.styleSheet() + "border: 0px solid black;")
item.setItemDelegate(NoFocusDelegate())
def update_df(self, df):
self.df = df
def showEvent(self, event: QtGui.QShowEvent):
"""
Initialize column and row sizes on the first time the widget is shown
"""
if not self._loaded:
# Set column widths
for column_index in range(self.columnHeader.model().columnCount()):
self.auto_size_column(column_index)
# Set row heights
# Just sets a single uniform row height based on the first N rows for performance.
N = 100
default_row_height = 30
for row_index in range(self.indexHeader.model().rowCount())[:N]:
self.auto_size_row(row_index)
height = self.indexHeader.rowHeight(row_index)
default_row_height = max(default_row_height, height)
# Set limit for default row height
default_row_height = min(default_row_height, 100)
self.indexHeader.verticalHeader().setDefaultSectionSize(default_row_height)
self.dataView.verticalHeader().setDefaultSectionSize(default_row_height)
self._loaded = True
event.accept()
def auto_size_column(self, column_index):
"""
Set the size of column at column_index to fit its contents
"""
padding = 20
self.columnHeader.resizeColumnToContents(column_index)
width = self.columnHeader.columnWidth(column_index)
# Iterate over the column's rows and check the width of each to determine the max width for the column
# Only check the first N rows for performance. If there is larger content in cells below it will be cut off
N = 100
for i in range(self.dataView.model().rowCount())[:N]:
mi = self.dataView.model().index(i, column_index)
text = self.dataView.model().data(mi)
w = self.dataView.fontMetrics().boundingRect(text).width()
width = max(width, w)
width += padding
# add maximum allowable column width so column is never too big.
max_allowable_width = 500
width = min(width, max_allowable_width)
self.columnHeader.setColumnWidth(column_index, width)
self.dataView.setColumnWidth(column_index, width)
self.dataView.updateGeometry()
self.columnHeader.updateGeometry()
def auto_size_row(self, row_index):
"""
Set the size of row at row_index to fix its contents
"""
padding = 20
self.indexHeader.resizeRowToContents(row_index)
height = self.indexHeader.rowHeight(row_index)
# Iterate over the row's columns and check the width of each to determine the max height for the row
# Only check the first N columns for performance.
N = 100
for i in range(min(N, self.dataView.model().columnCount())):
mi = self.dataView.model().index(row_index, i)
cell_width = self.columnHeader.columnWidth(i)
text = self.dataView.model().data(mi)
# Gets row height at a constrained width (the column width).
# This constrained width, with the flag of Qt.TextWordWrap
# gets the height the cell would have to be to fit the text.
constrained_rect = QtCore.QRect(0, 0, cell_width, 0)
h = (
self.dataView.fontMetrics()
.boundingRect(constrained_rect, Qt.TextWordWrap, text)
.height()
)
height = max(height, h)
height += padding
self.indexHeader.setRowHeight(row_index, height)
self.dataView.setRowHeight(row_index, height)
self.dataView.updateGeometry()
self.indexHeader.updateGeometry()
def keyPressEvent(self, event):
QtWidgets.QWidget.keyPressEvent(self, event)
if event.matches(QtGui.QKeySequence.Copy):
print("Ctrl + C")
self.dataView.copy()
if event.matches(QtGui.QKeySequence.Paste):
self.dataView.paste()
print("Ctrl + V")
if event.key() == Qt.Key_P and (event.modifiers() & Qt.ControlModifier):
self.dataView.print()
print("Ctrl + P")
if event.key() == Qt.Key_D and (event.modifiers() & Qt.ControlModifier):
self.debug()
print("Ctrl + D")
def data_changed(self):
# Call dataChanged on all models for all data
for model in [
self.dataView.model(),
self.columnHeader.model(),
self.indexHeader.model(),
]:
model.dataChanged.emit(
model.index(0, 0), model.index(model.rowCount(), model.columnCount())
)
# Remove dotted border on cell focus. https://stackoverflow.com/a/55252650/3620725
class NoFocusDelegate(QtWidgets.QStyledItemDelegate):
def paint(
self,
painter: QtGui.QPainter,
item: QtWidgets.QStyleOptionViewItem,
ix: QtCore.QModelIndex,
):
if item.state & QtWidgets.QStyle.State_HasFocus:
item.state = item.state ^ QtWidgets.QStyle.State_HasFocus
super().paint(painter, item, ix)
class DataTableModel(QtCore.QAbstractTableModel):
"""
Model for DataTableView to connect for DataFrame data
"""
def __init__(self, parent):
super().__init__(parent)
def headerData(self, section, orientation, role=None):
# Headers for DataTableView are hidden. Header data is shown in HeaderView
pass
def columnCount(self, parent=None):
if type(self.parent().df) == pd.Series:
return 1
else:
return self.parent().df.columns.shape[0]
def rowCount(self, parent=None):
return len(self.parent().df)
# Returns the data from the DataFrame
def data(self, index, role=QtCore.Qt.DisplayRole):
if (
role == QtCore.Qt.DisplayRole
or role == QtCore.Qt.EditRole
or role == QtCore.Qt.ToolTipRole
):
row = index.row()
col = index.column()
cell = self.parent().df.iloc[row, col]
# NaN case
if pd.isnull(cell):
return ""
# Float formatting
if isinstance(cell, (float, np.floating)):
if not role == QtCore.Qt.ToolTipRole:
return "{:.4f}".format(cell)
return str(cell)
elif role == QtCore.Qt.ToolTipRole:
row = index.row()
col = index.column()
cell = self.parent().df.iloc[row, col]
# NaN case
if pd.isnull(cell):
return "NaN"
return str(cell)
def flags(self, index):
if self.parent().editable:
return (
QtCore.Qt.ItemIsEditable
| QtCore.Qt.ItemIsEnabled
| QtCore.Qt.ItemIsSelectable
)
else:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
# Set data in the DataFrame. Required if table is editable
def setData(self, index, value, role=None):
if role == QtCore.Qt.EditRole:
row = index.row()
col = index.column()
try:
self.parent().df.iat[row, col] = value
except Exception as e:
print(e)
return False
self.dataChanged.emit(index, index)
return True
class DataTableView(QtWidgets.QTableView):
"""
Displays the DataFrame data as a table
"""
def __init__(self, parent):
super().__init__(parent)
# Create and set model
model = DataTableModel(parent)
self.setModel(model)
# Hide the headers. The DataFrame headers (index & columns) will be displayed in the DataFrameHeaderViews
self.horizontalHeader().hide()
self.verticalHeader().hide()
# Link selection to headers
self.selectionModel().selectionChanged.connect(self.on_selectionChanged)
# Settings
# self.setWordWrap(True)
# self.resizeRowsToContents()
self.setAlternatingRowColors(True)
self.setHorizontalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
self.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
def on_selectionChanged(self):
"""
Runs when cells are selected in the main table. This logic highlights the correct cells in the vertical and
horizontal headers when a data cell is selected
"""
columnHeader = self.parent().columnHeader
indexHeader = self.parent().indexHeader
# The two blocks below check what columns or rows are selected in the data table and highlights the
# corresponding ones in the two headers. The if statements check for focus on headers, because if the user
# clicks a header that will auto-select all cells in that row or column which will trigger this function
# and cause and infinite loop
if not columnHeader.hasFocus():
selection = self.selectionModel().selection()
columnHeader.selectionModel().select(
selection,
QtCore.QItemSelectionModel.Columns
| QtCore.QItemSelectionModel.ClearAndSelect,
)
if not indexHeader.hasFocus():
selection = self.selectionModel().selection()
indexHeader.selectionModel().select(
selection,
QtCore.QItemSelectionModel.Rows
| QtCore.QItemSelectionModel.ClearAndSelect,
)
def copy(self):
"""
Copy the selected cells to clipboard in an Excel-pasteable format
"""
# Get the bounds using the top left and bottom right selected cells
indexes = self.selectionModel().selection().indexes()
rows = [ix.row() for ix in indexes]
cols = [ix.column() for ix in indexes]
df = self.parent().df.iloc[min(rows) : max(rows) + 1, min(cols) : max(cols) + 1]
# If I try to use Pyperclip without starting new thread large values give access denied error
def thread_function(df):
df.to_clipboard(index=False, header=False)
threading.Thread(target=thread_function, args=(df,)).start()
def paste(self):
# Set up clipboard object
app = QtWidgets.QApplication.instance()
if not app:
app = QtWidgets.QApplication(sys.argv)
clipboard = app.clipboard()
# TODO
print(clipboard.text())
def sizeHint(self):
# Set width and height based on number of columns in model
# Width
width = 2 * self.frameWidth() # Account for border & padding
# width += self.verticalScrollBar().width() # Dark theme has scrollbars always shown
for i in range(self.model().columnCount()):
width += self.columnWidth(i)
# Height
height = 2 * self.frameWidth() # Account for border & padding
# height += self.horizontalScrollBar().height() # Dark theme has scrollbars always shown
for i in range(self.model().rowCount()):
height += self.rowHeight(i)
return QtCore.QSize(width, height)
class HeaderModel(QtCore.QAbstractTableModel):
"""
Model for HeaderView
"""
def __init__(self, parent, orientation):
super().__init__(parent)
self.orientation = orientation
def columnCount(self, parent=None):
if self.orientation == Qt.Horizontal:
return self.parent().df.columns.shape[0]
else: # Vertical
return self.parent().df.index.nlevels
def rowCount(self, parent=None):
if self.orientation == Qt.Horizontal:
return self.parent().df.columns.nlevels
elif self.orientation == Qt.Vertical:
return self.parent().df.index.shape[0]
def data(self, index, role=None):
row = index.row()
col = index.column()
if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.ToolTipRole:
if self.orientation == Qt.Horizontal:
if type(self.parent().df.columns) == pd.MultiIndex:
return str(self.parent().df.columns.values[col][row])
else:
return str(self.parent().df.columns.values[col])
elif self.orientation == Qt.Vertical:
if type(self.parent().df.index) == pd.MultiIndex:
return str(self.parent().df.index.values[row][col])
else:
return str(self.parent().df.index.values[row])
# The headers of this table will show the level names of the MultiIndex
def headerData(self, section, orientation, role=None):
if role in [QtCore.Qt.DisplayRole, QtCore.Qt.ToolTipRole]:
if self.orientation == Qt.Horizontal and orientation == Qt.Vertical:
if type(self.parent().df.columns) == pd.MultiIndex:
return str(self.parent().df.columns.names[section])
else:
return str(self.parent().df.columns.name)
elif self.orientation == Qt.Vertical and orientation == Qt.Horizontal:
if type(self.parent().df.index) == pd.MultiIndex:
return str(self.parent().df.index.names[section])
else:
return str(self.parent().df.index.name)
else:
return None # These cells should be hidden anyways
class HeaderView(QtWidgets.QTableView):
"""
Displays the DataFrame index or columns depending on orientation
"""
def __init__(self, parent: DataFrameViewer, orientation):
super().__init__(parent)
# Setup
self.orientation = orientation
self.table = parent.dataView
self.setModel(HeaderModel(parent, orientation))
# These are used during column resizing
self.header_being_resized = None
self.resize_start_position = None
self.initial_header_size = None
# Events
self.clicked.connect(self.on_clicked)
# Handled by self.eventFilter()
self.setMouseTracking(True)
self.viewport().setMouseTracking(True)
self.viewport().installEventFilter(self)
# Settings
self.setSizePolicy(
QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum
)
)
self.setWordWrap(False)
self.setFont(QtGui.QFont("Times", weight=QtGui.QFont.Bold))
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
self.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
# Link selection to DataTable
self.selectionModel().selectionChanged.connect(self.on_selectionChanged)
self.setSpans()
self.initSize()
# Orientation specific settings
if orientation == Qt.Horizontal:
self.setHorizontalScrollBarPolicy(
Qt.ScrollBarAlwaysOff
) # Scrollbar is replaced in DataFrameViewer
self.horizontalHeader().hide()
self.verticalHeader().setDisabled(True)
self.verticalHeader().setHighlightSections(
False
) # Selection lags a lot without this
else:
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.verticalHeader().hide()
self.horizontalHeader().setDisabled(True)
self.horizontalHeader().setHighlightSections(
False
) # Selection lags a lot without this
# Set initial size
self.resize(self.sizeHint())
def on_clicked(self, ix: QtCore.QModelIndex):
# When a header is clicked, sort the DataFrame by that column
if self.orientation == Qt.Horizontal:
df = self.parent().df
df_sorted = df.sort_values(df.columns[ix.column()], kind="mergesort")
self.parent().update_df(df_sorted)
self.parent().data_changed()
# Header
def on_selectionChanged(self):
"""
Runs when cells are selected in the Header. This selects columns in the data table when the header is clicked,
and then calls selectAbove()
"""
# Check focus so we don't get recursive loop, since headers trigger selection of data cells and vice versa
if self.hasFocus():
dataView = self.parent().dataView
# Set selection mode so selecting one row or column at a time adds to selection each time
if (
self.orientation == Qt.Horizontal
): # This case is for the horizontal header
# Get the header's selected columns
selection = self.selectionModel().selection()
# Removes the higher levels so that only the lowest level of the header affects the data table selection
last_row_ix = self.parent().df.columns.nlevels - 1
last_col_ix = self.model().columnCount() - 1
higher_levels = QtCore.QItemSelection(
self.model().index(0, 0),
self.model().index(last_row_ix - 1, last_col_ix),
)
selection.merge(higher_levels, QtCore.QItemSelectionModel.Deselect)
# Select the cells in the data view
dataView.selectionModel().select(
selection,
QtCore.QItemSelectionModel.Columns
| QtCore.QItemSelectionModel.ClearAndSelect,
)
if self.orientation == Qt.Vertical:
selection = self.selectionModel().selection()
last_row_ix = self.model().rowCount() - 1
last_col_ix = self.parent().df.index.nlevels - 1
higher_levels = QtCore.QItemSelection(
self.model().index(0, 0),
self.model().index(last_row_ix, last_col_ix - 1),
)
selection.merge(higher_levels, QtCore.QItemSelectionModel.Deselect)
dataView.selectionModel().select(
selection,
QtCore.QItemSelectionModel.Rows
| QtCore.QItemSelectionModel.ClearAndSelect,
)
self.selectAbove()
# Take the current set of selected cells and make it so that any spanning cell above a selected cell is selected too
# This should happen after every selection change
def selectAbove(self):
if self.orientation == Qt.Horizontal:
if self.parent().df.columns.nlevels == 1:
return
else:
if self.parent().df.index.nlevels == 1:
return
for ix in self.selectedIndexes():
if self.orientation == Qt.Horizontal:
# Loop over the rows above this one
for row in range(ix.row()):
ix2 = self.model().index(row, ix.column())
self.setSelection(
self.visualRect(ix2), QtCore.QItemSelectionModel.Select
)
else:
# Loop over the columns left of this one
for col in range(ix.column()):
ix2 = self.model().index(ix.row(), col)
self.setSelection(
self.visualRect(ix2), QtCore.QItemSelectionModel.Select
)
# Fits columns to contents but with a minimum width and added padding
def initSize(self):
padding = 20
if self.orientation == Qt.Horizontal:
min_size = 100
self.resizeColumnsToContents()
for col in range(self.model().columnCount()):
width = self.columnWidth(col)
if width + padding < min_size:
new_width = min_size
else:
new_width = width + padding
self.setColumnWidth(col, new_width)
self.table.setColumnWidth(col, new_width)
else:
max_size = 1000
self.resizeColumnsToContents()
for col in range(self.model().columnCount()):
width = self.columnWidth(col)
self.setColumnWidth(col, width + padding)
# This sets spans to group together adjacent cells with the same values
def setSpans(self):
df = self.parent().df
# Find spans for horizontal HeaderView
if self.orientation == Qt.Horizontal:
# Find how many levels the MultiIndex has
if type(df.columns) == pd.MultiIndex:
N = len(df.columns[0])
else:
N = 1
for level in range(N): # Iterates over the levels
# Find how many segments the MultiIndex has
if type(df.columns) == pd.MultiIndex:
arr = [df.columns[i][level] for i in range(len(df.columns))]
else:
arr = df.columns
# Holds the starting index of a range of equal values.
# None means it is not currently in a range of equal values.
match_start = None
for col in range(1, len(arr)): # Iterates over cells in row
# Check if cell matches cell to its left
if arr[col] == arr[col - 1]:
if match_start is None:
match_start = col - 1
# If this is the last cell, need to end it
if col == len(arr) - 1:
match_end = col
span_size = match_end - match_start + 1
self.setSpan(level, match_start, 1, span_size)
else:
if match_start is not None:
match_end = col - 1
span_size = match_end - match_start + 1
self.setSpan(level, match_start, 1, span_size)
match_start = None
# Find spans for vertical HeaderView
else:
# Find how many levels the MultiIndex has
if type(df.index) == pd.MultiIndex:
N = len(df.index[0])
else:
N = 1
for level in range(N): # Iterates over the levels
# Find how many segments the MultiIndex has
if type(df.index) == pd.MultiIndex:
arr = [df.index[i][level] for i in range(len(df.index))]
else:
arr = df.index
# Holds the starting index of a range of equal values.
# None means it is not currently in a range of equal values.
match_start = None
for row in range(1, len(arr)): # Iterates over cells in column
# Check if cell matches cell above
if arr[row] == arr[row - 1]:
if match_start is None:
match_start = row - 1
# If this is the last cell, need to end it
if row == len(arr) - 1:
match_end = row
span_size = match_end - match_start + 1
self.setSpan(match_start, level, span_size, 1)
else:
if match_start is not None:
match_end = row - 1
span_size = match_end - match_start + 1
self.setSpan(match_start, level, span_size, 1)
match_start = None
def over_header_edge(self, mouse_position, margin=3):
# Return the index of the column this x position is on the right edge of
if self.orientation == Qt.Horizontal:
x = mouse_position
if self.columnAt(x - margin) != self.columnAt(x + margin):
if self.columnAt(x + margin) == 0:
# We're at the left edge of the first column
return None
else:
return self.columnAt(x - margin)
else:
return None
# Return the index of the row this y position is on the top edge of
elif self.orientation == Qt.Vertical:
y = mouse_position
if self.rowAt(y - margin) != self.rowAt(y + margin):
if self.rowAt(y + margin) == 0:
# We're at the top edge of the first row
return None
else:
return self.rowAt(y - margin)
else:
return None
def eventFilter(self, object: QtCore.QObject, event: QtCore.QEvent):
# If mouse is on an edge, start the drag resize process
if event.type() == QtCore.QEvent.MouseButtonPress:
if self.orientation == Qt.Horizontal:
mouse_position = event.pos().x()
elif self.orientation == Qt.Vertical:
mouse_position = event.pos().y()
if self.over_header_edge(mouse_position) is not None:
self.header_being_resized = self.over_header_edge(mouse_position)
self.resize_start_position = mouse_position
if self.orientation == Qt.Horizontal:
self.initial_header_size = self.columnWidth(
self.header_being_resized
)
elif self.orientation == Qt.Vertical:
self.initial_header_size = self.rowHeight(self.header_being_resized)
return True
else:
self.header_being_resized = None
# End the drag process
if event.type() == QtCore.QEvent.MouseButtonRelease:
self.header_being_resized = None
# Auto size the column that was double clicked
if event.type() == QtCore.QEvent.MouseButtonDblClick:
if self.orientation == Qt.Horizontal:
mouse_position = event.pos().x()
elif self.orientation == Qt.Vertical:
mouse_position = event.pos().y()
# Find which column or row edge the mouse was over and auto size it
if self.over_header_edge(mouse_position) is not None:
header_index = self.over_header_edge(mouse_position)
if self.orientation == Qt.Horizontal:
self.parent().auto_size_column(header_index)
elif self.orientation == Qt.Vertical:
self.parent().auto_size_row(header_index)
return True
# Handle active drag resizing
if event.type() == QtCore.QEvent.MouseMove:
if self.orientation == Qt.Horizontal:
mouse_position = event.pos().x()
elif self.orientation == Qt.Vertical:
mouse_position = event.pos().y()
# If this is None, there is no drag resize happening
if self.header_being_resized is not None:
size = self.initial_header_size + (
mouse_position - self.resize_start_position
)
if size > 10:
if self.orientation == Qt.Horizontal:
self.setColumnWidth(self.header_being_resized, size)
self.parent().dataView.setColumnWidth(
self.header_being_resized, size
)
if self.orientation == Qt.Vertical:
self.setRowHeight(self.header_being_resized, size)
self.parent().dataView.setRowHeight(
self.header_being_resized, size
)
self.updateGeometry()
self.parent().dataView.updateGeometry()
return True
# Set the cursor shape
if self.over_header_edge(mouse_position) is not None:
if self.orientation == Qt.Horizontal:
self.viewport().setCursor(QtGui.QCursor(Qt.SplitHCursor))
elif self.orientation == Qt.Vertical:
self.viewport().setCursor(QtGui.QCursor(Qt.SplitVCursor))
else:
self.viewport().setCursor(QtGui.QCursor(Qt.ArrowCursor))
return False
# Return the size of the header needed to match the corresponding DataTableView
def sizeHint(self):
# Horizontal HeaderView
if self.orientation == Qt.Horizontal:
# Width of DataTableView
width = self.table.sizeHint().width() + self.verticalHeader().width()
# Height
height = 2 * self.frameWidth() # Account for border & padding
for i in range(self.model().rowCount()):
height += self.rowHeight(i)
# Vertical HeaderView
else:
# Height of DataTableView
height = self.table.sizeHint().height() + self.horizontalHeader().height()
# Width
width = 2 * self.frameWidth() # Account for border & padding
for i in range(self.model().columnCount()):
width += self.columnWidth(i)
return QtCore.QSize(width, height)
# This is needed because otherwise when the horizontal header is a single row it will add whitespace to be bigger
def minimumSizeHint(self):
if self.orientation == Qt.Horizontal:
return QtCore.QSize(0, self.sizeHint().height())
else:
return QtCore.QSize(self.sizeHint().width(), 0)
# This is a fixed size widget with a size that tracks some other widget
class TrackingSpacer(QtWidgets.QFrame):
def __init__(self, ref_x=None, ref_y=None):
super().__init__()
self.ref_x = ref_x
self.ref_y = ref_y
def minimumSizeHint(self):
width = 0
height = 0
if self.ref_x:
width = self.ref_x.width()
if self.ref_y:
height = self.ref_y.height()
return QtCore.QSize(width, height)
# Examples
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
from pandasgui.datasets import pokemon
# view = DataFrameViewer(pokemon)
# view.show()
view2 = DataFrameViewer(pokemon)
view2.show()
app.exec_()