From 4aa49e171da526210a807a1cd99af5b44c39aabd Mon Sep 17 00:00:00 2001 From: adish-rmr Date: Thu, 22 Jan 2026 22:42:25 +0100 Subject: [PATCH] fix multiple cas --- app.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 5bae1d4..0ca1496 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,5 @@ +import re + import streamlit as st from functions_ui import search_cas_inci @@ -68,13 +70,26 @@ def home(): # Salva solo il CAS selezionato nel session_state (estrae la parte prima del " - ") if selected_display and selected_display != "": - selected_cas = selected_display.split(" - ")[0] - if ";" in selected_cas: - selected_cas = st.selectbox(options=selected_cas.split(";"), label="CAS multipli, selezionarne uno:") - elif "/" in selected_cas: - selected_cas = st.selectbox(options=selected_cas.split("/"), label="CAS multipli, selezionarne uno:") - st.session_state.selected_cas = selected_cas.strip() - st.success(f"CAS selezionato: {selected_cas}") + cas_pattern = r'\b\d{2,7}-\d{2}-\d\b' + found_cas = re.findall(cas_pattern, selected_display) + unique_cas = list(dict.fromkeys(found_cas)) + + if not unique_cas: + st.warning("Nessun pattern CAS valido trovato nella stringa.") + selected_cas = None + + elif len(unique_cas) == 1: + selected_cas = unique_cas[0] + st.info(f"CAS rilevato: {selected_cas}") + + else: + selected_cas = st.selectbox( + label="Sono stati rilevati più CAS. Selezionane uno:", + options=unique_cas + ) + if selected_cas: + st.session_state.selected_cas = selected_cas + st.success(f"CAS selezionato: {selected_cas}") else: # Nessun risultato trovato: permetti di usare l'input manuale st.warning("Nessun risultato trovato nel database.")