import pandas as pd import numpy as np import streamlit as st import streamlit as ste from typing import Optional, List, Dict, Any from st_aggrid import AgGrid from st_aggrid.grid_options_builder import GridOptionsBuilder from io import BytesIO import xlsxwriter from datetime import date, datetime from helpertools_TM import helpers helper = helpers() class makeInterface: """ A class to create and manage the user interface for data validation and processing. This class handles file uploads, creates the interface layout, manages validation checks, and provides functionality for displaying and downloading results. Attributes: valori: Configuration values and constants for the interface ANNO_RIFERIMENTO (str): The reference year for data validation DEBUGWARNINGS (bool): Flag for showing debug warnings TOLLERANZAZERO (bool): Flag for zero tolerance mode ERRORDICT (dict): Dictionary of error types and descriptions ERROR_ICONS (dict): Dictionary mapping error types to their icons """ def __init__(self, valori): """ Initialize the interface with configuration values. Args: valori: Object containing configuration values and constants """ self.valori = valori # self.ERRDICT = ERRDICT # st.write(ERRDICT) # Add spacer at the top of sidebar to push elements down st.sidebar.markdown('
', unsafe_allow_html=True) # st.write(self.ERRDICT) self.ANNO_RIFERIMENTO = st.sidebar.selectbox( "ANNO RIFERIMENTO", (["2023","2024", "2025", "2026"]), index=1 ) self.make_sidebar() self.DEBUGWARNINGS = False self.TOLLERANZAZERO = False self.STATUS = st.sidebar.empty() self.STATUS2 = st.sidebar.empty() self.STATUS3 = st.sidebar.empty() try: self.ERRORDICT = valori.ERRORDICT[self.ANNO_RIFERIMENTO] self.ERROR_ICONS = valori.ERROR_ICONS except Exception as e: self.STATUS.error( f"Non e' stato possibile creare la lista dei controlli. Forse non sono stati definiti per l'anno {self.ANNO_RIFERIMENTO}? Errore: {e}" ) if self.TOLLERANZAZERO or self.DEBUGWARNINGS: st.error("Elaborazione interrotta") st.stop() self.make_form() # Use the unified file upload method instead of the separate ones self.upload_all_files() self.choose_checks() self.apply_custom_theme() def apply_custom_theme(self): """Apply custom styling to improve the overall appearance of the interface.""" st.markdown(""" """, unsafe_allow_html=True) def get_version(self) -> str: return "Interface 1.0.0" def make_sidebar(self): """ Create the sidebar interface with configuration options. Creates checkboxes and radio buttons for various settings including: - Integrity message display - Date string conversion - Processing interruption settings - Constants display - General debug mode """ # st.sidebar.info("Einstellungen") # expander = st.sidebar.expander("Impostazioni generali") with st.sidebar.expander("Configurazione"): self.COMUNETEDESCO = st.checkbox( "Mostra comuni in tedesco", False ) self.MOSTRAERRORIINTEGRITA = st.checkbox( "Mostra messaggi integrita' strutturale", True ) self.CONVERTIDATESTRINGHE = st.checkbox( "Convertire le date in stringhe", True ) self.INTERRUZIONE = st.radio( "Interrompere elaborazione se...", ("Non interrompere", "Warnings", "Tolleranza zero"), ) if self.INTERRUZIONE == "Warnings": self.DEBUGWARNINGS = True elif self.INTERRUZIONE == "Tolleranza zero": self.TOLLERANZAZERO = True self.MOSTRACOSTANTI = st.checkbox("Mostra costanti per anno riferimento") if self.MOSTRACOSTANTI: try: st.info(self.valori.COSTANTI[self.ANNO_RIFERIMENTO]) except: st.error("Impossibile mostrare costanti") self.DEBUGGENERAL = st.checkbox("Debug generale") # def get_file_list(self): # """ # Create a file uploader for selecting multiple Excel files. # """ # self.uploaded_files = st.file_uploader( # "ExcelDateienAuswaehlen", accept_multiple_files=True # ) # # st.write(self.filelist) def make_expander(self, title1, title2, count, dftemp): """ Create an expandable section to display data results. Args: title1 (str): Primary title for the expander title2 (str): Secondary title/description count (int): Number of occurrences found dftemp (pd.DataFrame): DataFrame containing the results to display """ with st.expander(f"Trovate **{count}** occorrenze per {title1}"): st.info(title2) # st.write(dftemp) # Create a grid display of the sorted dataframe self.make_grid( dftemp.sort_values(by=["Cognome", "Nome"]), title1, ) self.download_excel_file( dftemp.sort_values(by=["Cognome", "Nome"]), f"{title1}.xlsx", ) def make_form(self) -> None: """ Create a sidebar form with a submit button. The form includes a submit button that triggers the start of data processing and error checking. """ frm = st.sidebar.form("Auswahl", clear_on_submit=True) with frm: # This solution is somewhat awkward, but we want to move the expanders outside of the form # so that we can use the button for download self.submit = frm.form_submit_button("Iniziare elaborazione e controlli") # def choose_files(self, c1) -> None: # """Allow users to upload multiple Excel files for checking.""" # self.uploaded_files: List[FileUploadBase] = c1.file_uploader( # "SCEGLIERE FILE EXCEL DA CONTROLLARE", accept_multiple_files=True # ) # def choose_allegato666(self, c2) -> None: # """ # Allow users to upload the Allegato 666 file for checking.# # Args: # None # Returns: # None # """ # self.uploaded_allegato666: FileUploadBase = c2.file_uploader( # "CARICARE ALLEGATO 666", accept_multiple_files=False # ) def choose_checks(self) -> None: """ Creates a dynamic list of checkboxes based on a dictionary containing error names and descriptions. Updates the 'checks' dictionary with the selected checkboxes. :return: None """ # Initialize the checks dictionary self.checks: Dict[str, bool] = {} # Create an expander to hold the checkboxes expndr = st.expander( "SELEZIONE CONTROLLI DA ESEGUIRE (i controlli sull'integrità dei dati vengono eseguiti sempre)", expanded=False, ) try: with expndr: # Create a 'Select All' checkbox select_all = st.checkbox( "🗂️ Selezionare/deselezionare tutti i controlli", value=True, key="select_all_checks", ) # Create 4 columns to distribute checkboxes evenly columns = expndr.columns(4) # Iterate over the ERRORDICT items with enumeration for idx, (error_key, error_description) in enumerate( self.ERRORDICT.items() ): # Determine the appropriate column for the current checkbox col = columns[idx % 4] # Retrieve the corresponding icon, default to a warning emoji if not found icon = self.ERROR_ICONS.get(error_key, "⚠️") # Create the checkbox within the determined column self.checks[error_key] = col.checkbox( label=f"{icon} {error_description}", value=select_all, key=f"{error_key}_checkbox", ) except Exception as e: with expndr: # Display a warning message if an error occurs st.warning( f"Non è stato possibile creare la lista dei controlli. Forse non sono stati definiti per l'anno {self.ANNO_RIFERIMENTO}? Errore: {e}" ) # Optionally display an error and stop execution based on flags if getattr(self, "TOLLERANZAZERO", False) or getattr( self, "DEBUGWARNINGS", False ): st.error("Elaborazione interrotta") st.stop() def make_tabs(self) -> None: """ Create the main navigation tabs for the application. Creates three tabs: - Data Integrity - Errors and Inconsistencies - Final Tables """ self.TAB1, self.TAB2, self.TAB3 = st.tabs( [ "INTEGRITÀ DATI", "ERRORI e INCONGRUENZE RISCONTRATI", "TABELLE FINALI", ] ) @st.fragment() def buildGrid(self, data: pd.DataFrame): """ Build a grid configuration for displaying tabular data. Args: data (pd.DataFrame): The DataFrame to be displayed in the grid Returns: dict: Grid options configuration for AG-Grid """ gb = GridOptionsBuilder.from_dataframe(data) # Configure pagination settings gb.configure_pagination(paginationAutoPageSize=True, paginationPageSize=20) # Configure side bar for column filtering and selection gb.configure_side_bar() # Configure selection mode to multiple rows with checkbox gb.configure_selection(selection_mode="multiple", use_checkbox=True) # Configure default column properties gb.configure_default_column( groupable=True, value=True, enableRowGroup=True, aggFunc="count", editable=True, ) gridOptions = gb.build() return gridOptions @st.fragment() def make_grid(self, dff: pd.DataFrame, k: str) -> None: """ Create and display an interactive data grid. Args: dff (pd.DataFrame): DataFrame to be displayed in the grid k (str): Unique key identifier for the grid component """ # st.write(dff) # Drop unnecessary columns from the DataFrame helper = helpers() dff = helper.drop_columns(dff, self.ERRORDICT) # st.write(dff) # Append a new row with "filename" set to an empty string new_row_df = pd.DataFrame({"filename": " "}, index=[0]) dff = pd.concat([dff, new_row_df], ignore_index=True) # Append another new row with "filename" set to the current date new_row_data = { "filename": "file generato in data: " + date.today().strftime("%d/%m/%Y") } new_row_df = pd.DataFrame(new_row_data, index=[0]) dff = pd.concat([dff, new_row_df], ignore_index=True) # Build the grid options object gridOptions = self.buildGrid(dff) # Display the grid using AgGrid AgGrid(dff, gridOptions=gridOptions, enable_enterprise_modules=True, key=k) @st.fragment() def download_excel_file(self, dftosave: pd.DataFrame, nome_file: str, flag: Optional[str] = None) -> None: """ Create and provide a download button for an Excel file with improved styling. Args: dftosave (pd.DataFrame): DataFrame to be saved as Excel nome_file (str): Name of the output file flag (Optional[str]): Flag to control column handling ('mantieniColonneErrore' to keep error columns) Raises: KeyError: If required columns are missing from the DataFrame ValueError: If there are invalid or incompatible values Exception: For other unexpected errors """ try: # Step 1: Remove 'Colonna_L_da_cancellare_dopo' if it exists dftosave = dftosave.drop( columns=["Colonna_L_da_cancellare_dopo"], errors="ignore" ) # Step 2: Add timestamp to the file name timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") nome_file = f"{timestamp}_{nome_file}" # Step 3: Drop columns based on the flag if flag == "mantieniColonneErrore": pass else: dftosave = helper.drop_columns(dftosave, self.ERRORDICT) # Step 4: Initialize an in-memory buffer buffer = BytesIO() # Step 5: Write DataFrame to the buffer using ExcelWriter with pd.ExcelWriter(buffer, engine="xlsxwriter") as writer: dftosave.to_excel(writer, sheet_name="risultato", index=False) workbook = writer.book worksheet = writer.sheets["risultato"] # Add a header format header_format = workbook.add_format({ 'bold': True, 'bg_color': '#4E8CFF', 'color': 'white', 'border': 1, 'align': 'center' }) # Add data format data_format = workbook.add_format({ 'border': 1 }) # Apply the header format to the first row for col_num, value in enumerate(dftosave.columns.values): worksheet.write(0, col_num, value, header_format) # Step 6: Dynamically set column widths based on content for idx, col in enumerate(dftosave.columns): # Calculate the maximum length of the column content max_length = dftosave[col].astype(str).map(len).max() max_length = max(max_length, len(col)) + 2 # Adding extra space worksheet.set_column(idx, idx, max_length, data_format) # Add autofilter worksheet.autofilter(0, 0, len(dftosave), len(dftosave.columns) - 1) # Step 7: Retrieve the Excel file bytes from the buffer buffer.seek(0) file_bytes = buffer.read() # Better styled download section col1, col2 = st.columns([1, 2]) with col1: # Step 8: Add an icon to the download button using an emoji ste.download_button( label="📥 Scarica Excel", data=file_bytes, file_name=nome_file, mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", use_container_width=True, ) with col2: st.markdown(f"""❌ Errore: I seguenti file non sono in formato Excel e non verranno elaborati:
✅ Allegato 666: {self.uploaded_allegato666.name}
❌ Errore: Più file contengono '666' nel nome ({len(allegato_666_files)})
⚠️ Attenzione: Allegato 666 mancante
ℹ️ Elenchi caricati: {len(other_files)}
⚠️ Attenzione: Nessun file Excel aggiuntivo