import streamlit as st import os import tempfile import subprocess EXAMPLES_DIR = "examples" PREVIEWS_DIR = "previews" os.makedirs(PREVIEWS_DIR, exist_ok=True) st.title("KiCad Schematic Preview") st.write("Seleziona un file schematico dalla lista di esempi o carica il tuo file.") # Lista file esempi example_files = [f for f in os.listdir(EXAMPLES_DIR) if f.endswith((".kicad_sch", ".sch"))] selected_example = st.selectbox("File di esempio", ["-- Nessuno --"] + example_files) uploaded_file = st.file_uploader("Oppure carica un file schematico", type=["kicad_sch", "sch"]) def generate_preview(input_path, output_path): try: subprocess.run([ "kicad-cli", "sch", "export", "png", input_path, "--output", output_path, "--width", "800", "--height", "600" ], check=True) return True except Exception as e: st.error(f"Errore nella generazione anteprima: {e}") return False def show_preview(file_path, file_name): preview_path = os.path.join(PREVIEWS_DIR, file_name + ".png") if not os.path.exists(preview_path): success = generate_preview(file_path, preview_path) if not success: return st.image(preview_path, caption=f"Anteprima di {file_name}") st.download_button("Scarica file originale", file_path, file_name) # Gestione file di esempio selezionato if selected_example != "-- Nessuno --": example_path = os.path.join(EXAMPLES_DIR, selected_example) show_preview(example_path, selected_example) # Gestione file caricato if uploaded_file is not None: with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file: tmp_file.write(uploaded_file.read()) tmp_filepath = tmp_file.name show_preview(tmp_filepath, uploaded_file.name)