Spaces:
Running
on
Zero
Running
on
Zero
viklofg
commited on
Commit
·
dae80b9
1
Parent(s):
be3c9c0
Update comments and variable names for clarity
Browse files- app/main.py +1 -1
- app/tabs/visualizer.py +42 -40
app/main.py
CHANGED
@@ -7,7 +7,7 @@ from app.tabs.submit import (
|
|
7 |
custom_template_yaml,
|
8 |
collection_submit_state,
|
9 |
)
|
10 |
-
from app.tabs.visualizer import visualizer, collection_viz_state
|
11 |
|
12 |
from app.tabs.templating import (
|
13 |
templating_block,
|
|
|
7 |
custom_template_yaml,
|
8 |
collection_submit_state,
|
9 |
)
|
10 |
+
from app.tabs.visualizer import visualizer, collection as collection_viz_state
|
11 |
|
12 |
from app.tabs.templating import (
|
13 |
templating_block,
|
app/tabs/visualizer.py
CHANGED
@@ -7,12 +7,12 @@ _IMAGE_TEMPLATE = _ENV.get_template("image")
|
|
7 |
_TRANSCRIPTION_TEMPLATE = _ENV.get_template("transcription")
|
8 |
|
9 |
|
10 |
-
def render_image(collection,
|
11 |
-
return _IMAGE_TEMPLATE.render(page=collection[
|
12 |
|
13 |
|
14 |
-
def render_transcription(collection,
|
15 |
-
return _TRANSCRIPTION_TEMPLATE.render(lines=collection[
|
16 |
|
17 |
|
18 |
def toggle_navigation_button(collection):
|
@@ -20,13 +20,13 @@ def toggle_navigation_button(collection):
|
|
20 |
return gr.update(visible=visible)
|
21 |
|
22 |
|
23 |
-
def activate_left_button(
|
24 |
-
interactive =
|
25 |
return gr.update(interactive=interactive)
|
26 |
|
27 |
|
28 |
-
def activate_right_button(collection,
|
29 |
-
interactive =
|
30 |
return gr.update(interactive=interactive)
|
31 |
|
32 |
|
@@ -35,9 +35,13 @@ def right_button_click(collection, current_page_index):
|
|
35 |
return min(max_index, current_page_index + 1)
|
36 |
|
37 |
|
38 |
-
def
|
|
|
|
|
|
|
|
|
39 |
n_pages = len(collection.pages)
|
40 |
-
return f"Image {
|
41 |
|
42 |
|
43 |
with gr.Blocks() as visualizer:
|
@@ -62,33 +66,31 @@ with gr.Blocks() as visualizer:
|
|
62 |
left = gr.Button("← Previous", visible=False, interactive=False)
|
63 |
right = gr.Button("Next →", visible=False)
|
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 |
-
current_page_idx.change(update_image_caption, inputs=[collection_viz_state, current_page_idx], outputs=image_caption)
|
94 |
-
collection_viz_state.change(update_image_caption, inputs=[collection_viz_state, current_page_idx], outputs=image_caption)
|
|
|
7 |
_TRANSCRIPTION_TEMPLATE = _ENV.get_template("transcription")
|
8 |
|
9 |
|
10 |
+
def render_image(collection, current_page_index):
|
11 |
+
return _IMAGE_TEMPLATE.render(page=collection[current_page_index], lines=collection[current_page_index].traverse(lambda node: node.is_line()))
|
12 |
|
13 |
|
14 |
+
def render_transcription(collection, current_page_index):
|
15 |
+
return _TRANSCRIPTION_TEMPLATE.render(lines=collection[current_page_index].traverse(lambda node: node.is_line()))
|
16 |
|
17 |
|
18 |
def toggle_navigation_button(collection):
|
|
|
20 |
return gr.update(visible=visible)
|
21 |
|
22 |
|
23 |
+
def activate_left_button(current_page_index):
|
24 |
+
interactive = current_page_index > 0
|
25 |
return gr.update(interactive=interactive)
|
26 |
|
27 |
|
28 |
+
def activate_right_button(collection, current_page_index):
|
29 |
+
interactive = current_page_index + 1 < len(collection.pages)
|
30 |
return gr.update(interactive=interactive)
|
31 |
|
32 |
|
|
|
35 |
return min(max_index, current_page_index + 1)
|
36 |
|
37 |
|
38 |
+
def left_button_click(current_page_index):
|
39 |
+
return max(0, current_page_index - 1)
|
40 |
+
|
41 |
+
|
42 |
+
def update_image_caption(collection, current_page_index):
|
43 |
n_pages = len(collection.pages)
|
44 |
+
return f"Image {current_page_index + 1} of {n_pages}: `{collection[current_page_index].label}`"
|
45 |
|
46 |
|
47 |
with gr.Blocks() as visualizer:
|
|
|
66 |
left = gr.Button("← Previous", visible=False, interactive=False)
|
67 |
right = gr.Button("Next →", visible=False)
|
68 |
|
69 |
+
collection = gr.State()
|
70 |
+
current_page_index = gr.State(0)
|
71 |
+
|
72 |
+
# Wiring of navigation buttons
|
73 |
+
left.click(left_button_click, current_page_index, current_page_index)
|
74 |
+
right.click(right_button_click, [collection, current_page_index], current_page_index)
|
75 |
+
|
76 |
+
# Updates on collection change:
|
77 |
+
# - update the view
|
78 |
+
# - reset the page index (always start on page 0)
|
79 |
+
# - toggle visibility of navigation buttons (don't show them for single pages)
|
80 |
+
# - update the image caption
|
81 |
+
collection.change(render_image, inputs=[collection, current_page_index], outputs=image)
|
82 |
+
collection.change(render_transcription, inputs=[collection, current_page_index], outputs=transcription)
|
83 |
+
collection.change(lambda _: 0, current_page_index, current_page_index)
|
84 |
+
collection.change(toggle_navigation_button, collection, left)
|
85 |
+
collection.change(toggle_navigation_button, collection, right)
|
86 |
+
collection.change(update_image_caption, inputs=[collection, current_page_index], outputs=image_caption)
|
87 |
+
|
88 |
+
# Updates on page change:
|
89 |
+
# - update the view
|
90 |
+
# - activate/deactivate buttons
|
91 |
+
# - update the image caption
|
92 |
+
current_page_index.change(render_image, inputs=[collection, current_page_index], outputs=image)
|
93 |
+
current_page_index.change(render_transcription, inputs=[collection, current_page_index], outputs=transcription)
|
94 |
+
current_page_index.change(activate_left_button, current_page_index, left)
|
95 |
+
current_page_index.change(activate_right_button, [collection, current_page_index], right)
|
96 |
+
current_page_index.change(update_image_caption, inputs=[collection, current_page_index], outputs=image_caption)
|
|
|
|