bluenevus commited on
Commit
c528af3
·
1 Parent(s): c7006a6

Update app.py via AI Editor

Browse files
Files changed (1) hide show
  1. app.py +56 -32
app.py CHANGED
@@ -126,22 +126,27 @@ def make_shred_doc_preview():
126
  return dbc.Card(
127
  dbc.CardBody([
128
  html.Div(id="shred-upload-preview", style={"whiteSpace": "pre-wrap", "overflowWrap": "break-word"}),
129
- dbc.Button("Delete Document", id="shred-delete-btn", className="mt-2 btn-tertiary", n_clicks=0)
130
  ]), className="mb-2", id="shred-doc-preview-card", style={"display": "none"}
131
  )
132
 
 
 
 
 
 
 
 
 
133
  def make_tab(tab_id, label):
134
  if tab_id == "shred":
135
- # Insert doc preview card for shred
136
  return dbc.Card(
137
  dbc.CardBody([
138
  make_textarea(tab_id, f"Instructions for {label} (optional)"),
139
  make_upload(tab_id),
140
  make_shred_doc_preview(),
141
- dbc.Button(f"Generate {label}", id=f'{tab_id}-btn', className="mt-2 btn-primary", n_clicks=0),
142
  dcc.Loading(html.Div(id=f'{tab_id}-output'), id="loading", type="default", parent_style={'justifyContent': 'center'}),
143
- dbc.Button(f"Download {label} Report", id=f"{tab_id}-download-btn", className="mt-2 btn-secondary", n_clicks=0),
144
- dcc.Download(id=f"{tab_id}-download"),
145
  dcc.Store(id="shred-upload-store")
146
  ]), className="mb-4"
147
  )
@@ -256,7 +261,8 @@ def update_shred_upload(contents, delete_clicks, filename, stored_data):
256
  [Output(f'{tab_id}-output', 'children') for tab_id in tab_cards] +
257
  [Output(f"{tab_id}-download", "data") for tab_id in tab_cards],
258
  [Input(f'{tab_id}-btn', 'n_clicks') for tab_id in tab_cards] +
259
- [Input(f"{tab_id}-download-btn", "n_clicks") for tab_id in tab_cards],
 
260
  [State(f'{tab_id}-upload', 'contents') for tab_id in tab_cards] +
261
  [State(f'{tab_id}-upload', 'filename') for tab_id in tab_cards] +
262
  [State(f'{tab_id}-instructions', 'value') for tab_id in tab_cards] +
@@ -271,6 +277,7 @@ def handle_all_tabs(*args):
271
  return outputs
272
  trig = ctx.triggered[0]['prop_id']
273
  logger.info(f"Main callback triggered by {trig}")
 
274
  for idx, tab_id in enumerate(tab_cards):
275
  gen_btn = f"{tab_id}-btn.n_clicks"
276
  dl_btn = f"{tab_id}-download-btn.n_clicks"
@@ -282,10 +289,12 @@ def handle_all_tabs(*args):
282
  prev_output_idx = idx + 3 * n
283
  shred_upload_store_idx = 4 * n
284
 
285
- if trig == gen_btn:
286
- logger.info(f"Generate button pressed for {tab_id}")
287
- if tab_id == "shred":
288
- # Use stored doc for Shred
 
 
289
  shred_data = args[shred_upload_store_idx]
290
  instr = args[instr_idx] or ""
291
  if shred_data and "contents" in shred_data and "filename" in shred_data:
@@ -307,30 +316,45 @@ def handle_all_tabs(*args):
307
  ])
308
  else:
309
  outputs[out_idx] = "Please upload a document to begin."
310
- else:
311
- upload = args[upload_idx]
312
- filename = args[filename_idx]
313
- instr = args[instr_idx] or ""
314
- doc_type = tab_id.replace('-', ' ').title().replace(' ', '')
315
- doc_type = next((k for k in document_types if k.lower().replace(' ', '') == tab_id.replace('-', '')), tab_id.title())
316
- if upload and filename:
317
- doc = process_document(upload, filename)
 
 
 
318
  else:
319
- doc = ""
320
- if doc or tab_id == "virtual-board":
321
- content, df = generate_content(doc, doc_type, instr)
322
- if not df.empty:
323
- outputs[out_idx] = dash_table.DataTable(
324
- data=df.to_dict('records'),
325
- columns=[{'name': i, 'id': i} for i in df.columns],
326
- style_table={'overflowX': 'auto'},
327
- style_cell={'textAlign': 'left', 'padding': '5px'},
328
- style_header={'fontWeight': 'bold'}
329
- )
330
- else:
331
- outputs[out_idx] = dcc.Markdown(content)
 
 
 
 
 
 
 
 
 
 
332
  else:
333
- outputs[out_idx] = "Please upload a document to begin."
 
 
334
  elif trig == dl_btn:
335
  prev_output = args[prev_output_idx]
336
  if prev_output and hasattr(prev_output, 'props') and 'data' in prev_output.props:
 
126
  return dbc.Card(
127
  dbc.CardBody([
128
  html.Div(id="shred-upload-preview", style={"whiteSpace": "pre-wrap", "overflowWrap": "break-word"}),
 
129
  ]), className="mb-2", id="shred-doc-preview-card", style={"display": "none"}
130
  )
131
 
132
+ def make_shred_controls():
133
+ return html.Div([
134
+ dbc.Button("Delete Document", id="shred-delete-btn", className="mt-2 btn-tertiary", n_clicks=0, style={'marginRight': '8px'}),
135
+ dbc.Button("Generate Shred", id='shred-btn', className="mt-2 btn-primary", n_clicks=0, style={'marginRight': '8px'}),
136
+ dbc.Button("Download Shred Report", id="shred-download-btn", className="mt-2 btn-secondary", n_clicks=0),
137
+ dcc.Download(id="shred-download"),
138
+ ], style={'display': 'flex', 'flexWrap': 'wrap', 'gap': '8px', 'marginTop': '10px', 'marginBottom': '10px'})
139
+
140
  def make_tab(tab_id, label):
141
  if tab_id == "shred":
142
+ # Upload first, then preview, then control buttons below as requested
143
  return dbc.Card(
144
  dbc.CardBody([
145
  make_textarea(tab_id, f"Instructions for {label} (optional)"),
146
  make_upload(tab_id),
147
  make_shred_doc_preview(),
148
+ make_shred_controls(),
149
  dcc.Loading(html.Div(id=f'{tab_id}-output'), id="loading", type="default", parent_style={'justifyContent': 'center'}),
 
 
150
  dcc.Store(id="shred-upload-store")
151
  ]), className="mb-4"
152
  )
 
261
  [Output(f'{tab_id}-output', 'children') for tab_id in tab_cards] +
262
  [Output(f"{tab_id}-download", "data") for tab_id in tab_cards],
263
  [Input(f'{tab_id}-btn', 'n_clicks') for tab_id in tab_cards] +
264
+ [Input(f"{tab_id}-download-btn", "n_clicks") for tab_id in tab_cards] +
265
+ [Input("shred-btn", "n_clicks"), Input("shred-download-btn", "n_clicks")],
266
  [State(f'{tab_id}-upload', 'contents') for tab_id in tab_cards] +
267
  [State(f'{tab_id}-upload', 'filename') for tab_id in tab_cards] +
268
  [State(f'{tab_id}-instructions', 'value') for tab_id in tab_cards] +
 
277
  return outputs
278
  trig = ctx.triggered[0]['prop_id']
279
  logger.info(f"Main callback triggered by {trig}")
280
+ # Index mapping because Shred tab has custom button IDs
281
  for idx, tab_id in enumerate(tab_cards):
282
  gen_btn = f"{tab_id}-btn.n_clicks"
283
  dl_btn = f"{tab_id}-download-btn.n_clicks"
 
289
  prev_output_idx = idx + 3 * n
290
  shred_upload_store_idx = 4 * n
291
 
292
+ # For shred, support the special button IDs
293
+ if tab_id == "shred":
294
+ shred_gen_btn = "shred-btn.n_clicks"
295
+ shred_dl_btn = "shred-download-btn.n_clicks"
296
+ if trig == shred_gen_btn:
297
+ logger.info(f"Generate Shred button pressed for {tab_id}")
298
  shred_data = args[shred_upload_store_idx]
299
  instr = args[instr_idx] or ""
300
  if shred_data and "contents" in shred_data and "filename" in shred_data:
 
316
  ])
317
  else:
318
  outputs[out_idx] = "Please upload a document to begin."
319
+ elif trig == shred_dl_btn:
320
+ prev_output = args[prev_output_idx]
321
+ shred_data = args[shred_upload_store_idx]
322
+ if prev_output and hasattr(prev_output, 'props') and 'data' in prev_output.props:
323
+ df = pd.DataFrame(prev_output.props['data'])
324
+ buffer = BytesIO()
325
+ df.to_csv(buffer, index=False)
326
+ outputs[dl_idx] = dcc.send_bytes(buffer.getvalue(), f"{tab_id}_report.csv")
327
+ elif prev_output:
328
+ buffer = BytesIO(prev_output.encode("utf-8") if isinstance(prev_output, str) else b"")
329
+ outputs[dl_idx] = dcc.send_bytes(buffer.getvalue(), f"{tab_id}_report.txt")
330
  else:
331
+ outputs[dl_idx] = None
332
+ # All other tabs
333
+ if trig == gen_btn:
334
+ logger.info(f"Generate button pressed for {tab_id}")
335
+ upload = args[upload_idx]
336
+ filename = args[filename_idx]
337
+ instr = args[instr_idx] or ""
338
+ doc_type = tab_id.replace('-', ' ').title().replace(' ', '')
339
+ doc_type = next((k for k in document_types if k.lower().replace(' ', '') == tab_id.replace('-', '')), tab_id.title())
340
+ if upload and filename:
341
+ doc = process_document(upload, filename)
342
+ else:
343
+ doc = ""
344
+ if doc or tab_id == "virtual-board":
345
+ content, df = generate_content(doc, doc_type, instr)
346
+ if not df.empty:
347
+ outputs[out_idx] = dash_table.DataTable(
348
+ data=df.to_dict('records'),
349
+ columns=[{'name': i, 'id': i} for i in df.columns],
350
+ style_table={'overflowX': 'auto'},
351
+ style_cell={'textAlign': 'left', 'padding': '5px'},
352
+ style_header={'fontWeight': 'bold'}
353
+ )
354
  else:
355
+ outputs[out_idx] = dcc.Markdown(content)
356
+ else:
357
+ outputs[out_idx] = "Please upload a document to begin."
358
  elif trig == dl_btn:
359
  prev_output = args[prev_output_idx]
360
  if prev_output and hasattr(prev_output, 'props') and 'data' in prev_output.props: