packages = ["numpy", "matplotlib"] >

Weekly Attendance

Waiting for attendance data...

Attendance Chart

from pyscript import document, display import numpy as np import matplotlib.pyplot as plt import logging # Hide matplotlib logs logging.getLogger('matplotlib').setLevel(logging.ERROR) # Data storage days = [] absences = [] # Preload matplotlib plt.figure() plt.close() def displaying(event): # Get values day = document.getElementById("dayOfTheWeek").value absence_value = document.getElementById("absences").value # Validation if absence_value == "": document.getElementById("output").innerText = ( "Please enter a value between 0 and 100." ) return # Convert to integer absence = int(absence_value) # Limit validation if absence < 0 or absence > 100: document.getElementById("output").innerText = ( "Absences must only be between 0 and 100." ) return # Save data days.append(day) absences.append(absence) # Convert to NumPy array absence_array = np.array(absences) # Statistics total_absences = np.sum(absence_array) average_absences = np.mean(absence_array) # Update output document.getElementById("output").innerText = ( f"{day}: {absence} absences added | " f"Total: {total_absences} | " f"Average: {average_absences:.2f}" ) # Clear graph plt.close('all') # Create graph fig, ax = plt.subplots(figsize=(7,4)) # Bar graph ax.bar(days, absence_array) # Line graph ax.plot(days, absence_array, marker='o', linewidth=2) # Labels ax.set_title("Weekly Attendance Chart") ax.set_xlabel("Days") ax.set_ylabel("Absences") # Grid ax.grid(True) # Display graph display(fig, target="plot", append=False)