-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1429 lines (1240 loc) · 56.5 KB
/
main.py
File metadata and controls
1429 lines (1240 loc) · 56.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
import tkinter as tk
from tkinter import ttk, filedialog, messagebox, font as tkFont
import threading
import bisect
import sys
import os
import subprocess
import time
import json
import sqlite3
import pandas as pd
# High DPI support for Windows
try:
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except Exception:
pass
CONFIG_PATH = os.path.expanduser("~/.table-viewer.json")
class FilterDialog(tk.Toplevel):
"""Filter dialog with column selection and regex support."""
def __init__(self, parent, columns, default_column=None):
super().__init__(parent)
self.title("Find/Filter")
self.transient(parent)
self.resizable(False, False)
self.result = None
self.geometry(f"+{parent.winfo_rootx() + 80}+{parent.winfo_rooty() + 80}")
tk.Label(self, text="Search:").grid(row=0, column=0, padx=8, pady=(10, 4), sticky="w")
self.entry = tk.Entry(self, width=36)
self.entry.grid(row=0, column=1, columnspan=2, padx=8, pady=(10, 4), sticky="ew")
self.entry.focus_set()
tk.Label(self, text="In column:").grid(row=1, column=0, padx=8, pady=4, sticky="w")
self.column_var = tk.StringVar(value=default_column or "All columns")
ttk.Combobox(self, textvariable=self.column_var,
values=["All columns"] + list(columns), state="readonly").grid(
row=1, column=1, columnspan=2, padx=8, pady=4, sticky="ew")
self.regex_var = tk.BooleanVar(value=False)
tk.Checkbutton(self, text="Regular expression", variable=self.regex_var).grid(
row=2, column=0, columnspan=3, padx=8, pady=4, sticky="w")
btn_frame = tk.Frame(self)
btn_frame.grid(row=3, column=0, columnspan=3, pady=(4, 10))
tk.Button(btn_frame, text="Filter", width=10, command=self._ok).pack(side="left", padx=4)
tk.Button(btn_frame, text="Cancel", width=10, command=self.destroy).pack(side="left", padx=4)
self.bind("<Return>", lambda e: self._ok())
self.bind("<Escape>", lambda e: self.destroy())
self.grab_set()
self.wait_window()
def _ok(self):
term = self.entry.get()
if term:
col = self.column_var.get()
self.result = (term, None if col == "All columns" else col, self.regex_var.get())
self.destroy()
class TableViewer(tk.Tk):
"""A fast CSV viewer with virtualized canvas rendering and two-stage loading."""
def __init__(self):
super().__init__()
self.title("Table Viewer")
self.modifier = "Command" if sys.platform == "darwin" else "Control"
self._setup_theme_colors()
# Fonts
self._font_size = 10
self._font_family = "Segoe UI" if os.name == "nt" else "Helvetica"
self._mono_family = "Consolas" if os.name == "nt" else "Courier New"
self.default_font = tkFont.Font(family=self._font_family, size=self._font_size)
self.mono_font = tkFont.Font(family=self._mono_family, size=self._font_size)
self.header_font = tkFont.Font(family=self._font_family, size=self._font_size, weight="bold")
self._default_char_width = self.default_font.measure("x")
self._mono_char_width = self.mono_font.measure("0")
# Data
self.file_path = None
self.original_data = pd.DataFrame()
self.view_df = pd.DataFrame()
self.column_names = []
self.sort_info = {"col_index": None, "ascending": True}
self.col_alignments = {}
self.col_types = {}
self.selected_cell = {"row": None, "col": None}
# SQLite state
self._sqlite_tables = []
self._sqlite_current_table = None
# Virtual grid
self.row_height = 25
self.col_widths = {}
self._col_offsets = [0]
self.total_rows = 0
self.features_ready = False
self._redraw_job = None
self._gutter_width = 50
# Vertical scroll position (not canvas-based — we manage it ourselves)
self._first_row = 0
# Pool: items at FIXED screen positions. Slot i always sits at
# y = i * row_height. Scrolling only changes text content.
self._pool_rows = 0
self._pool_cols = 0
self._pool_stripes = [] # [slot] -> rect id
self._pool_sel = None # single rect id
self._pool_texts = [] # [slot][col] -> text id
self._pool_vlines = [] # [slot][col] -> line id
self._pool_hlines = [] # [slot] -> line id
# Gutter pool (same approach)
self._gpool_rows = 0
self._gpool_stripes = []
self._gpool_texts = []
self._gpool_hlines = []
# Threads
self.sort_thread = None
self.filter_thread = None
self.auto_fit_thread = None
self._load_thread = None
# Column resizing state
self.resizing_col_index = None
self.resize_start_x = 0
self.initial_col_width = 0
self.potential_sort_click = False
self.last_press_time = 0
self.last_press_col = None
self._load_config()
self.config(bg=self.BACKGROUND_COLOR)
self._create_menu()
self._create_widgets()
self.protocol("WM_DELETE_WINDOW", self._on_close)
# ================================================================
# Config
# ================================================================
def _load_config(self):
self._recent_files = []
try:
with open(CONFIG_PATH, "r") as f:
cfg = json.load(f)
geo = cfg.get("geometry")
if geo:
self.geometry(geo)
self._recent_files = cfg.get("recent", [])
return
except Exception:
pass
sw, sh = self.winfo_screenwidth(), self.winfo_screenheight()
w, h = int(sw * 0.75), int(sh * 0.8)
self.geometry(f"{w}x{h}+{(sw - w) // 2}+{(sh - h) // 2}")
def _save_config(self):
try:
with open(CONFIG_PATH, "w") as f:
json.dump({"geometry": self.geometry(),
"recent": self._recent_files[:10]}, f)
except Exception:
pass
def _add_recent(self, path):
path = os.path.abspath(path)
if path in self._recent_files:
self._recent_files.remove(path)
self._recent_files.insert(0, path)
self._recent_files = self._recent_files[:10]
self._rebuild_recent_menu()
def _rebuild_recent_menu(self):
self._recent_menu.delete(0, "end")
for path in self._recent_files:
label = os.path.basename(path)
self._recent_menu.add_command(
label=label, command=lambda p=path: self.load_file_from_path(p))
if not self._recent_files:
self._recent_menu.add_command(label="(none)", state="disabled")
# ================================================================
# Theme
# ================================================================
def _setup_theme_colors(self):
style = ttk.Style(self)
try:
style.theme_use("clam")
except tk.TclError:
pass
is_dark = False
if sys.platform == "darwin":
try:
r = subprocess.run(["defaults", "read", "-g", "AppleInterfaceStyle"],
capture_output=True, text=True)
is_dark = r.stdout.strip() == "Dark"
except Exception:
pass
if is_dark:
self.BACKGROUND_COLOR = "#2e2e2e"
self.FOREGROUND_COLOR = "#dcdcdc"
self.SELECTION_COLOR = "#4a6fa5"
self.GRID_LINE_COLOR = "#4a4a4a"
self.HEADER_BG = "#404040"
self.HEADER_FG = "#ffffff"
self.STRIPE_COLOR = "#353535"
self.GUTTER_BG = "#383838"
else:
self.BACKGROUND_COLOR = "#ffffff"
self.FOREGROUND_COLOR = "#000000"
self.SELECTION_COLOR = "#a6d1ff"
self.GRID_LINE_COLOR = "#e0e0e0"
self.HEADER_BG = "#e1e1e1"
self.HEADER_FG = "#000000"
self.STRIPE_COLOR = "#f7f7f7"
self.GUTTER_BG = "#f0f0f0"
# ================================================================
# Menu
# ================================================================
def _create_menu(self):
menu_bar = tk.Menu(self)
self.config(menu=menu_bar)
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Open...", command=self.open_file,
accelerator=f"{self.modifier}+O")
self._recent_menu = tk.Menu(file_menu, tearoff=0)
file_menu.add_cascade(label="Open Recent", menu=self._recent_menu)
file_menu.add_command(label="Export View...", command=self.export_filtered,
state="disabled")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self._on_close)
menu_bar.add_cascade(label="File", menu=file_menu)
self.file_menu = file_menu
self._rebuild_recent_menu()
self.edit_menu = tk.Menu(menu_bar, tearoff=0)
self.edit_menu.add_command(label="Find/Filter...", command=self.find_data,
accelerator=f"{self.modifier}+F", state="disabled")
self.edit_menu.add_command(label="Clear Filter", command=self.clear_filter,
accelerator="Esc", state="disabled")
self.edit_menu.add_command(label="Go to Row...", command=self.go_to_line,
accelerator=f"{self.modifier}+G")
self.edit_menu.add_separator()
self.edit_menu.add_command(label="Copy Cell", command=self.copy_selection,
accelerator=f"{self.modifier}+C")
menu_bar.add_cascade(label="Edit", menu=self.edit_menu)
help_menu = tk.Menu(menu_bar, tearoff=0)
help_menu.add_command(label="About", command=self.show_about)
menu_bar.add_cascade(label="Help", menu=help_menu)
self.bind(f"<{self.modifier}-o>", lambda e: self.open_file())
self.bind(f"<{self.modifier}-f>", lambda e: self.find_data())
self.bind(f"<{self.modifier}-g>", lambda e: self.go_to_line())
self.bind(f"<{self.modifier}-c>", lambda e: self.copy_selection())
self.bind("<Escape>", lambda e: self.clear_filter())
for key in ("Up", "Down", "Left", "Right", "Prior", "Next", "Home", "End"):
self.bind(f"<{key}>", self._on_key_nav)
self.bind("<Tab>", self._on_key_nav)
self.bind("<Shift-Tab>", self._on_key_nav)
# Zoom
self.bind(f"<{self.modifier}-equal>", lambda e: self._zoom(1))
self.bind(f"<{self.modifier}-minus>", lambda e: self._zoom(-1))
self.bind(f"<{self.modifier}-0>", lambda e: self._zoom(0))
# ================================================================
# Widgets
# ================================================================
def _create_widgets(self):
# Tab bar for SQLite tables (hidden until needed)
self._tab_bar = tk.Frame(self, bg=self.HEADER_BG)
self._tab_buttons = []
main_frame = tk.Frame(self, bg=self.BACKGROUND_COLOR)
main_frame.pack(fill="both", expand=True)
main_frame.grid_rowconfigure(1, weight=1)
main_frame.grid_columnconfigure(1, weight=1)
# Gutter header
self.gutter_header = tk.Canvas(main_frame, height=30, bd=0,
highlightthickness=0, bg=self.HEADER_BG,
width=self._gutter_width)
self.gutter_header.grid(row=0, column=0, sticky="nsew")
# Main header
self.header_canvas = tk.Canvas(main_frame, height=30, bd=0,
highlightthickness=0, bg=self.HEADER_BG)
self.header_canvas.grid(row=0, column=1, sticky="ew")
# Gutter data
self.gutter_canvas = tk.Canvas(main_frame, bg=self.GUTTER_BG,
highlightthickness=0, bd=0,
width=self._gutter_width)
self.gutter_canvas.grid(row=1, column=0, sticky="ns")
# Main data
self.canvas = tk.Canvas(main_frame, bg=self.BACKGROUND_COLOR,
highlightthickness=0, bd=0)
self.canvas.grid(row=1, column=1, sticky="nsew")
# Scrollbars — vertical is managed manually, horizontal is canvas-based
self.vsb = ttk.Scrollbar(main_frame, orient="vertical", command=self.on_vscroll)
self.hsb = ttk.Scrollbar(main_frame, orient="horizontal", command=self.on_hscroll)
self.canvas.configure(xscrollcommand=self.hsb.set)
self.vsb.grid(row=1, column=2, sticky="ns")
self.hsb.grid(row=2, column=1, sticky="ew")
# Status bar
self.status_bar = ttk.Label(self, text="Ready", anchor=tk.W,
padding=(5, 2), relief=tk.SUNKEN)
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
# Bindings
self.canvas.bind("<Configure>", lambda e: self._schedule_redraw())
self.canvas.bind("<Button-1>", self._on_cell_click)
if sys.platform == "darwin":
self.canvas.bind("<Button-2>", self._on_cell_right_click)
self.canvas.bind("<Control-Button-1>", self._on_cell_right_click)
else:
self.canvas.bind("<Button-3>", self._on_cell_right_click)
for cvs in (self.canvas, self.gutter_canvas):
cvs.bind("<MouseWheel>", self._on_mousewheel)
cvs.bind("<Button-4>", self._on_mousewheel)
cvs.bind("<Button-5>", self._on_mousewheel)
self.header_canvas.bind("<Motion>", self._on_header_motion)
self.header_canvas.bind("<ButtonPress-1>", self._on_header_press)
self.header_canvas.bind("<B1-Motion>", self._on_header_drag)
self.header_canvas.bind("<ButtonRelease-1>", self._on_header_release)
if sys.platform == "darwin":
self.header_canvas.bind("<Button-2>", self._on_header_right_click)
self.header_canvas.bind("<Control-Button-1>", self._on_header_right_click)
else:
self.header_canvas.bind("<Button-3>", self._on_header_right_click)
# ================================================================
# Scrolling
# ================================================================
def _schedule_redraw(self):
if self._redraw_job:
self.after_cancel(self._redraw_job)
self._redraw_job = self.after(8, self._perform_redraw)
def _perform_redraw(self):
self._redraw_job = None
self._update_cells()
def _clamp_first_row(self):
# Use fully visible rows (not pool size which includes partial row buffer)
visible = max(1, int(self.canvas.winfo_height() / self.row_height))
max_first = max(0, self.total_rows - visible)
self._first_row = max(0, min(self._first_row, max_first))
def _update_scrollbar(self):
if self.total_rows <= 0:
self.vsb.set(0, 1)
return
top = self._first_row / self.total_rows
bottom = min(1.0, (self._first_row + self._pool_rows) / self.total_rows)
self.vsb.set(top, bottom)
def on_vscroll(self, *args):
if self.total_rows <= 0:
return
action = args[0]
if action == "moveto":
self._first_row = int(float(args[1]) * self.total_rows)
elif action == "scroll":
amount = int(args[1])
if args[2] == "pages":
amount *= max(1, self._pool_rows - 1)
self._first_row += amount
self._clamp_first_row()
self._update_scrollbar()
self._update_cells()
def on_hscroll(self, *args):
self.canvas.xview(*args)
self.header_canvas.xview(*args)
self._redraw_header()
def _on_mousewheel(self, event):
# Ctrl/Cmd + scroll = zoom
ctrl = event.state & 0x4 if sys.platform != "darwin" else event.state & 0x8
if ctrl:
if sys.platform == "darwin":
direction = 1 if event.delta > 0 else -1
elif event.num == 4 or event.delta > 0:
direction = 1
else:
direction = -1
self._zoom(direction)
return
if sys.platform == "darwin":
delta = -int(event.delta)
elif event.num == 5 or event.delta < 0:
delta = 3
elif event.num == 4 or event.delta > 0:
delta = -3
else:
return
self._first_row += delta
self._clamp_first_row()
self._update_scrollbar()
self._update_cells()
# ================================================================
# Data loading
# ================================================================
def open_file(self):
path = filedialog.askopenfilename(
filetypes=[("CSV files", "*.csv"),
("SQLite", "*.sqlite *.db *.sqlite3"),
("All files", "*.*")])
if path:
self.load_file_from_path(path)
def load_file_from_path(self, file_path):
if not os.path.exists(file_path):
messagebox.showerror("Error", f"File not found:\n{file_path}")
return
self._reset_state()
self.file_path = file_path
self._add_recent(file_path)
self.title(f"Table Viewer - {os.path.basename(file_path)}")
self.status_bar.config(text=f"Opening {file_path}...")
ext = os.path.splitext(file_path)[1].lower()
if ext in (".sqlite", ".db", ".sqlite3"):
self._load_thread = threading.Thread(target=self._load_sqlite_data, daemon=True)
else:
self._load_thread = threading.Thread(target=self._load_csv_data, daemon=True)
self._load_thread.start()
def _reset_state(self):
self._sqlite_tables = []
self._sqlite_current_table = None
self._hide_table_tabs()
self.original_data = pd.DataFrame()
self.view_df = pd.DataFrame()
self.column_names = []
self.sort_info = {"col_index": None, "ascending": True}
self.features_ready = False
self.selected_cell = {"row": None, "col": None}
self.last_press_time = 0
self.last_press_col = None
self.edit_menu.entryconfig("Find/Filter...", state="disabled")
self.edit_menu.entryconfig("Clear Filter", state="disabled")
self.file_menu.entryconfig("Export View...", state="disabled")
self._first_row = 0
self._pool_rows = 0
self._pool_cols = 0
self._gpool_rows = 0
self.canvas.delete("all")
self.header_canvas.delete("all")
self.gutter_canvas.delete("all")
self.gutter_header.delete("all")
def _detect_encoding(self):
try:
with open(self.file_path, "r", encoding="utf-8-sig") as f:
f.read(8192)
return "utf-8-sig"
except UnicodeDecodeError:
return "latin-1"
def _load_csv_data(self):
INITIAL_ROWS = 200
CHUNK_SIZE = 50000
try:
encoding = self._detect_encoding()
preview = pd.read_csv(self.file_path, nrows=INITIAL_ROWS,
encoding=encoding, on_bad_lines="skip", engine="c")
col_names = preview.columns.tolist()
self._detect_column_types(preview)
self._estimate_col_widths(preview)
self._update_col_offsets()
def _apply_preview():
self.column_names = col_names
self.original_data = preview
self.view_df = preview
self.total_rows = len(preview)
self._update_gutter_width()
self.setup_display()
self.status_bar.config(text="Loaded preview. Reading full file...")
self.after(0, _apply_preview)
reader = pd.read_csv(self.file_path, chunksize=CHUNK_SIZE, iterator=True,
low_memory=False, encoding=encoding,
on_bad_lines="skip", engine="c", header=0,
skiprows=range(1, INITIAL_ROWS + 1))
chunks = [preview]
for chunk in reader:
chunks.append(chunk)
full_data = pd.concat(chunks, ignore_index=True)
self.after(0, lambda: self._finalize_load(full_data))
except Exception as e:
self.after(0, lambda: messagebox.showerror("Error", f"Failed to read file: {e}"))
def _load_sqlite_data(self):
try:
conn = sqlite3.connect(self.file_path)
tables = [r[0] for r in conn.execute(
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").fetchall()]
conn.close()
if not tables:
self.after(0, lambda: messagebox.showerror("Error", "No tables found in database."))
return
self._sqlite_tables = tables
if len(tables) > 1:
self.after(0, lambda: self._show_table_tabs(tables, tables[0]))
self._load_sqlite_table(tables[0])
except Exception as e:
self.after(0, lambda: messagebox.showerror("Error", f"Failed to read database: {e}"))
def _load_sqlite_table(self, table):
"""Load a table with preview-then-full, all state on main thread."""
self._sqlite_current_table = table
self.after(0, lambda: self.title(
f"Table Viewer - {os.path.basename(self.file_path)} \u2014 {table}"))
self.after(0, lambda: self._update_tab_highlight(table))
try:
conn = sqlite3.connect(self.file_path)
# Stage 1: fast preview
preview = pd.read_sql(f"SELECT * FROM [{table}] LIMIT 200", conn)
col_names = preview.columns.tolist()
self._detect_column_types(preview)
self._estimate_col_widths(preview)
# Widen numeric columns using MAX values (instant on SQLite)
try:
num_cols = [col_names[i] for i, t in self.col_types.items()
if t in ("int", "float")]
if num_cols:
exprs = ", ".join(f"MAX([{c}])" for c in num_cols)
row = conn.execute(f"SELECT {exprs} FROM [{table}]").fetchone()
for c, val in zip(num_cols, row):
if val is None:
continue
i = col_names.index(c)
formatted = f"{int(val):,}" if self.col_types[i] == "int" else f"{val:,.2f}"
w = self.mono_font.measure(formatted) + 15
if w > self.col_widths.get(i, 0):
self.col_widths[i] = min(w, 400)
except Exception:
pass
self._update_col_offsets()
def _apply_preview():
self.column_names = col_names
self.original_data = preview
self.view_df = preview
self.total_rows = len(preview)
self._first_row = 0
self._update_gutter_width()
self.setup_display()
self.status_bar.config(text="Loaded preview. Reading full table...")
self.after(0, _apply_preview)
# Stage 2: full load
full_data = pd.read_sql(f"SELECT * FROM [{table}]", conn)
conn.close()
self.after(0, lambda: self._finalize_load(full_data))
except Exception as e:
self.after(0, lambda: messagebox.showerror("Error", f"Failed to load table: {e}"))
def _show_table_tabs(self, tables, active):
"""Show the tab bar with buttons for each table."""
for btn in self._tab_buttons:
btn.destroy()
self._tab_buttons = []
for t in tables:
btn = tk.Label(self._tab_bar, text=t, padx=12, pady=4, cursor="hand2")
btn.bind("<Button-1>", lambda e, name=t: self._on_tab_click(name))
btn.pack(side="left", padx=(0, 1))
self._tab_buttons.append(btn)
# Pack above main_frame but below menu/status
children = self.pack_slaves()
main_frame = [c for c in children if isinstance(c, tk.Frame) and c != self._tab_bar]
if main_frame:
self._tab_bar.pack(before=main_frame[0], fill="x", side="top")
else:
self._tab_bar.pack(fill="x", side="top")
self._update_tab_highlight(active)
def _hide_table_tabs(self):
self._tab_bar.pack_forget()
for btn in self._tab_buttons:
btn.destroy()
self._tab_buttons = []
def _update_tab_highlight(self, active):
for btn in self._tab_buttons:
if btn.cget("text") == active:
btn.config(bg=self.BACKGROUND_COLOR, fg=self.FOREGROUND_COLOR)
else:
btn.config(bg=self.HEADER_BG, fg=self.HEADER_FG)
def _on_tab_click(self, table):
if table == self._sqlite_current_table:
return
if self._load_thread and self._load_thread.is_alive():
return
self.status_bar.config(text=f"Loading table '{table}'...")
self._load_thread = threading.Thread(
target=self._load_sqlite_table, args=(table,), daemon=True)
self._load_thread.start()
def _finalize_load(self, full_data):
self.original_data = full_data
self.view_df = full_data
self.total_rows = len(full_data)
# Re-estimate widths from full data, only widen (no jarring shrink)
old_widths = dict(self.col_widths)
self._estimate_col_widths(full_data)
widened = False
for i in old_widths:
if i in self.col_widths and self.col_widths[i] < old_widths[i]:
self.col_widths[i] = old_widths[i]
elif i in self.col_widths and self.col_widths[i] > old_widths[i]:
widened = True
if widened:
self._update_col_offsets()
self._update_gutter_width()
self.features_ready = True
self.edit_menu.entryconfig("Find/Filter...", state="normal")
self.edit_menu.entryconfig("Clear Filter", state="normal")
self.file_menu.entryconfig("Export View...", state="normal")
self.setup_display()
self.status_bar.config(text=f"Ready. {self.total_rows:,} rows.")
# ================================================================
# Column types, widths, offsets
# ================================================================
def _detect_column_types(self, df):
self.col_alignments = {}
self.col_types = {}
for i, col_name in enumerate(df.columns):
dtype = df[col_name].dtype
if pd.api.types.is_integer_dtype(dtype):
col_type = "int"
elif pd.api.types.is_float_dtype(dtype):
col_type = "float"
elif pd.api.types.is_datetime64_any_dtype(dtype):
col_type = "datetime"
else:
col_type = "text"
self.col_types[i] = col_type
self.col_alignments[i] = "e" if col_type in ("int", "float") else "w"
def _estimate_col_widths(self, df):
self.col_widths = {}
head = df.iloc[:100]
tail = df.iloc[-100:] if len(df) > 100 else df.iloc[:0]
sample = pd.concat([head, tail]).drop_duplicates()
for i, col_name in enumerate(df.columns):
header_w = self.header_font.measure(col_name) + 30
col_type = self.col_types.get(i, "text")
font = self.mono_font if col_type in ("int", "float", "datetime") else self.default_font
col_data = sample[col_name]
if col_data.empty:
max_data_w = 0
else:
strs = col_data.astype(str)
longest = strs.loc[strs.str.len().idxmax()]
if col_type == "int":
try:
longest = f"{int(float(longest)):,}"
except (ValueError, TypeError):
pass
elif col_type == "float":
try:
longest = f"{float(longest):,.2f}"
except (ValueError, TypeError):
pass
max_data_w = font.measure(longest) + 15
self.col_widths[i] = min(max(header_w, max_data_w, 50), 400)
def _update_col_offsets(self):
offsets = [0]
for i in range(len(self.col_widths)):
offsets.append(offsets[-1] + self.col_widths.get(i, 100))
self._col_offsets = offsets
def _update_gutter_width(self):
max_str = f"{self.total_rows:,}" if self.total_rows > 0 else "999"
self._gutter_width = max(50, self.mono_font.measure(max_str) + 20)
self.gutter_header.config(width=self._gutter_width)
self.gutter_canvas.config(width=self._gutter_width)
# ================================================================
# Formatting
# ================================================================
def _format_cell(self, col_idx, val):
if pd.isna(val):
return ""
col_type = self.col_types.get(col_idx, "text")
if col_type == "int":
try:
return f"{int(val):,}"
except (ValueError, TypeError):
return str(val)
elif col_type == "float":
try:
return f"{float(val):,.2f}"
except (ValueError, TypeError):
return str(val)
return str(val)
def _get_font(self, col_idx):
col_type = self.col_types.get(col_idx, "text")
return self.mono_font if col_type in ("int", "float", "datetime") else self.default_font
def _clip_text(self, text, col_idx, col_w):
max_w = col_w - 10
if max_w <= 0:
return ""
font = self._get_font(col_idx)
w = font.measure(text)
if w <= max_w:
return text
# Estimate truncation point proportionally
n = max(0, int(len(text) * max_w / w) - 1)
return text[:n] + "\u2026" if n > 0 else ""
# ================================================================
# Display
# ================================================================
def setup_display(self):
total_w = self._col_offsets[-1] if self._col_offsets else 0
canvas_h = max(self.canvas.winfo_height(), 100)
# Horizontal scrolling is canvas-based; vertical is manual
self.canvas.configure(scrollregion=(0, 0, total_w, canvas_h))
self.header_canvas.configure(scrollregion=(0, 0, total_w, 30))
# Force pool rebuild
self._pool_rows = 0
self._pool_cols = 0
self._gpool_rows = 0
self._clamp_first_row()
self._update_scrollbar()
self._redraw_header()
self._update_cells()
self.deiconify()
self.lift()
# ================================================================
# Pool management
# ================================================================
def _rebuild_pool(self, n_rows, n_cols):
"""Create items at fixed screen positions. Called on resize or data change."""
self.canvas.delete("all")
self._pool_rows = n_rows
self._pool_cols = n_cols
total_w = self._col_offsets[-1] if len(self._col_offsets) > 1 else 0
# Stripes at fixed y — use large width so they always fill the canvas
stripe_w = max(total_w, self.canvas.winfo_width(), 4000)
self._pool_stripes = []
for slot in range(n_rows):
y = slot * self.row_height
self._pool_stripes.append(
self.canvas.create_rectangle(0, y, stripe_w, y + self.row_height,
fill="", outline="", state="hidden"))
# Selection rect
self._pool_sel = self.canvas.create_rectangle(
0, 0, 0, 0, fill=self.SELECTION_COLOR, outline="", state="hidden")
# Grid lines and text at fixed positions
self._pool_vlines = []
self._pool_hlines = []
self._pool_texts = []
for slot in range(n_rows):
y = slot * self.row_height
row_vl = []
row_txt = []
for col in range(n_cols):
col_x = self._col_offsets[col]
col_w = self.col_widths.get(col, 100)
col_right = col_x + col_w
row_vl.append(self.canvas.create_line(
col_right, y, col_right, y + self.row_height,
fill=self.GRID_LINE_COLOR, state="hidden"))
align = self.col_alignments.get(col, "w")
if align == "e":
tx, anchor = col_x + col_w - 5, "e"
else:
tx, anchor = col_x + 5, "w"
row_txt.append(self.canvas.create_text(
tx, y + self.row_height / 2, text="", anchor=anchor,
font=self._get_font(col), fill=self.FOREGROUND_COLOR, state="hidden"))
self._pool_vlines.append(row_vl)
self._pool_texts.append(row_txt)
self._pool_hlines.append(self.canvas.create_line(
0, y + self.row_height, total_w, y + self.row_height,
fill=self.GRID_LINE_COLOR, state="hidden"))
def _rebuild_gutter_pool(self, n_rows):
"""Create gutter items at fixed screen positions."""
self.gutter_canvas.delete("all")
self._gpool_rows = n_rows
gw = self._gutter_width
self._gpool_stripes = []
self._gpool_texts = []
self._gpool_hlines = []
for slot in range(n_rows):
y = slot * self.row_height
self._gpool_stripes.append(
self.gutter_canvas.create_rectangle(0, y, gw, y + self.row_height,
fill="", outline="", state="hidden"))
self._gpool_texts.append(
self.gutter_canvas.create_text(gw - 8, y + self.row_height / 2,
text="", anchor="e", font=self.mono_font,
fill=self.FOREGROUND_COLOR, state="hidden"))
self._gpool_hlines.append(
self.gutter_canvas.create_line(0, y + self.row_height, gw, y + self.row_height,
fill=self.GRID_LINE_COLOR, state="hidden"))
# ================================================================
# Rendering — only text/fill changes, no coords
# ================================================================
def _update_cells(self):
"""Update cell content for current scroll position. No coords calls."""
if not self.column_names or self.total_rows == 0:
return
canvas_h = self.canvas.winfo_height()
if canvas_h <= 1:
return
n_cols = len(self.column_names)
n_rows = int(canvas_h / self.row_height) + 2
if n_rows > self._pool_rows or n_cols != self._pool_cols:
self._rebuild_pool(n_rows, n_cols)
if n_rows > self._gpool_rows:
self._rebuild_gutter_pool(n_rows)
# Batch data access
end_row = min(self._first_row + self._pool_rows, len(self.view_df))
visible_count = max(0, end_row - self._first_row)
if visible_count > 0:
batch = self.view_df.iloc[self._first_row:end_row]
batch_vals = batch.values
batch_index = batch.index
else:
batch_vals = []
batch_index = []
sel_row = self.selected_cell["row"]
sel_col = self.selected_cell["col"]
sel_slot = None
# --- Main canvas ---
c = self.canvas
for slot in range(self._pool_rows):
if slot >= visible_count:
c.itemconfig(self._pool_stripes[slot], state="hidden")
c.itemconfig(self._pool_hlines[slot], state="hidden")
for pc in range(self._pool_cols):
c.itemconfig(self._pool_texts[slot][pc], state="hidden")
c.itemconfig(self._pool_vlines[slot][pc], state="hidden")
continue
data_row = self._first_row + slot
fill = self.STRIPE_COLOR if data_row % 2 == 0 else ""
c.itemconfig(self._pool_stripes[slot], fill=fill, state="normal")
c.itemconfig(self._pool_hlines[slot], state="normal")
row_vals = batch_vals[slot]
for pc in range(self._pool_cols):
col_w = self.col_widths.get(pc, 100)
display = self._clip_text(self._format_cell(pc, row_vals[pc]), pc, col_w)
c.itemconfig(self._pool_texts[slot][pc], text=display, state="normal")
c.itemconfig(self._pool_vlines[slot][pc], state="normal")
if data_row == sel_row:
sel_slot = slot
# Selection
if sel_slot is not None and sel_col is not None:
y = sel_slot * self.row_height
sx = self._col_offsets[sel_col]
sw = self.col_widths.get(sel_col, 100)
c.coords(self._pool_sel, sx, y, sx + sw, y + self.row_height)
c.itemconfig(self._pool_sel, state="normal")
c.tag_raise(self._pool_sel)
for pc in range(self._pool_cols):
c.tag_raise(self._pool_texts[sel_slot][pc])
else:
c.itemconfig(self._pool_sel, state="hidden")
# --- Gutter ---
gc = self.gutter_canvas
for slot in range(self._gpool_rows):
if slot >= visible_count:
gc.itemconfig(self._gpool_stripes[slot], state="hidden")
gc.itemconfig(self._gpool_texts[slot], state="hidden")
gc.itemconfig(self._gpool_hlines[slot], state="hidden")
continue
data_row = self._first_row + slot
fill = self.STRIPE_COLOR if data_row % 2 == 0 else ""
gc.itemconfig(self._gpool_stripes[slot], fill=fill, state="normal")
gc.itemconfig(self._gpool_hlines[slot], state="normal")
row_num = batch_index[slot] + 1
gc.itemconfig(self._gpool_texts[slot], text=f"{row_num:,}", state="normal")
def _redraw_header(self):
self.header_canvas.delete("all")
if not self.column_names:
return
h = self.header_canvas.winfo_height()
x_left = self.header_canvas.xview()[0]
total_w = self._col_offsets[-1]
x_off = -int(x_left * total_w)
canvas_w = self.header_canvas.winfo_width()
for i in range(len(self.column_names)):
col_x = self._col_offsets[i]
col_w = self.col_widths.get(i, 100)
draw_x = col_x + x_off
if draw_x + col_w < 0 or draw_x > canvas_w:
continue
text = self.column_names[i]
if self.sort_info["col_index"] == i:
text += " \u25b2" if self.sort_info["ascending"] else " \u25bc"
align = self.col_alignments.get(i, "w")
tx = col_x + (col_w - 10 if align == "e" else 10)
anchor = "e" if align == "e" else "w"
self.header_canvas.create_text(tx, h / 2, text=text, anchor=anchor,
font=self.header_font, fill=self.HEADER_FG)
line_x = col_x + col_w
self.header_canvas.create_line(line_x, 4, line_x, h - 4, fill=self.GRID_LINE_COLOR)
# Gutter header
self.gutter_header.delete("all")
self.gutter_header.create_text(self._gutter_width - 8, h / 2, text="#",
anchor="e", font=self.header_font, fill=self.HEADER_FG)
self.gutter_header.create_line(self._gutter_width - 1, 0,
self._gutter_width - 1, h, fill=self.GRID_LINE_COLOR)
# ================================================================
# Sorting
# ================================================================
def sort_by_column(self, col_index):
if not self.features_ready or (self.sort_thread and self.sort_thread.is_alive()):
return
if self.sort_info["col_index"] == col_index: