Ankushbl6 commited on
Commit
49486db
·
verified ·
1 Parent(s): 6411f8b

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +29 -19
src/streamlit_app.py CHANGED
@@ -1214,25 +1214,32 @@ elif len(st.session_state.batch_results) > 0:
1214
  st.text_input("Branch", key=f"Bank_bank_branch_{selected_hash}")
1215
 
1216
  with tabs[3]:
1217
- # Build base DF from current edited_data (not raw mapped) so it's always what the user last saved
1218
- item_rows = form_data.get('Itemized Data', []) or []
1219
- normalized = []
1220
- for it in item_rows:
1221
- if not isinstance(it, dict):
1222
- it = {}
1223
- normalized.append({
1224
- "Description": it.get("Description", it.get("Item Description", "")),
1225
- "Quantity": it.get("Quantity", it.get("Item Quantity", 0)),
1226
- "Unit Price": it.get("Unit Price", it.get("Item Unit Price", 0.0)),
1227
- "Amount": it.get("Amount", it.get("Item Amount", 0.0)),
1228
- "IO Number/Cost Centre": it.get("IO Number/Cost Centre", ""), # New column
1229
- "Tax": it.get("Tax", it.get("Item Tax", 0.0)),
1230
- "Line Total": it.get("Line Total", it.get("Item Line Total", 0.0)),
1231
- })
1232
-
1233
- items_df = pd.DataFrame(normalized) if normalized else pd.DataFrame(
1234
- columns=["Description", "Quantity", "Unit Price", "Amount", "IO Number/Cost Centre", "Tax", "Line Total"]
1235
- )
 
 
 
 
 
 
 
1236
 
1237
  # Configure column widths to make Description wider and avoid horizontal scrolling
1238
  column_config = {
@@ -1279,6 +1286,9 @@ elif len(st.session_state.batch_results) > 0:
1279
  height=DATA_EDITOR_HEIGHT - 50, # Reduce height slightly for totals below
1280
  column_config=column_config,
1281
  )
 
 
 
1282
 
1283
  # Display non-editable totals row immediately below (looks integrated)
1284
  if len(edited_df) > 0:
 
1214
  st.text_input("Branch", key=f"Bank_bank_branch_{selected_hash}")
1215
 
1216
  with tabs[3]:
1217
+ # Initialize line items DataFrame in session state ONLY ONCE (preserves edits)
1218
+ items_state_key = f"items_df_{selected_hash}"
1219
+
1220
+ if items_state_key not in st.session_state:
1221
+ # Build base DF from current edited_data only on first load
1222
+ item_rows = form_data.get('Itemized Data', []) or []
1223
+ normalized = []
1224
+ for it in item_rows:
1225
+ if not isinstance(it, dict):
1226
+ it = {}
1227
+ normalized.append({
1228
+ "Description": it.get("Description", it.get("Item Description", "")),
1229
+ "Quantity": it.get("Quantity", it.get("Item Quantity", 0)),
1230
+ "Unit Price": it.get("Unit Price", it.get("Item Unit Price", 0.0)),
1231
+ "Amount": it.get("Amount", it.get("Item Amount", 0.0)),
1232
+ "IO Number/Cost Centre": it.get("IO Number/Cost Centre", ""), # New column
1233
+ "Tax": it.get("Tax", it.get("Item Tax", 0.0)),
1234
+ "Line Total": it.get("Line Total", it.get("Item Line Total", 0.0)),
1235
+ })
1236
+
1237
+ st.session_state[items_state_key] = pd.DataFrame(normalized) if normalized else pd.DataFrame(
1238
+ columns=["Description", "Quantity", "Unit Price", "Amount", "IO Number/Cost Centre", "Tax", "Line Total"]
1239
+ )
1240
+
1241
+ # Use the stored DataFrame
1242
+ items_df = st.session_state[items_state_key]
1243
 
1244
  # Configure column widths to make Description wider and avoid horizontal scrolling
1245
  column_config = {
 
1286
  height=DATA_EDITOR_HEIGHT - 50, # Reduce height slightly for totals below
1287
  column_config=column_config,
1288
  )
1289
+
1290
+ # Update session state with edited DataFrame (preserves changes across reruns)
1291
+ st.session_state[items_state_key] = edited_df
1292
 
1293
  # Display non-editable totals row immediately below (looks integrated)
1294
  if len(edited_df) > 0: