pre-commit renamed files to PEP standard and removed unused imports
pipeline / Test (push) Failing after 51s

This commit is contained in:
Brian Bjarke Jensen
2024-02-25 14:26:54 +01:00
parent ea28f23304
commit ce063822b5
12 changed files with 230 additions and 215 deletions
+46 -42
View File
@@ -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)
+47 -50
View File
@@ -1,27 +1,24 @@
from dash import html, dcc
from __future__ import annotations
import dash_mantine_components as dmc
from typing import List
from src.model_experiential import (
VisualSyntaxModelOutput
)
from src.model_interpersonal import (
ContactModelOutput,
AngleModelOutput,
PointOfViewModelOutput,
DistanceModelOutput,
ModalityLightingModelOutput,
ModalityColorModelOutput,
ModalityDepthModelOutput
)
from src.model_textual import (
InformationValueModelOutput,
FramingModelOutput,
SalienceModelOutput
from dash import dcc
from dash import html
from model_experiential import (
VisualSyntaxModelOutput,
)
from model_interpersonal import AngleModelOutput
from model_interpersonal import ContactModelOutput
from model_interpersonal import DistanceModelOutput
from model_interpersonal import ModalityColorModelOutput
from model_interpersonal import ModalityDepthModelOutput
from model_interpersonal import ModalityLightingModelOutput
from model_interpersonal import PointOfViewModelOutput
from model_textual import FramingModelOutput
from model_textual import InformationValueModelOutput
from model_textual import SalienceModelOutput
def generate_option_labels(model) -> List[str]:
def generate_option_labels(model) -> list[str]:
"""Generate presentable list of attributes from an OutputModel."""
labels = [
label.replace('_', ' ').title()
@@ -34,8 +31,8 @@ def generate_visual_syntax_options_map():
"""Generate map of titles and options for visual syntax labels."""
options_map = {}
# add experiential labels
options_map["visual syntax"] = generate_option_labels(
VisualSyntaxModelOutput
options_map['visual syntax'] = generate_option_labels(
VisualSyntaxModelOutput,
)
return options_map
@@ -44,20 +41,20 @@ def generate_interpersonal_options_map():
"""Generate map of titles and options for interpersonal labels."""
options_map = {}
# add interpersonal labels
options_map["contact"] = generate_option_labels(ContactModelOutput)
options_map["angle"] = generate_option_labels(AngleModelOutput)
options_map["point of view"] = generate_option_labels(
PointOfViewModelOutput
options_map['contact'] = generate_option_labels(ContactModelOutput)
options_map['angle'] = generate_option_labels(AngleModelOutput)
options_map['point of view'] = generate_option_labels(
PointOfViewModelOutput,
)
options_map["distance"] = generate_option_labels(DistanceModelOutput)
options_map["modality lighting"] = generate_option_labels(
ModalityLightingModelOutput
options_map['distance'] = generate_option_labels(DistanceModelOutput)
options_map['modality lighting'] = generate_option_labels(
ModalityLightingModelOutput,
)
options_map["modality color"] = generate_option_labels(
ModalityColorModelOutput
options_map['modality color'] = generate_option_labels(
ModalityColorModelOutput,
)
options_map["modality depth"] = generate_option_labels(
ModalityDepthModelOutput
options_map['modality depth'] = generate_option_labels(
ModalityDepthModelOutput,
)
return options_map
@@ -66,11 +63,11 @@ def generate_textual_options_map():
"""Generate map of titles and options for textual labels."""
options_map = {}
# add textual labels
options_map["information value"] = generate_option_labels(
InformationValueModelOutput
options_map['information value'] = generate_option_labels(
InformationValueModelOutput,
)
options_map["framing"] = generate_option_labels(FramingModelOutput)
options_map["salience"] = generate_option_labels(SalienceModelOutput)
options_map['framing'] = generate_option_labels(FramingModelOutput)
options_map['salience'] = generate_option_labels(SalienceModelOutput)
return options_map
@@ -78,11 +75,11 @@ def generate_textual_options_map():
experiential_map = generate_visual_syntax_options_map()
experiential_container = dmc.Col(
children=[
html.H4("experiential".title()),
], span=5
html.H4('experiential'.title()),
], span=5,
)
for title, options in experiential_map.items():
id_dict = {"type": "annotation", "index": title.replace('_', '-')}
id_dict = {'type': 'annotation', 'index': title.replace('_', '-')}
experiential_container.children.append(
dmc.Container([
html.B(title.title()),
@@ -90,17 +87,17 @@ for title, options in experiential_map.items():
options=options,
id=id_dict,
),
])
]),
)
# prepare interpersonal container
interpersonal_map = generate_interpersonal_options_map()
interpersonal_container = dmc.Col(
children=[
html.H4("interpersonal".title()),
], span=3
html.H4('interpersonal'.title()),
], span=3,
)
for title, options in interpersonal_map.items():
id_dict = {"type": "annotation", "index": title.replace('_', '-')}
id_dict = {'type': 'annotation', 'index': title.replace('_', '-')}
interpersonal_container.children.append(
dmc.Container([
html.B(title.title()),
@@ -108,17 +105,17 @@ for title, options in interpersonal_map.items():
options=options,
id=id_dict,
),
])
]),
)
# prepare textual container
textual_map = generate_textual_options_map()
textual_container = dmc.Col(
children=[
html.H4("textual".title()),
], span=4
html.H4('textual'.title()),
], span=4,
)
for title, options in textual_map.items():
id_dict = {"type": "annotation", "index": title.replace('_', '-')}
id_dict = {'type': 'annotation', 'index': title.replace('_', '-')}
textual_container.children.append(
dmc.Container([
html.B(title),
@@ -126,7 +123,7 @@ for title, options in textual_map.items():
options=options,
id=id_dict,
),
])
]),
)
labels_element = dmc.Grid(
@@ -134,5 +131,5 @@ labels_element = dmc.Grid(
experiential_container,
interpersonal_container,
textual_container,
]
],
)