Add support for importing moodle result csv files.
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import streamlit as st
|
||||
|
||||
reserved_columns = ['Nachname', 'Vorname', 'Punkte', 'Note']
|
||||
|
||||
def update_columns(new_columns, table):
|
||||
if len(table) > 0:
|
||||
# Drop table columns that are not in the list of columns
|
||||
for column in table.columns:
|
||||
if column not in new_columns and column not in reserved_columns+list(st.session_state.session.students_df.columns):
|
||||
table.drop(column, axis=1, inplace=True)
|
||||
# Add columns that are in the list of columns but not in the table
|
||||
for column in new_columns:
|
||||
if column not in table.columns:
|
||||
table[column] = pd.Series(np.nan, index=table.index, dtype='float64')
|
||||
# Reorder columns
|
||||
before_reorder = table.columns
|
||||
original_columns = [col for col in before_reorder if col not in new_columns]
|
||||
table = table[original_columns + new_columns]
|
||||
return table
|
||||
@@ -0,0 +1,47 @@
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from io import StringIO
|
||||
|
||||
|
||||
pandas_json_format = 'table'
|
||||
|
||||
def read_csv(table_str: StringIO) -> pd.DataFrame:
|
||||
|
||||
try:
|
||||
df = pd.read_csv(table_str, engine='python', skipfooter=2)
|
||||
except:
|
||||
return pd.DataFrame()
|
||||
|
||||
df = df.set_index(['Nachname', 'Vorname'])
|
||||
|
||||
|
||||
# Rename columns to replace commas.
|
||||
# Replace with _ as AggGrid does not seem to support columns with decimals in name
|
||||
df = df.rename(columns=lambda x: x.replace(',','_'))
|
||||
exercises = [col for col in list(df.columns) if 'F' in col]
|
||||
df.update(df[exercises].map(lambda v: v.replace(',','.'))) # German floats -> international floats
|
||||
df.update(df[exercises].map(lambda v: v.replace('-','0.0'))) # '-' -> 0
|
||||
df = df.astype({k: 'float64' for k in exercises})
|
||||
|
||||
return df
|
||||
|
||||
|
||||
def parse_header(df: pd.DataFrame) -> tuple[int, list[str]]:
|
||||
max_points = [col for col in list(df.columns) if 'Bewertung' in col]
|
||||
if len(max_points) != 1:
|
||||
raise ValueError("Could not find column 'Bewertung' in table!")
|
||||
max_points = float(max_points[0].split('/', 1)[1].replace('_','.'))
|
||||
|
||||
exercises = [col for col in list(df.columns) if 'F' in col]
|
||||
if len(exercises) < 1:
|
||||
raise ValueError("Table does not seem to contain exercise points!")
|
||||
return max_points, exercises
|
||||
|
||||
|
||||
def merge_points(points_df_hisio: pd.DataFrame, points_df_moodle: pd.DataFrame) -> tuple[pd.DataFrame, int]:
|
||||
points_df_hisio = points_df_hisio.reset_index().set_index(['Nachname', 'Vorname'])
|
||||
common_keys = points_df_hisio.index.intersection(points_df_moodle.index)
|
||||
update_count = len(common_keys)
|
||||
points_df_hisio.update(points_df_moodle)
|
||||
points_df_hisio = points_df_hisio.reset_index().set_index('Matrikelnummer')
|
||||
return points_df_hisio, update_count
|
||||
Reference in New Issue
Block a user