From cfd592885303cef9cc2595556f89a340d8ca5423 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Tue, 12 Jan 2021 17:25:58 +0100 Subject: [PATCH] Feat: autosave while editing scores --- recopytex/scripts/exam_dash.py | 55 +++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/recopytex/scripts/exam_dash.py b/recopytex/scripts/exam_dash.py index 5dc45f4..3a06436 100644 --- a/recopytex/scripts/exam_dash.py +++ b/recopytex/scripts/exam_dash.py @@ -7,10 +7,12 @@ import dash_core_components as dcc import dash_table from dash.exceptions import PreventUpdate from pathlib import Path +from datetime import datetime import pandas as pd from .. import flat_df_students, pp_q_scores +from ..config import NO_ST_COLUMNS from .getconfig import config, CONFIGPATH COLORS = { @@ -24,13 +26,13 @@ COLORS = { app = dash.Dash(__name__) app.layout = html.Div([ - html.H1("Coucou"), + html.H1("Analyse des notes"), html.Div(["Classe: ", dcc.Dropdown( id='tribe', options=[{"label": t["name"], "value": t["name"]} for t in config["tribes"]], value=config["tribes"][0]["name"], )]), - html.Div(["Evaluation: ", dcc.Dropdown(id='exam')]), + html.Div(["Evaluation: ", dcc.Dropdown(id='csv')]), html.Div([dash_table.DataTable( id="final_score_table", columns = [{"id": "Élève", "name": "Élève"}, {"id": "Note", "name": "Note"},{"id": "Barème", "name": "Bareme"}], @@ -55,37 +57,40 @@ app.layout = html.Div([ html.Br(), html.Div([dash_table.DataTable( id="scores_table", - columns = [{"id": "plop", "name": "popo"}], + columns = [{"id": c, "name":c} for c in NO_ST_COLUMNS.values()], style_cell={ 'whiteSpace': 'normal', 'height': 'auto', }, - style_data_conditional=[] + style_data_conditional=[], + editable=True, )]), + html.P(id="lastsave"), ]) @app.callback( - [dash.dependencies.Output("exam", "options"), dash.dependencies.Output("exam", "value")], + [dash.dependencies.Output("csv", "options"), dash.dependencies.Output("csv", "value")], [dash.dependencies.Input("tribe", "value")], ) -def update_exams(value): +def update_csvs(value): if not value: raise PreventUpdate p = Path(value) csvs = list(p.glob("*.csv")) - return [{"label": str(c), "value": str(c)} for c in csvs], str(csvs[0]) + try: + return [{"label": str(c), "value": str(c)} for c in csvs], str(csvs[0]) + except IndexError: + return [] @app.callback( [dash.dependencies.Output("final_score_table", "columns"), dash.dependencies.Output("final_score_table", "data")], - [dash.dependencies.Input("exam", "value")], + [dash.dependencies.Input("scores_table", "data")], ) -def update_scores_table(value): - if not value: +def update_final_scores_table(data): + if not data: raise PreventUpdate try: - scores = pd.read_csv(value, encoding="UTF8") - comments = scores.iloc[0] - scores.drop([0], inplace=True) + scores = pd.DataFrame.from_records(data) scores = flat_df_students(scores).dropna(subset=["Score"]) scores = pp_q_scores(scores) assessment_scores = scores.groupby(["Eleve"]).agg({"Note": "sum", "Bareme": "sum"}) @@ -93,6 +98,16 @@ def update_scores_table(value): except KeyError: raise PreventUpdate +@app.callback( + [dash.dependencies.Output("lastsave", "children")], + [dash.dependencies.Input("scores_table", "data"), dash.dependencies.State("csv", "value")], + ) +def save_scores(data, csv): + scores = pd.DataFrame.from_records(data) + print(f"save at {csv} ({datetime.today()})") + scores.to_csv(csv, index=False) + return [datetime.today()] + def highlight_value(df): """ Cells style """ @@ -106,20 +121,20 @@ def highlight_value(df): }, 'backgroundColor': color, 'color': 'white' - } for col in df.columns if col not in ["Exercice", "Question", "Commentaire"] + } for col in df.columns if col not in NO_ST_COLUMNS.values() ] return hight @app.callback( [dash.dependencies.Output("scores_table", "columns"), dash.dependencies.Output("scores_table", "data"), dash.dependencies.Output("scores_table", "style_data_conditional"), ], - [dash.dependencies.Input("exam", "value")], + [dash.dependencies.Input("csv", "value")], ) def update_scores_table(value): if not value: raise PreventUpdate - scores = pd.read_csv(value, encoding="UTF8") - try: - stack = scores.drop(columns=["Nom", "Trimestre", "Date", "Competence", "Domaine", "Est_nivele", "Bareme"]) - except KeyError: - stack = scores + stack = pd.read_csv(value, encoding="UTF8") + # try: + # stack = stack.drop(columns=["Nom", "Trimestre", "Date", "Competence", "Domaine", "Est_nivele", "Bareme"]) + # except KeyError: + # stack = stack return [{"id": c, "name": c} for c in stack.columns], stack.to_dict('records'), highlight_value(stack)