#!/usr/bin/env python # encoding: utf-8 from dash.dependencies import Input, Output, State import dash from dash.exceptions import PreventUpdate import dash_table import json import pandas as pd from ...app import app from .models import get_tribes, get_exams, get_unstack_scores, get_students_from_exam @app.callback( [ Output("exam_select", "options"), Output("exam_select", "value"), ], [Input("tribe", "value")], ) def update_exams_choices(tribe): if not tribe: raise PreventUpdate exams = get_exams(tribe) exams.reset_index(inplace=True) if not exams.empty: return [ {"label": e["name"], "value": e.to_json()} for i, e in exams.iterrows() ], exams.loc[0].to_json() return [], None @app.callback( [ Output("scores_table", "columns"), Output("scores_table", "data"), Output("scores_table", "style_data_conditional"), Output("scores_table", "fixed_columns"), ], [ Input("exam_select", "value"), ], ) def update_scores_store(exam): ctx = dash.callback_context if not exam: return [[], [], [], {}] exam = pd.DataFrame.from_dict([json.loads(exam)]) scores = get_unstack_scores(exam) fixed_columns = [ "exercise", "question", "competence", "theme", "comment", "score_rate", "is_leveled", ] columns = fixed_columns + list(get_students_from_exam(exam)) return [ [{"id": c, "name": c} for c in columns], scores.to_dict("records"), [], {"headers": True, "data": len(fixed_columns)}, ]