pre-commit renamed files to PEP standard and removed unused imports
pipeline / Test (push) Failing after 51s
pipeline / Test (push) Failing after 51s
This commit is contained in:
+46
-42
@@ -1,29 +1,33 @@
|
||||
from dash import Dash, Input, Output, State, ALL
|
||||
import dash_bootstrap_components as dbc
|
||||
from dash_auth import BasicAuth
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import List
|
||||
from pydantic import ValidationError
|
||||
import os
|
||||
|
||||
import dash_bootstrap_components as dbc
|
||||
from dash import ALL
|
||||
from dash import Dash
|
||||
from dash import Input
|
||||
from dash import Output
|
||||
from dash import State
|
||||
from dash_auth import BasicAuth
|
||||
from database import connect
|
||||
from database import get_visual_communication
|
||||
from database import ModelOutputs
|
||||
from database import NoDocumentFoundException
|
||||
from database import upsert_annotations
|
||||
from pydantic import ValidationError
|
||||
|
||||
from .layout import app_layout
|
||||
from src.database import (
|
||||
connect,
|
||||
get_visual_communication,
|
||||
NoDocumentFoundException,
|
||||
upsert_annotations,
|
||||
ModelOutputs
|
||||
)
|
||||
|
||||
# setup app
|
||||
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
||||
app.title = "visual critical discourse analysis".title()
|
||||
app.title = 'visual critical discourse analysis'.title()
|
||||
app.layout = app_layout
|
||||
server = app.server
|
||||
|
||||
# setup authentication
|
||||
AUTH_DICT = {
|
||||
os.getenv("DASH_AUTH_USERNAME"): os.getenv("DASH_AUTH_PASSWORD")
|
||||
os.getenv('DASH_AUTH_USERNAME'): os.getenv('DASH_AUTH_PASSWORD'),
|
||||
}
|
||||
BasicAuth(app, AUTH_DICT)
|
||||
|
||||
@@ -33,58 +37,58 @@ collection, db, client = connect()
|
||||
|
||||
# define callbacks
|
||||
@app.callback(
|
||||
Output("alert-element", "is_open"),
|
||||
Output("alert-element", "children"),
|
||||
Input("alert-message", "data")
|
||||
Output('alert-element', 'is_open'),
|
||||
Output('alert-element', 'children'),
|
||||
Input('alert-message', 'data'),
|
||||
)
|
||||
def show_alert(
|
||||
msg: str | None
|
||||
msg: str | None,
|
||||
):
|
||||
if msg is None or msg == "":
|
||||
return False, ""
|
||||
if msg is None or msg == '':
|
||||
return False, ''
|
||||
logging.info(f"updated alert message: {msg}")
|
||||
return True, msg
|
||||
|
||||
|
||||
@app.callback(
|
||||
Output("alert-message", "data"),
|
||||
Output("vis-com-name", "data"),
|
||||
Output("image-container", "src"),
|
||||
Output({"type": "annotation", "index": ALL}, "value"),
|
||||
Input("next-button", "n_clicks"),
|
||||
State("vis-com-name", "data"),
|
||||
State("image-container", "src"),
|
||||
State({"type": "annotation", "index": ALL}, "id"),
|
||||
State({"type": "annotation", "index": ALL}, "value"),
|
||||
Output('alert-message', 'data'),
|
||||
Output('vis-com-name', 'data'),
|
||||
Output('image-container', 'src'),
|
||||
Output({'type': 'annotation', 'index': ALL}, 'value'),
|
||||
Input('next-button', 'n_clicks'),
|
||||
State('vis-com-name', 'data'),
|
||||
State('image-container', 'src'),
|
||||
State({'type': 'annotation', 'index': ALL}, 'id'),
|
||||
State({'type': 'annotation', 'index': ALL}, 'value'),
|
||||
prevent_initial_call=True,
|
||||
)
|
||||
def cycle_visual_communication_data(
|
||||
n_clicks: int,
|
||||
vis_com_name: str,
|
||||
image_src: str,
|
||||
annotation_keys: List,
|
||||
annotation_values: List,
|
||||
annotation_keys: list,
|
||||
annotation_values: list,
|
||||
):
|
||||
logging.info("began cycling visual communication data")
|
||||
logging.info('began cycling visual communication data')
|
||||
global collection
|
||||
# prepare default response
|
||||
response = [
|
||||
"",
|
||||
'',
|
||||
vis_com_name,
|
||||
image_src,
|
||||
annotation_values
|
||||
annotation_values,
|
||||
]
|
||||
# check if next-button clicked
|
||||
if n_clicks == 0:
|
||||
logging.info("stopping early: next-button has not yet been clicked")
|
||||
logging.info('stopping early: next-button has not yet been clicked')
|
||||
return response
|
||||
# check if visual communication name is set
|
||||
if len(vis_com_name) > 0:
|
||||
logging.info("saving annotations to database: %s", vis_com_name)
|
||||
logging.info('saving annotations to database: %s', vis_com_name)
|
||||
try:
|
||||
# extract option keys
|
||||
annotation_keys = [
|
||||
elem["index"]
|
||||
elem['index']
|
||||
for elem in annotation_keys
|
||||
]
|
||||
# ensure all options are set
|
||||
@@ -114,7 +118,7 @@ def cycle_visual_communication_data(
|
||||
upsert_annotations(
|
||||
collection=collection,
|
||||
vis_com_name=vis_com_name,
|
||||
annotations=annotations
|
||||
annotations=annotations,
|
||||
)
|
||||
except (ValueError, ValidationError) as exc:
|
||||
msg = f"failed saving annotation: {exc}"
|
||||
@@ -122,12 +126,12 @@ def cycle_visual_communication_data(
|
||||
response[0] = msg
|
||||
return tuple(response)
|
||||
# get new visual communication
|
||||
logging.info("trying to get new visual communication")
|
||||
logging.info('trying to get new visual communication')
|
||||
try:
|
||||
# get data
|
||||
vis_com = get_visual_communication(
|
||||
collection=collection,
|
||||
with_annotation=False
|
||||
with_annotation=False,
|
||||
)
|
||||
# set variables
|
||||
vis_com_name = vis_com.name
|
||||
@@ -139,7 +143,7 @@ def cycle_visual_communication_data(
|
||||
# reset annotations
|
||||
annotation_values = [None for elem in annotation_values]
|
||||
except NoDocumentFoundException:
|
||||
msg = "no unannotated data in database"
|
||||
msg = 'no unannotated data in database'
|
||||
logging.warning(msg)
|
||||
response[0] = msg
|
||||
return tuple(response)
|
||||
@@ -147,5 +151,5 @@ def cycle_visual_communication_data(
|
||||
response[1] = vis_com_name
|
||||
response[2] = image_src
|
||||
response[3] = annotation_values
|
||||
logging.info("finished getting visual communication: %s", vis_com_name)
|
||||
logging.info('finished getting visual communication: %s', vis_com_name)
|
||||
return tuple(response)
|
||||
|
||||
Reference in New Issue
Block a user