recopytex/recopytex/scripts/exam_dash.py

141 lines
4.5 KiB
Python
Raw Normal View History

2021-01-10 19:46:14 +00:00
#!/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
2021-01-12 16:25:58 +00:00
from datetime import datetime
2021-01-10 19:46:14 +00:00
import pandas as pd
from .. import flat_df_students, pp_q_scores
2021-01-12 16:25:58 +00:00
from ..config import NO_ST_COLUMNS
2021-01-10 19:46:14 +00:00
from .getconfig import config, CONFIGPATH
COLORS = {
".": "black",
0: "#E7472B",
1: "#FF712B",
2: "#F2EC4C",
3: "#68D42F",
}
app = dash.Dash(__name__)
app.layout = html.Div([
2021-01-12 16:25:58 +00:00
html.H1("Analyse des notes"),
2021-01-10 19:46:14 +00:00
html.Div(["Classe: ", dcc.Dropdown(
id='tribe',
options=[{"label": t["name"], "value": t["name"]} for t in config["tribes"]],
value=config["tribes"][0]["name"],
)]),
2021-01-12 16:25:58 +00:00
html.Div(["Evaluation: ", dcc.Dropdown(id='csv')]),
2021-01-10 19:46:14 +00:00
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",
2021-01-12 16:25:58 +00:00
columns = [{"id": c, "name":c} for c in NO_ST_COLUMNS.values()],
2021-01-10 19:46:14 +00:00
style_cell={
'whiteSpace': 'normal',
'height': 'auto',
},
2021-01-12 16:25:58 +00:00
style_data_conditional=[],
editable=True,
2021-01-10 19:46:14 +00:00
)]),
2021-01-12 16:25:58 +00:00
html.P(id="lastsave"),
2021-01-10 19:46:14 +00:00
])
@app.callback(
2021-01-12 16:25:58 +00:00
[dash.dependencies.Output("csv", "options"), dash.dependencies.Output("csv", "value")],
2021-01-10 19:46:14 +00:00
[dash.dependencies.Input("tribe", "value")],
)
2021-01-12 16:25:58 +00:00
def update_csvs(value):
2021-01-10 19:46:14 +00:00
if not value:
raise PreventUpdate
p = Path(value)
csvs = list(p.glob("*.csv"))
2021-01-12 16:25:58 +00:00
try:
return [{"label": str(c), "value": str(c)} for c in csvs], str(csvs[0])
except IndexError:
return []
2021-01-10 19:46:14 +00:00
@app.callback(
[dash.dependencies.Output("final_score_table", "columns"), dash.dependencies.Output("final_score_table", "data")],
2021-01-12 16:25:58 +00:00
[dash.dependencies.Input("scores_table", "data")],
2021-01-10 19:46:14 +00:00
)
2021-01-12 16:25:58 +00:00
def update_final_scores_table(data):
if not data:
2021-01-10 19:46:14 +00:00
raise PreventUpdate
try:
2021-01-12 16:25:58 +00:00
scores = pd.DataFrame.from_records(data)
2021-01-10 19:46:14 +00:00
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
2021-01-12 16:25:58 +00:00
@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()]
2021-01-10 19:46:14 +00:00
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'
2021-01-12 16:25:58 +00:00
} for col in df.columns if col not in NO_ST_COLUMNS.values()
2021-01-10 19:46:14 +00:00
]
return hight
@app.callback(
[dash.dependencies.Output("scores_table", "columns"), dash.dependencies.Output("scores_table", "data"), dash.dependencies.Output("scores_table", "style_data_conditional"), ],
2021-01-12 16:25:58 +00:00
[dash.dependencies.Input("csv", "value")],
2021-01-10 19:46:14 +00:00
)
def update_scores_table(value):
if not value:
raise PreventUpdate
2021-01-12 16:25:58 +00:00
stack = pd.read_csv(value, encoding="UTF8")
# try:
# stack = stack.drop(columns=["Nom", "Trimestre", "Date", "Competence", "Domaine", "Est_nivele", "Bareme"])
# except KeyError:
# stack = stack
2021-01-10 19:46:14 +00:00
return [{"id": c, "name": c} for c in stack.columns], stack.to_dict('records'), highlight_value(stack)