viklofg commited on
Commit
0f4c765
·
1 Parent(s): 97c2382

Enable multi-page output view

Browse files
Files changed (1) hide show
  1. app/tabs/visualizer.py +63 -6
app/tabs/visualizer.py CHANGED
@@ -7,12 +7,37 @@ _IMAGE_TEMPLATE = _ENV.get_template("image")
7
  _TRANSCRIPTION_TEMPLATE = _ENV.get_template("transcription")
8
 
9
 
10
- def render_image(state):
11
- return _IMAGE_TEMPLATE.render(page=state[0], lines=state[0].traverse(lambda node: node.is_line()))
12
 
13
 
14
- def render_transcription(state):
15
- return _TRANSCRIPTION_TEMPLATE.render(lines=state[0].traverse(lambda node: node.is_line()))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
 
18
  with gr.Blocks() as visualizer:
@@ -32,6 +57,38 @@ with gr.Blocks() as visualizer:
32
  gr.Markdown("## Annotated image")
33
  image = gr.HTML(padding=False, elem_classes="svg-image", container=True)
34
 
 
 
 
 
 
35
  collection_viz_state = gr.State()
36
- collection_viz_state.change(render_image, inputs=collection_viz_state, outputs=image)
37
- collection_viz_state.change(render_transcription, inputs=collection_viz_state, outputs=transcription)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  _TRANSCRIPTION_TEMPLATE = _ENV.get_template("transcription")
8
 
9
 
10
+ def render_image(collection, current_page_idx):
11
+ return _IMAGE_TEMPLATE.render(page=collection[current_page_idx], lines=collection[current_page_idx].traverse(lambda node: node.is_line()))
12
 
13
 
14
+ def render_transcription(collection, current_page_idx):
15
+ return _TRANSCRIPTION_TEMPLATE.render(lines=collection[current_page_idx].traverse(lambda node: node.is_line()))
16
+
17
+
18
+ def toggle_navigation_button(collection):
19
+ visible = len(collection.pages) > 1
20
+ return gr.update(visible=visible)
21
+
22
+
23
+ def activate_left_button(current_page_idx):
24
+ interactive = current_page_idx > 0
25
+ return gr.update(interactive=interactive)
26
+
27
+
28
+ def activate_right_button(collection, current_page_idx):
29
+ interactive = current_page_idx + 1 < len(collection.pages)
30
+ return gr.update(interactive=interactive)
31
+
32
+
33
+ def right_button_click(collection, current_page_index):
34
+ max_index = len(collection.pages) - 1
35
+ return min(max_index, current_page_index + 1)
36
+
37
+
38
+ def update_image_caption(collection, current_page_idx):
39
+ n_pages = len(collection.pages)
40
+ return f"Image {current_page_idx + 1} of {n_pages}: `{collection[current_page_idx].label}`"
41
 
42
 
43
  with gr.Blocks() as visualizer:
 
57
  gr.Markdown("## Annotated image")
58
  image = gr.HTML(padding=False, elem_classes="svg-image", container=True)
59
 
60
+ image_caption = gr.Markdown()
61
+ with gr.Row():
62
+ left = gr.Button("← Previous", visible=False, interactive=False)
63
+ right = gr.Button("Next →", visible=False)
64
+
65
  collection_viz_state = gr.State()
66
+
67
+ current_page_idx = gr.State(0)
68
+
69
+ # Update `current_page_idx` on button click
70
+ left.click(lambda current_page_idx: max(0, current_page_idx-1), current_page_idx, current_page_idx)
71
+ right.click(right_button_click, [collection_viz_state, current_page_idx], current_page_idx)
72
+
73
+ # Update the view when...
74
+ # ...the collection changes, or...
75
+ collection_viz_state.change(render_image, inputs=[collection_viz_state, current_page_idx], outputs=image)
76
+ collection_viz_state.change(render_transcription, inputs=[collection_viz_state, current_page_idx], outputs=transcription)
77
+ # ...`current_page_idx` changes
78
+ current_page_idx.change(render_image, inputs=[collection_viz_state, current_page_idx], outputs=image)
79
+ current_page_idx.change(render_transcription, inputs=[collection_viz_state, current_page_idx], outputs=transcription)
80
+
81
+ # Toggle interactivity of navigation buttons when `current_page_idx` changes
82
+ current_page_idx.change(activate_left_button, current_page_idx, left)
83
+ current_page_idx.change(activate_right_button, [collection_viz_state, current_page_idx], right)
84
+
85
+ # Reset `current_page_idx` when the collection is updated
86
+ collection_viz_state.change(lambda _: 0, current_page_idx, current_page_idx)
87
+
88
+ # Toggle visibility of navigation buttons (they're hidden if there's only one page) when the collection is updated
89
+ collection_viz_state.change(toggle_navigation_button, collection_viz_state, left)
90
+ collection_viz_state.change(toggle_navigation_button, collection_viz_state, right)
91
+
92
+ # Update the image caption when the collection or current index changes
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)