bluenevus commited on
Commit
565fcd0
·
verified ·
1 Parent(s): cb5f761

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -245,31 +245,50 @@ app.layout = dbc.Container([
245
  ], fluid=True)
246
 
247
  @app.callback(
248
- Output("pdf-store", "data"),
249
- Output("status-message", "children"),
250
  Output("submit-button", "disabled"),
 
 
 
 
251
  Input("submit-button", "n_clicks"),
 
252
  State("url-input", "value"),
253
  State("depth-slider", "value"),
 
254
  prevent_initial_call=True
255
  )
256
- def generate_pdf(n_clicks, url, depth):
257
- if not url:
258
- return None, "Please enter a URL", False
259
-
260
- try:
261
- pdf_content = asyncio.run(process_url(url, depth, lambda x: None))
262
- return pdf_content, "PDF generated successfully. Ready for download!", False
263
- except Exception as e:
264
- logger.error(f"Error generating PDF: {str(e)}")
265
- return None, f"Error: {str(e)}", False
266
 
267
- @app.callback(
268
- Output("download-button", "disabled"),
269
- Input("pdf-store", "data")
270
- )
271
- def update_download_button(pdf_data):
272
- return pdf_data is None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
 
274
  @app.callback(
275
  Output("download-pdf", "data"),
@@ -280,7 +299,8 @@ def update_download_button(pdf_data):
280
  def download_pdf(n_clicks, pdf_data):
281
  if pdf_data is None:
282
  raise PreventUpdate
283
- return dcc.send_bytes(pdf_data, f"website_content_{int(time.time())}.pdf")
 
284
 
285
  def background_task(url, depth, task_id):
286
  global generated_file
 
245
  ], fluid=True)
246
 
247
  @app.callback(
 
 
248
  Output("submit-button", "disabled"),
249
+ Output("download-button", "disabled"),
250
+ Output("progress-bar", "value"),
251
+ Output("progress-message", "children"),
252
+ Output("pdf-store", "data"),
253
  Input("submit-button", "n_clicks"),
254
+ Input("progress-interval", "n_intervals"),
255
  State("url-input", "value"),
256
  State("depth-slider", "value"),
257
+ State("progress-store", "data"),
258
  prevent_initial_call=True
259
  )
260
+ def update_progress(submit_clicks, n_intervals, url, depth, progress_data):
261
+ global generated_file
262
+ ctx = dash.callback_context
263
+ if not ctx.triggered:
264
+ raise PreventUpdate
 
 
 
 
 
265
 
266
+ triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
267
+
268
+ if triggered_id == "submit-button":
269
+ if not url:
270
+ return False, True, 0, "Please enter a URL", None
271
+
272
+ # Start the background task
273
+ task_id = str(uuid.uuid4())
274
+ executor.submit(background_task, url, depth, task_id)
275
+
276
+ return True, True, 0, "Processing... Please wait.", None
277
+
278
+ elif triggered_id == "progress-interval":
279
+ progress = progress_data['progress']
280
+ message = progress_data['message']
281
+
282
+ if isinstance(message, str) and message.startswith("Error"):
283
+ return False, True, 100, message, None
284
+ elif progress == 100 and generated_file:
285
+ # Encode the PDF file as base64
286
+ encoded_pdf = base64.b64encode(generated_file).decode('utf-8')
287
+ return False, False, 100, "PDF ready for download!", {'content': encoded_pdf, 'filename': f"website_content_{int(time.time())}.pdf"}
288
+ elif progress > 0:
289
+ return True, True, progress, message, None
290
+
291
+ return False, True, 0, "", None
292
 
293
  @app.callback(
294
  Output("download-pdf", "data"),
 
299
  def download_pdf(n_clicks, pdf_data):
300
  if pdf_data is None:
301
  raise PreventUpdate
302
+
303
+ return dcc.send_bytes(base64.b64decode(pdf_data['content']), pdf_data['filename'])
304
 
305
  def background_task(url, depth, task_id):
306
  global generated_file