viklofg commited on
Commit
dae80b9
·
1 Parent(s): be3c9c0

Update comments and variable names for clarity

Browse files
Files changed (2) hide show
  1. app/main.py +1 -1
  2. 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, 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):
@@ -20,13 +20,13 @@ def toggle_navigation_button(collection):
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
 
@@ -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 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:
@@ -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
- 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)
 
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)