From 14be5cfffc7ed0132cf8e0fa6e1bcdef075ad261 Mon Sep 17 00:00:00 2001 From: Brian Bjarke Jensen Date: Sat, 24 Feb 2024 00:04:34 +0100 Subject: [PATCH] restructured app --- src/web/app.py | 79 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/src/web/app.py b/src/web/app.py index eac840a..6cc300b 100644 --- a/src/web/app.py +++ b/src/web/app.py @@ -1,28 +1,65 @@ -from dash import Dash, html, Input, Output, State, page_container, page_registry +from dash import Dash, Input, Output import dash_bootstrap_components as dbc -import dash_mantine_components as dmc - -from .header import generate_header -from .body import generate_body +import logging +from .layout import app_layout +from src.database import ( + connect, + get_visual_communication, + NoDocumentFoundException +) +# setup app app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) +app.title = "visual critical discourse analysis".title() +app.layout = app_layout server = app.server -app.layout = dmc.MantineProvider( - theme={ - 'fontFamily': '"Inter", sans-serif', - "components": { - "NavLink":{'styles':{'label':{'color':'#c2c7d0'}}} - }, - }, - children=[ - dmc.Container( - [ - generate_header(), - generate_body(), - ], fluid=True - ), - ], +# connect to database +collection, db, client = connect() + +# define callbacks +@app.callback( + Output("alert-element", "is_open"), + Output("alert-element", "children"), + Input("alert-message", "data") ) - \ No newline at end of file +def show_alert( + msg: str | None +): + if msg is None: + return False, "" + return True, msg + +@app.callback( + Output("alert-message", "data"), + Output("visual-communication-name", "data"), + Output("image-container", "src"), + Input("next-button", "n_clicks"), + prevent_initial_call=True +) +def load_unannotated_visual_communication_data( + n_clicks: int +): + global collection + try: + vis_com = get_visual_communication( + collection=collection, + with_annotation=False + ) + except NoDocumentFoundException: + return ( + "Did not find any unannotated data in database", + None, + "" + ) + # prepare return values + alert_message = None + vis_com_name = vis_com.name + img_src = f"data:image/png;base64, {vis_com.webencoded_image()}" + logging.info("updated visual communication") + return ( + alert_message, + vis_com_name, + img_src + )