|
1 | 1 | import dash |
2 | | -from dash import Input, Output |
| 2 | +from dash import Input, Output, State |
3 | 3 |
|
4 | 4 |
|
5 | 5 | def get_callbacks(app: dash.Dash) -> None: |
@@ -87,3 +87,111 @@ def update_event_select_options( |
87 | 87 | return options, value |
88 | 88 | else: |
89 | 89 | return dash.no_update, dash.no_update |
| 90 | + |
| 91 | + @app.callback( |
| 92 | + Output("tag-event-button", "disabled"), |
| 93 | + Input("event-select", "value"), |
| 94 | + ) |
| 95 | + def disable_tag_event_button( |
| 96 | + event_tag: str, |
| 97 | + ) -> bool: |
| 98 | + """Disable the tag event button if the event tag is empty |
| 99 | +
|
| 100 | + Parameters |
| 101 | + ---------- |
| 102 | + event_tag : str |
| 103 | + Currently selected event tag |
| 104 | +
|
| 105 | + Returns |
| 106 | + ------- |
| 107 | + disabled : bool |
| 108 | + True if the tag event button should be disabled, False otherwise |
| 109 | + """ |
| 110 | + if event_tag == "" or event_tag is None: |
| 111 | + return True |
| 112 | + else: |
| 113 | + return False |
| 114 | + |
| 115 | + @app.callback( |
| 116 | + Output("events-storage", "data"), |
| 117 | + Input("tag-event-button", "n_clicks"), |
| 118 | + [ |
| 119 | + State("frame-index-input", "value"), |
| 120 | + State("events-video-select", "value"), |
| 121 | + State("event-select", "value"), |
| 122 | + State("events-storage", "data"), |
| 123 | + ], |
| 124 | + ) |
| 125 | + def update_events_storage( |
| 126 | + n_clicks: int, |
| 127 | + frame_index: int, |
| 128 | + video_name: str, |
| 129 | + event_tag: str, |
| 130 | + events_storage: dict, |
| 131 | + ) -> dict: |
| 132 | + """Update the events storage with the currently selected event tag |
| 133 | + and frame index, when the tag event button is clicked. |
| 134 | +
|
| 135 | + Parameters |
| 136 | + ---------- |
| 137 | + n_clicks : int |
| 138 | + Number of times the tag event button has been clicked |
| 139 | + frame_index : int |
| 140 | + Currently selected frame index |
| 141 | + video_name : str |
| 142 | + Currently selected video name |
| 143 | + event_tag : str |
| 144 | + Currently selected event tag |
| 145 | + events_storage : dict |
| 146 | + Dictionary storing event tags for each video. |
| 147 | +
|
| 148 | + Returns |
| 149 | + ------- |
| 150 | + events_storage : dict |
| 151 | + data held in temporary memory storage, |
| 152 | + accessible to all tabs in the app |
| 153 | + """ |
| 154 | + if n_clicks > 0: |
| 155 | + if video_name not in events_storage.keys(): |
| 156 | + events_storage[video_name] = dict() |
| 157 | + events_storage[video_name][event_tag] = frame_index |
| 158 | + return events_storage |
| 159 | + |
| 160 | + @app.callback( |
| 161 | + Output("events-table", "data"), |
| 162 | + [ |
| 163 | + Input("events-video-select", "value"), |
| 164 | + Input("events-storage", "data"), |
| 165 | + ], |
| 166 | + ) |
| 167 | + def update_events_table( |
| 168 | + video_name: str, |
| 169 | + events_storage: dict, |
| 170 | + ) -> list[dict]: |
| 171 | + """Update the events table based on the stored events. |
| 172 | +
|
| 173 | + Parameters |
| 174 | + ---------- |
| 175 | + video_name : str |
| 176 | + Currently selected video name |
| 177 | + events_storage : dict |
| 178 | + Dictionary storing event tags for each video. |
| 179 | +
|
| 180 | + Returns |
| 181 | + ------- |
| 182 | + data : list[dict] |
| 183 | + list of dictionaries with the following keys and values: |
| 184 | + - 'tag': event tag |
| 185 | + - 'frame index': frame index |
| 186 | + - 'seconds': frame index converted to seconds, based on the |
| 187 | + video FPS |
| 188 | + """ |
| 189 | + fps = 40 |
| 190 | + rows = [] |
| 191 | + if video_name in events_storage.keys(): |
| 192 | + for event, frame_idx in events_storage[video_name].items(): |
| 193 | + sec = frame_idx / fps |
| 194 | + rows.append( |
| 195 | + {"tag": event, "frame index": frame_idx, "seconds": sec} |
| 196 | + ) |
| 197 | + return rows |
0 commit comments