From 8fcad94df4efe509da130052182a488b4526f7ff Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sun, 10 Jan 2021 20:46:14 +0100 Subject: [PATCH] Feat: start analysis dash board --- recopytex/scripts/exam_dash.py | 125 +++++++++++++++++++++++++++++++++ recopytex/scripts/recopytex.py | 4 ++ 2 files changed, 129 insertions(+) create mode 100644 recopytex/scripts/exam_dash.py diff --git a/recopytex/scripts/exam_dash.py b/recopytex/scripts/exam_dash.py new file mode 100644 index 0000000..5dc45f4 --- /dev/null +++ b/recopytex/scripts/exam_dash.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python +# encoding: utf-8 + +import dash +import dash_html_components as html +import dash_core_components as dcc +import dash_table +from dash.exceptions import PreventUpdate +from pathlib import Path +import pandas as pd + + +from .. import flat_df_students, pp_q_scores +from .getconfig import config, CONFIGPATH + +COLORS = { + ".": "black", + 0: "#E7472B", + 1: "#FF712B", + 2: "#F2EC4C", + 3: "#68D42F", +} + +app = dash.Dash(__name__) + +app.layout = html.Div([ + html.H1("Coucou"), + 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([dash_table.DataTable( + id="final_score_table", + columns = [{"id": "Élève", "name": "Élève"}, {"id": "Note", "name": "Note"},{"id": "Barème", "name": "Bareme"}], + data=[], + style_data_conditional=[ + { + 'if': {'row_index': 'odd'}, + 'backgroundColor': 'rgb(248, 248, 248)' + } + ], + style_header={ + 'backgroundColor': 'rgb(230, 230, 230)', + 'fontWeight': 'bold' + }, + style_data={ + 'width': '100px', + 'maxWidth': '100px', + 'minWidth': '100px', + }, + ), + ]), + html.Br(), + html.Div([dash_table.DataTable( + id="scores_table", + columns = [{"id": "plop", "name": "popo"}], + style_cell={ + 'whiteSpace': 'normal', + 'height': 'auto', + }, + style_data_conditional=[] + )]), + ]) + +@app.callback( + [dash.dependencies.Output("exam", "options"), dash.dependencies.Output("exam", "value")], + [dash.dependencies.Input("tribe", "value")], + ) +def update_exams(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]) + +@app.callback( + [dash.dependencies.Output("final_score_table", "columns"), dash.dependencies.Output("final_score_table", "data")], + [dash.dependencies.Input("exam", "value")], + ) +def update_scores_table(value): + if not value: + raise PreventUpdate + try: + scores = pd.read_csv(value, encoding="UTF8") + comments = scores.iloc[0] + scores.drop([0], inplace=True) + scores = flat_df_students(scores).dropna(subset=["Score"]) + scores = pp_q_scores(scores) + assessment_scores = scores.groupby(["Eleve"]).agg({"Note": "sum", "Bareme": "sum"}) + return [{"id": c, "name": c} for c in assessment_scores.reset_index().columns], assessment_scores.reset_index().to_dict('records') + except KeyError: + raise PreventUpdate + + +def highlight_value(df): + """ Cells style """ + hight = [] + for v, color in COLORS.items(): + hight +=[ + { + 'if': { + 'filter_query': '{{{}}} = {}'.format(col, v), + 'column_id': col + }, + 'backgroundColor': color, + 'color': 'white' + } for col in df.columns if col not in ["Exercice", "Question", "Commentaire"] + ] + 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")], + ) +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 + return [{"id": c, "name": c} for c in stack.columns], stack.to_dict('records'), highlight_value(stack) diff --git a/recopytex/scripts/recopytex.py b/recopytex/scripts/recopytex.py index 3cd5416..27079bd 100644 --- a/recopytex/scripts/recopytex.py +++ b/recopytex/scripts/recopytex.py @@ -13,6 +13,7 @@ from .getconfig import config, CONFIGPATH from .prompts import prompt_exam, prompt_exercise, prompt_validate from ..config import NO_ST_COLUMNS from .exam import Exam +from .exam_dash import app as exam_app @click.group() @@ -84,6 +85,9 @@ def new_exam(): base_df.to_csv(exam.path(".csv"), index=False) print(f"Le fichier note a été enregistré à {exam.path('.csv')}") +@cli.command() +def exam_analysis(): + exam_app.run_server(debug=True) @cli.command() @click.argument("csv_file")