Feat: autosave while editing scores

This commit is contained in:
Bertrand Benjamin 2021-01-12 17:25:58 +01:00
parent 8fcad94df4
commit cfd5928853

View File

@ -7,10 +7,12 @@ import dash_core_components as dcc
import dash_table import dash_table
from dash.exceptions import PreventUpdate from dash.exceptions import PreventUpdate
from pathlib import Path from pathlib import Path
from datetime import datetime
import pandas as pd import pandas as pd
from .. import flat_df_students, pp_q_scores from .. import flat_df_students, pp_q_scores
from ..config import NO_ST_COLUMNS
from .getconfig import config, CONFIGPATH from .getconfig import config, CONFIGPATH
COLORS = { COLORS = {
@ -24,13 +26,13 @@ COLORS = {
app = dash.Dash(__name__) app = dash.Dash(__name__)
app.layout = html.Div([ app.layout = html.Div([
html.H1("Coucou"), html.H1("Analyse des notes"),
html.Div(["Classe: ", dcc.Dropdown( html.Div(["Classe: ", dcc.Dropdown(
id='tribe', id='tribe',
options=[{"label": t["name"], "value": t["name"]} for t in config["tribes"]], options=[{"label": t["name"], "value": t["name"]} for t in config["tribes"]],
value=config["tribes"][0]["name"], value=config["tribes"][0]["name"],
)]), )]),
html.Div(["Evaluation: ", dcc.Dropdown(id='exam')]), html.Div(["Evaluation: ", dcc.Dropdown(id='csv')]),
html.Div([dash_table.DataTable( html.Div([dash_table.DataTable(
id="final_score_table", id="final_score_table",
columns = [{"id": "Élève", "name": "Élève"}, {"id": "Note", "name": "Note"},{"id": "Barème", "name": "Bareme"}], 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.Br(),
html.Div([dash_table.DataTable( html.Div([dash_table.DataTable(
id="scores_table", id="scores_table",
columns = [{"id": "plop", "name": "popo"}], columns = [{"id": c, "name":c} for c in NO_ST_COLUMNS.values()],
style_cell={ style_cell={
'whiteSpace': 'normal', 'whiteSpace': 'normal',
'height': 'auto', 'height': 'auto',
}, },
style_data_conditional=[] style_data_conditional=[],
editable=True,
)]), )]),
html.P(id="lastsave"),
]) ])
@app.callback( @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")], [dash.dependencies.Input("tribe", "value")],
) )
def update_exams(value): def update_csvs(value):
if not value: if not value:
raise PreventUpdate raise PreventUpdate
p = Path(value) p = Path(value)
csvs = list(p.glob("*.csv")) csvs = list(p.glob("*.csv"))
try:
return [{"label": str(c), "value": str(c)} for c in csvs], str(csvs[0]) return [{"label": str(c), "value": str(c)} for c in csvs], str(csvs[0])
except IndexError:
return []
@app.callback( @app.callback(
[dash.dependencies.Output("final_score_table", "columns"), dash.dependencies.Output("final_score_table", "data")], [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): def update_final_scores_table(data):
if not value: if not data:
raise PreventUpdate raise PreventUpdate
try: try:
scores = pd.read_csv(value, encoding="UTF8") scores = pd.DataFrame.from_records(data)
comments = scores.iloc[0]
scores.drop([0], inplace=True)
scores = flat_df_students(scores).dropna(subset=["Score"]) scores = flat_df_students(scores).dropna(subset=["Score"])
scores = pp_q_scores(scores) scores = pp_q_scores(scores)
assessment_scores = scores.groupby(["Eleve"]).agg({"Note": "sum", "Bareme": "sum"}) assessment_scores = scores.groupby(["Eleve"]).agg({"Note": "sum", "Bareme": "sum"})
@ -93,6 +98,16 @@ def update_scores_table(value):
except KeyError: except KeyError:
raise PreventUpdate 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): def highlight_value(df):
""" Cells style """ """ Cells style """
@ -106,20 +121,20 @@ def highlight_value(df):
}, },
'backgroundColor': color, 'backgroundColor': color,
'color': 'white' '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 return hight
@app.callback( @app.callback(
[dash.dependencies.Output("scores_table", "columns"), dash.dependencies.Output("scores_table", "data"), dash.dependencies.Output("scores_table", "style_data_conditional"), ], [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): def update_scores_table(value):
if not value: if not value:
raise PreventUpdate raise PreventUpdate
scores = pd.read_csv(value, encoding="UTF8") stack = pd.read_csv(value, encoding="UTF8")
try: # try:
stack = scores.drop(columns=["Nom", "Trimestre", "Date", "Competence", "Domaine", "Est_nivele", "Bareme"]) # stack = stack.drop(columns=["Nom", "Trimestre", "Date", "Competence", "Domaine", "Est_nivele", "Bareme"])
except KeyError: # except KeyError:
stack = scores # stack = stack
return [{"id": c, "name": c} for c in stack.columns], stack.to_dict('records'), highlight_value(stack) return [{"id": c, "name": c} for c in stack.columns], stack.to_dict('records'), highlight_value(stack)