-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.py
More file actions
131 lines (101 loc) · 3.95 KB
/
viewer.py
File metadata and controls
131 lines (101 loc) · 3.95 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
#!/usr/bin/env python3
import gi
import os
import glob
import sys
import re
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GdkPixbuf
import client
from config import *
class NotebookOrchestrator:
def __init__(self, notebook_id, notebook):
self.notebook_id = notebook_id
self.notebook = notebook
self.images = self.load_sorted_images()
if not self.images:
print("No images found!")
sys.exit(1)
self.num_to_display = NUMBER_OF_MONITORS * IMAGES_PER_MONITOR
self.screen = Gdk.Screen.get_default()
self.windows = []
self.offset = 0
def numerical_sort_key(self, path):
match = re.search(r'img_(\d+)\.png', os.path.basename(path))
if match:
return int(match.group(1))
else:
return -1
def load_sorted_images(self):
images = sorted(
glob.glob(os.path.join(NOTEBOOK_DIR, self.notebook_id, "img_*.png")),
key=self.numerical_sort_key,
)
return images
def spawn_windows(self):
for monitor in range(NUMBER_OF_MONITORS):
win = NotebookWindow(monitor, self.on_key_press)
win.connect("destroy", Gtk.main_quit)
self.windows.append(win)
def assign_images(self):
start = max(0, len(self.images) - self.num_to_display + self.offset)
end = start + self.num_to_display
to_display = self.images[start:end]
# Split images into chunks per monitor
chunks = [to_display[i:i+IMAGES_PER_MONITOR] for i in range(0, len(self.images), IMAGES_PER_MONITOR)]
for window, chunk in zip(self.windows, chunks):
window.set_images(chunk)
def on_key_press(self, widget, event):
key = Gdk.keyval_name(event.keyval)
if key == "Left":
self.offset -= 1
self.assign_images()
elif key == "Right":
self.offset += 1
self.assign_images()
elif key == "r":
client.download_notebook(self.notebook, self.notebook_id, 0, -1)
self.assign_images()
class NotebookWindow(Gtk.Window):
def __init__(self, monitor_number, on_keypress_callback):
super().__init__()
# Setup
self.set_title(f"Notebook Monitor {monitor_number}")
# Layout
self.hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
self.add(self.hbox)
self.connect("key-press-event", on_keypress_callback)
def set_images(self, images):
for child in self.hbox.get_children():
self.hbox.remove(child)
for img_path in images:
if not os.path.exists(img_path):
continue
pixbuf = GdkPixbuf.Pixbuf.new_from_file(img_path)
# Calculate scaling: divide monitor width across images
target_width = MONITOR_WIDTH // IMAGES_PER_MONITOR
# Maintain aspect ratio
aspect = pixbuf.get_width() / pixbuf.get_height()
scaled_height = int(target_width / aspect)
# Make sure it doesn't exceed monitor height
if scaled_height > MONITOR_HEIGHT:
scaled_height = MONITOR_HEIGHT
target_width = int(MONITOR_HEIGHT * aspect)
scaled = pixbuf.scale_simple(
target_width, scaled_height, GdkPixbuf.InterpType.BILINEAR
)
image = Gtk.Image.new_from_pixbuf(scaled)
self.hbox.pack_start(image, True, True, 0)
self.show_all()
def main():
notebooks = client.list_notebooks()
selection = input(">")
notebook_id = notebooks[int(selection)]["id"]
notebook = client.get_render_token(notebook_id)
client.download_notebook(notebook, notebook_id, 0, -1)
notebook_orchestrator = NotebookOrchestrator(notebook_id, notebook)
notebook_orchestrator.spawn_windows()
notebook_orchestrator.assign_images()
Gtk.main()
if __name__ == "__main__":
main()