Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,85 +5,102 @@ import altair as alt
|
|
| 5 |
st.set_page_config(page_title="Shipment Monitoring Dashboard", layout="wide")
|
| 6 |
st.title("Shipment Monitoring Dashboard")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
st.set_page_config(page_title="Shipment Monitoring Dashboard", layout="wide")
|
| 6 |
st.title("Shipment Monitoring Dashboard")
|
| 7 |
|
| 8 |
+
tabs = st.tabs(["Main Dashboard", "Last Mile Summary"])
|
| 9 |
+
|
| 10 |
+
with tabs[0]:
|
| 11 |
+
uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx"], key="main_upload")
|
| 12 |
+
|
| 13 |
+
if uploaded_file:
|
| 14 |
+
df = pd.read_excel(uploaded_file, sheet_name=0)
|
| 15 |
+
|
| 16 |
+
# Rename columns
|
| 17 |
+
df.columns = ['MAWB', 'MAWB Serial Number', 'Injection Gateway', 'Clearance Port', 'Latest Status',
|
| 18 |
+
'Cartons', 'Packages', 'Weights', 'Not online packages count', 'ETD', 'ETA', 'ATD', 'ATA',
|
| 19 |
+
'Last Mile Carrier', 'Readynotice has been notified', 'Vehicle has been arranged',
|
| 20 |
+
'Pick-upcompletedfrom', 'WH-IN Scan', 'Cargo Ready', 'Ship Out', 'Note',
|
| 21 |
+
'Exception Note', 'Exception Note Description']
|
| 22 |
+
|
| 23 |
+
# Add status column
|
| 24 |
+
def determine_status(row):
|
| 25 |
+
if pd.isna(row['WH-IN Scan']):
|
| 26 |
+
return "Pending"
|
| 27 |
+
elif pd.isna(row['Cargo Ready']):
|
| 28 |
+
return "WH-IN"
|
| 29 |
+
elif pd.isna(row['Ship Out']):
|
| 30 |
+
return "Cargo Ready"
|
| 31 |
+
else:
|
| 32 |
+
return "Ship Out"
|
| 33 |
+
|
| 34 |
+
df['Shipment Status'] = df.apply(determine_status, axis=1)
|
| 35 |
+
df['ETA'] = pd.to_datetime(df['ETA'])
|
| 36 |
+
|
| 37 |
+
# Filter for Injection Gateway
|
| 38 |
+
injection_gateways = df['Injection Gateway'].dropna().unique().tolist()
|
| 39 |
+
selected_gateway = st.selectbox("Filter by Injection Gateway", options=["All"] + injection_gateways)
|
| 40 |
+
if selected_gateway != "All":
|
| 41 |
+
df = df[df['Injection Gateway'] == selected_gateway]
|
| 42 |
+
|
| 43 |
+
# Filter by ETA date range
|
| 44 |
+
st.subheader("Filter by ETA Date Range")
|
| 45 |
+
min_date, max_date = df['ETA'].min(), df['ETA'].max()
|
| 46 |
+
start_date, end_date = st.date_input("Select ETA date range:", [min_date, max_date], min_value=min_date, max_value=max_date)
|
| 47 |
+
df = df[(df['ETA'] >= pd.to_datetime(start_date)) & (df['ETA'] <= pd.to_datetime(end_date))]
|
| 48 |
+
|
| 49 |
+
# Shipment status distribution with interactive selection
|
| 50 |
+
st.subheader("Shipment Status Distribution")
|
| 51 |
+
status_order = ["Pending", "WH-IN", "Cargo Ready", "Ship Out"]
|
| 52 |
+
status_counts = df['Shipment Status'].value_counts().reindex(status_order).reset_index()
|
| 53 |
+
status_counts.columns = ['Shipment Status', 'Count']
|
| 54 |
+
status_counts = status_counts.dropna()
|
| 55 |
+
|
| 56 |
+
# Altair selection for interactivity
|
| 57 |
+
selection = alt.selection_single(fields=['Shipment Status'], bind='legend', name='Select', init={'Shipment Status': 'Pending'})
|
| 58 |
+
|
| 59 |
+
chart = alt.Chart(status_counts).mark_bar().encode(
|
| 60 |
+
x=alt.X('Shipment Status', sort=status_order),
|
| 61 |
+
y='Count',
|
| 62 |
+
color='Shipment Status',
|
| 63 |
+
tooltip=['Shipment Status', 'Count'],
|
| 64 |
+
opacity=alt.condition(selection, alt.value(1), alt.value(0.4))
|
| 65 |
+
).add_params(selection).properties(height=300)
|
| 66 |
+
|
| 67 |
+
st.altair_chart(chart, use_container_width=True)
|
| 68 |
+
|
| 69 |
+
selected_status = selection.init['Shipment Status'] if selection else "Pending"
|
| 70 |
+
|
| 71 |
+
# Volume by ETA
|
| 72 |
+
st.subheader("Shipment Volume by ETA")
|
| 73 |
+
volume_by_eta = df.groupby('ETA').agg({
|
| 74 |
+
'MAWB': 'count',
|
| 75 |
+
'Cartons': 'sum',
|
| 76 |
+
}).reset_index().rename(columns={'MAWB': 'Shipment Count'})
|
| 77 |
+
|
| 78 |
+
st.dataframe(volume_by_eta, use_container_width=True)
|
| 79 |
+
|
| 80 |
+
line_chart = alt.Chart(volume_by_eta).mark_line(point=True).encode(
|
| 81 |
+
x='ETA:T',
|
| 82 |
+
y='Cartons:Q',
|
| 83 |
+
tooltip=['ETA:T', 'Shipment Count:Q', 'Cartons:Q']
|
| 84 |
+
).properties(height=400)
|
| 85 |
+
|
| 86 |
+
st.altair_chart(line_chart, use_container_width=True)
|
| 87 |
+
|
| 88 |
+
# Table view sorted by ETA and filtered by selected status
|
| 89 |
+
st.subheader("Shipment Detail View by ETA")
|
| 90 |
+
filtered_df = df[df['Shipment Status'] == selected_status]
|
| 91 |
+
sorted_df = filtered_df[['MAWB', 'ETA', 'ATA', 'Shipment Status', 'Cartons', 'Last Mile Carrier']].sort_values(by='ETA')
|
| 92 |
+
st.dataframe(sorted_df, use_container_width=True)
|
| 93 |
+
|
| 94 |
+
with tabs[1]:
|
| 95 |
+
st.header("Upload CTN File for Last Mile Summary")
|
| 96 |
+
ctn_file = st.file_uploader("Upload Excel with Last Mile Service", type=["xlsx"], key="ctn_upload")
|
| 97 |
+
|
| 98 |
+
if ctn_file:
|
| 99 |
+
df_ctn = pd.read_excel(ctn_file, sheet_name=0)
|
| 100 |
+
|
| 101 |
+
# Try to match the correct column
|
| 102 |
+
last_mile_col = [col for col in df_ctn.columns if "last mile" in col.lower()][0]
|
| 103 |
+
|
| 104 |
+
# Group by Last Mile Service and count cartons
|
| 105 |
+
grouped = df_ctn.groupby(last_mile_col).agg({"Cartons": "sum"}).reset_index()
|
| 106 |
+
st.dataframe(grouped, use_container_width=True)
|