Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from supabase import create_client, Client | |
| import qrcode | |
| from io import BytesIO | |
| # --- CONFIGURAZIONE --- | |
| VIEWER_URL = "https://winelink-e-label.hf.space" | |
| URL = "https://ulqepcglktyojwtnhnga.supabase.co" | |
| KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InVscWVwY2dsa3R5b2p3dG5obmdhIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzIwMDM0MjAsImV4cCI6MjA4NzU3OTQyMH0.JLcSI9fYNxjW0MSkJKexXU8KBRQUzrZEj7s7hcbXuAc" | |
| supabase: Client = create_client(URL, KEY) | |
| st.set_page_config(page_title="WineLink Admin", page_icon="π·") | |
| st.title("WineLink Admin Pro") | |
| # --- NUOVA RIGA DI GUIDA --- | |
| st.info("π‘ **Guida Rapida:** Inserisci i dati nel nuovo vino, poi seleziona 'Gestione Lista' per vedere, modificare o scaricare il QR Code.") | |
| azione = st.selectbox("OPERAZIONE:", ["π Nuovo Inserimento", "π Gestione Lista"]) | |
| def get_qr_bytes(url): | |
| qr = qrcode.QRCode(box_size=10, border=4) | |
| qr.add_data(url) | |
| qr.make(fit=True) | |
| img = qr.make_image(fill_color="#581845", back_color="white") | |
| buf = BytesIO() | |
| img.save(buf, format="PNG") | |
| return buf.getvalue() | |
| if azione == "π Nuovo Inserimento": | |
| with st.form("form_nuovo", clear_on_submit=True): | |
| brand = st.text_input("Cantina") | |
| wine = st.text_input("Nome Vino") | |
| c1, c2, c3 = st.columns(3) | |
| year = c1.number_input("Annata", value=2024, step=1) | |
| alc = c2.number_input("Alcol %", value=13.0, step=0.1) | |
| kcal = c3.number_input("Kcal", value=80.0) | |
| is_deal = st.checkbox("Prodotto Dealcolizzato (Reg. 2026)") | |
| ingredients = st.text_area("Ingredienti") | |
| st.write("**Smaltimento**") | |
| r1, r2, r3 = st.columns(3) | |
| b_mat = r1.selectbox("Bottiglia", ["Vetro (GL 71)", "Vetro (GL 70)"]) | |
| c_mat = r2.selectbox("Tappo", ["Sughero (FOR 51)", "Plastica (LDPE 4)"]) | |
| k_mat = r3.selectbox("Capsula", ["Alluminio (C/ALU 90)", "Stagno", "Senza capsula"]) | |
| if st.form_submit_button("πΎ SALVA VINO"): | |
| dati = { | |
| "brand_name": brand, "wine_name": wine, "vintage": float(year), | |
| "alcohol_vol": float(alc), "energy_kcal": float(kcal), "energy_kj": float(kcal)*4.184, | |
| "is_dealcoholized": bool(is_deal), "ingredients": ingredients, | |
| "bottle_material": b_mat, "cork_material": c_mat, "capsule_material": k_mat, | |
| "carbs_sugar": 0.5, "salt": 0.01 | |
| } | |
| supabase.table("wine_labels").insert(dati).execute() | |
| st.success("β Vino salvato!") | |
| elif azione == "π Gestione Lista": | |
| res = supabase.table("wine_labels").select("*").order("created_at", desc=True).execute() | |
| if res.data: | |
| vini = {f"{v['brand_name']} - {v['wine_name']}": v for v in res.data} | |
| scelta = st.selectbox("Seleziona vino:", ["-- Scegli --"] + list(vini.keys())) | |
| if scelta != "-- Scegli --": | |
| v = vini[scelta] | |
| url = f"{VIEWER_URL}?id={v['id']}" | |
| t1, t2, t3 = st.tabs(["πΌοΈ QR Code", "βοΈ Modifica", "ποΈ Elimina"]) | |
| with t1: | |
| qr = get_qr_bytes(url) | |
| st.image(qr, width=200) | |
| st.download_button("π₯ Scarica QR", qr, file_name=f"QR_{v['wine_name']}.png") | |
| st.code(url) | |
| with t2: | |
| with st.form("edit_form"): | |
| e_brand = st.text_input("Cantina", value=v['brand_name']) | |
| e_wine = st.text_input("Vino", value=v['wine_name']) | |
| e_ing = st.text_area("Ingredienti", value=v['ingredients']) | |
| if st.form_submit_button("πΎ AGGIORNA"): | |
| supabase.table("wine_labels").update({"brand_name": e_brand, "wine_name": e_wine, "ingredients": e_ing}).eq("id", v['id']).execute() | |
| st.rerun() | |
| with t3: | |
| if st.button("π΄ ELIMINA DEFINITIVAMENTE"): | |
| supabase.table("wine_labels").delete().eq("id", v['id']).execute() | |
| st.rerun() |