51 lines
No EOL
1.6 KiB
Python
51 lines
No EOL
1.6 KiB
Python
import streamlit as st
|
|
import json
|
|
import uuid
|
|
|
|
from functions import generate_pdf_download
|
|
|
|
def open_csv_file(file_path):
|
|
"""Apre un file CSV e restituisce i dati come dataframe."""
|
|
import duckdb
|
|
|
|
# Usa DuckDB per leggere il file CSV
|
|
con = duckdb.connect(database=':memory:')
|
|
query = f"CREATE TABLE index AS SELECT * FROM read_csv_auto('{file_path}')"
|
|
con.execute(query)
|
|
return con
|
|
|
|
def search_cas_inci(input, type = 'cas'):
|
|
"""Cerca un numero CAS nei dati forniti e restituisce CAS e INCI."""
|
|
con = open_csv_file('data.csv')
|
|
if type == 'cas':
|
|
query = f"SELECT * FROM index WHERE casNo LIKE '%{input}%'"
|
|
else:
|
|
query = f"SELECT * FROM index WHERE inciName ILIKE '%{input}%'"
|
|
results = con.execute(query).fetchdf()
|
|
|
|
# Restituisce una lista di tuple (casNo, inciName)
|
|
if not results.empty:
|
|
return list(zip(results['casNo'].tolist(), results['inciName'].tolist()))
|
|
return []
|
|
|
|
def download_pdf(casNo, origin, link):
|
|
"""Scarica un PDF generato dall'API."""
|
|
if st.button("Generate PDF", key= f"gen_{casNo}_{origin}"):
|
|
with st.spinner("Fetching PDF..."):
|
|
pdf_data = generate_pdf_download(
|
|
cas=casNo,
|
|
origin=origin,
|
|
link=link
|
|
)
|
|
|
|
st.download_button(
|
|
label="Download PDF",
|
|
data=pdf_data,
|
|
file_name=f"{casNo}_{origin}.pdf",
|
|
mime="application/pdf",
|
|
key=f"dl_{casNo}_{origin}"
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
data = search_cas_inci('102242-62-6')
|
|
print(data) |