Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +129 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,131 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Funzione di conversione
|
| 9 |
+
def convert_json_to_excel_data(json_file_content: str, sheet_name: str = 'Dati Ordine'):
|
| 10 |
+
try:
|
| 11 |
+
json_data = json.loads(json_file_content)
|
| 12 |
+
|
| 13 |
+
channel = json_data.get('channel')
|
| 14 |
+
company_code = json_data.get('companyCode')
|
| 15 |
+
site_code = json_data.get('siteCode')
|
| 16 |
+
|
| 17 |
+
order_info = json_data.get('order', {})
|
| 18 |
+
order_number = order_info.get('orderNumber')
|
| 19 |
+
order_date_str = order_info.get('orderDate')
|
| 20 |
+
partner_code = order_info.get('partnerCode')
|
| 21 |
+
|
| 22 |
+
order_date_formatted = None
|
| 23 |
+
if order_date_str:
|
| 24 |
+
try:
|
| 25 |
+
dt_object = datetime.fromisoformat(order_date_str.replace('Z', '+00:00'))
|
| 26 |
+
order_date_formatted = dt_object.strftime('%d/%m/%Y')
|
| 27 |
+
except ValueError:
|
| 28 |
+
st.warning(f"Attenzione: Impossibile parsare la data '{order_date_str}'. Verrà usata la stringa originale.")
|
| 29 |
+
order_date_formatted = order_date_str
|
| 30 |
+
|
| 31 |
+
details = order_info.get('details', [])
|
| 32 |
+
|
| 33 |
+
processed_data = []
|
| 34 |
+
for item in details:
|
| 35 |
+
row = {
|
| 36 |
+
'Site': site_code,
|
| 37 |
+
'Data': order_date_formatted,
|
| 38 |
+
'Company': company_code,
|
| 39 |
+
'Furniture': partner_code,
|
| 40 |
+
'Stato': 'CREATED',
|
| 41 |
+
'Posizione': item.get('positionId'),
|
| 42 |
+
'Stato. Articolo': None,
|
| 43 |
+
'Codice Articolo': item.get('itemCode'),
|
| 44 |
+
'Quantità': item.get('quantity'),
|
| 45 |
+
'Proprietario': item.get('stockOwner'),
|
| 46 |
+
'Centro di Costo': item.get('warehouse'),
|
| 47 |
+
'Disponibilità': item.get('availability'),
|
| 48 |
+
'RefOrder': item.get('refOrder'),
|
| 49 |
+
'ReasonCO': item.get('reasonCode'),
|
| 50 |
+
'CancelPosOdA': False if item.get('cancelPosOdA') is None else bool(item.get('cancelPosOdA'))
|
| 51 |
+
}
|
| 52 |
+
processed_data.append(row)
|
| 53 |
+
|
| 54 |
+
if not processed_data:
|
| 55 |
+
st.error("Nessun dato di dettaglio trovato per la conversione. Assicurati che il JSON contenga 'order.details'.")
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
df = pd.DataFrame(processed_data)
|
| 59 |
+
return df
|
| 60 |
+
|
| 61 |
+
except json.JSONDecodeError:
|
| 62 |
+
st.error(f"Errore: Il contenuto del file non è un JSON valido.")
|
| 63 |
+
return None
|
| 64 |
+
except KeyError as ke:
|
| 65 |
+
st.error(f"Errore: Chiave JSON mancante o struttura inattesa: {ke}. Assicurati che il JSON abbia la struttura attesa (es. 'order.details').")
|
| 66 |
+
return None
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"Si è verificato un errore durante la conversione: {e}")
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
# --- Interfaccia Streamlit ---
|
| 72 |
+
st.title("Convertitore JSON a Excel Ordini")
|
| 73 |
+
st.write("Carica il tuo file JSON degli ordini e scarica il foglio Excel formattato.")
|
| 74 |
+
|
| 75 |
+
uploaded_file = st.file_uploader("Scegli un file JSON", type=["json"])
|
| 76 |
+
|
| 77 |
+
if uploaded_file is not None:
|
| 78 |
+
file_name = uploaded_file.name
|
| 79 |
+
_, file_extension = os.path.splitext(file_name)
|
| 80 |
+
|
| 81 |
+
if file_extension.lower() != '.json':
|
| 82 |
+
st.error("Per favore, carica un file con estensione .json.")
|
| 83 |
+
else:
|
| 84 |
+
json_content_bytes = uploaded_file.read()
|
| 85 |
+
json_content_string = json_content_bytes.decode('utf-8')
|
| 86 |
+
|
| 87 |
+
st.info(f"File '{file_name}' caricato. Tentativo di conversione...")
|
| 88 |
+
|
| 89 |
+
df_output = convert_json_to_excel_data(json_content_string)
|
| 90 |
+
|
| 91 |
+
if df_output is not None:
|
| 92 |
+
st.success("Conversione completata!")
|
| 93 |
+
|
| 94 |
+
output = io.BytesIO()
|
| 95 |
+
writer = pd.ExcelWriter(output, engine='xlsxwriter')
|
| 96 |
+
df_output.to_excel(writer, sheet_name='Dati Ordine', index=False)
|
| 97 |
+
|
| 98 |
+
workbook = writer.book
|
| 99 |
+
worksheet = writer.sheets['Dati Ordine']
|
| 100 |
+
|
| 101 |
+
header_format = workbook.add_format({
|
| 102 |
+
'bold': True, 'font_color': '#FFFFFF', 'bg_color': '#4F81BD',
|
| 103 |
+
'align': 'center', 'valign': 'vcenter', 'border': 1
|
| 104 |
+
})
|
| 105 |
+
|
| 106 |
+
col_widths = {
|
| 107 |
+
'Site': 10, 'Data': 12, 'Company': 15, 'Furniture': 15, 'Stato': 10,
|
| 108 |
+
'Posizione': 10, 'Stato. Articolo': 15, 'Codice Articolo': 25,
|
| 109 |
+
'Quantità': 10, 'Proprietario': 15, 'Centro di Costo': 15,
|
| 110 |
+
'Disponibilità': 15, 'RefOrder': 15, 'ReasonCO': 10, 'CancelPosOdA': 15
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
for col_num, col_name in enumerate(df_output.columns.values):
|
| 114 |
+
worksheet.write(0, col_num, col_name, header_format)
|
| 115 |
+
if col_name in col_widths:
|
| 116 |
+
worksheet.set_column(col_num, col_num, col_widths[col_name])
|
| 117 |
+
else:
|
| 118 |
+
worksheet.set_column(col_num, col_num, 15)
|
| 119 |
+
|
| 120 |
+
writer.close()
|
| 121 |
+
output.seek(0)
|
| 122 |
+
|
| 123 |
+
st.download_button(
|
| 124 |
+
label="Scarica file Excel",
|
| 125 |
+
data=output.getvalue(),
|
| 126 |
+
file_name="ordine_convertito.xlsx",
|
| 127 |
+
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 128 |
+
)
|
| 129 |
|
| 130 |
+
st.markdown("---")
|
| 131 |
+
st.markdown("Sviluppato con Streamlit e Python.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|