bluenevus commited on
Commit
183dbc9
·
verified ·
1 Parent(s): 88383bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -57
app.py CHANGED
@@ -29,8 +29,7 @@ app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
29
  server = app.server
30
 
31
  # Logging setup
32
- logging.basicConfig(filename='app.log', level=logging.INFO,
33
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
34
  logger = logging.getLogger(__name__)
35
 
36
  # Thread-local storage for database connections
@@ -229,10 +228,6 @@ async def process_url(url, depth, progress_callback):
229
 
230
  # App layout
231
  app.layout = dbc.Container([
232
- dcc.Location(id='url', refresh=True),
233
- dcc.Interval(id='completion-check-interval', interval=1000, n_intervals=0), # Check every second
234
- dcc.Store(id='refresh-flag', data={'refreshed': False}),
235
- dcc.Store(id='task-complete-store'),
236
  dcc.Store(id='pdf-store'),
237
  dcc.Store(id='progress-store', data={'progress': 0, 'message': ''}),
238
  dbc.Card(
@@ -248,8 +243,7 @@ app.layout = dbc.Container([
248
  html.Div(id="progress-message", className="text-center"),
249
  ], className="mb-3"),
250
  dcc.Interval(id="progress-interval", interval=1000, n_intervals=0),
251
- dcc.Download(id="download-pdf"),
252
- dcc.Interval(id="check-pdf-ready", interval=1000, n_intervals=0) # Check every second
253
  ]),
254
  className="mt-4"
255
  )
@@ -260,15 +254,15 @@ app.layout = dbc.Container([
260
  Output("download-button", "disabled"),
261
  Output("progress-bar", "value"),
262
  Output("progress-message", "children"),
 
263
  Input("submit-button", "n_clicks"),
264
  Input("progress-interval", "n_intervals"),
265
- Input("check-pdf-ready", "n_intervals"), # Add this line
266
  State("url-input", "value"),
267
  State("depth-slider", "value"),
268
  State("progress-store", "data"),
269
  prevent_initial_call=True
270
  )
271
- def update_progress(submit_clicks, n_intervals, check_pdf_intervals, url, depth, progress_data):
272
  global generated_file
273
  ctx = dash.callback_context
274
  if not ctx.triggered:
@@ -278,48 +272,28 @@ def update_progress(submit_clicks, n_intervals, check_pdf_intervals, url, depth,
278
 
279
  if triggered_id == "submit-button":
280
  if not url:
281
- return False, True, 0, "Please enter a URL"
282
-
283
- # Reset generated_file
284
- generated_file = None
285
-
286
  # Start the background task
287
  task_id = str(uuid.uuid4())
288
  executor.submit(background_task, url, depth, task_id)
289
 
290
- return True, True, 0, "Processing... Please wait."
291
 
292
- elif triggered_id in ["progress-interval", "check-pdf-ready"]:
293
  progress = progress_data['progress']
294
  message = progress_data['message']
295
 
296
  if isinstance(message, str) and message.startswith("Error"):
297
- return False, True, 100, message
298
- elif progress == 100 and generated_file is not None:
299
- return False, False, 100, "PDF ready for download!"
 
 
300
  elif progress > 0:
301
- return True, True, progress, message
302
-
303
- return False, True, 0, ""
304
-
305
- @app.callback(
306
- Output('url', 'refresh'),
307
- Output('refresh-flag', 'data'),
308
- Input('completion-check-interval', 'n_intervals'),
309
- State('refresh-flag', 'data'),
310
- prevent_initial_call=True
311
- )
312
- def check_task_completion(n_intervals, refresh_flag):
313
- if refresh_flag['refreshed']:
314
- raise PreventUpdate
315
 
316
- # Check the application logs for the completion message
317
- with open('app.log', 'r') as log_file:
318
- log_contents = log_file.read()
319
- if "INFO:__main__:Background task completed successfully" in log_contents:
320
- return True, {'refreshed': True}
321
-
322
- raise PreventUpdate
323
 
324
  @app.callback(
325
  Output("download-pdf", "data"),
@@ -346,26 +320,10 @@ def background_task(url, depth, task_id):
346
  logger.info("Background task completed successfully")
347
  generated_file = pdf_content
348
  app.layout.children[1].data = {'progress': 100, 'message': "PDF generation complete. Ready for download!"}
349
- # Update the task-complete-store
350
- app.layout.children[0].data = {'complete': True}
351
  except Exception as e:
352
  logger.error(f"Error in background task: {str(e)}")
353
  app.layout.children[1].data = {'progress': 100, 'message': f"Error: {str(e)}"}
354
 
355
- app.clientside_callback(
356
- """
357
- function(data) {
358
- if (data && data.complete) {
359
- location.reload();
360
- return null;
361
- }
362
- return null;
363
- }
364
- """,
365
- Output('task-complete-store', 'data'),
366
- Input('task-complete-store', 'data')
367
- )
368
-
369
  if __name__ == '__main__':
370
  print("Starting the Dash application...")
371
  app.run(debug=True, host='0.0.0.0', port=7860)
 
29
  server = app.server
30
 
31
  # Logging setup
32
+ logging.basicConfig(level=logging.INFO)
 
33
  logger = logging.getLogger(__name__)
34
 
35
  # Thread-local storage for database connections
 
228
 
229
  # App layout
230
  app.layout = dbc.Container([
 
 
 
 
231
  dcc.Store(id='pdf-store'),
232
  dcc.Store(id='progress-store', data={'progress': 0, 'message': ''}),
233
  dbc.Card(
 
243
  html.Div(id="progress-message", className="text-center"),
244
  ], className="mb-3"),
245
  dcc.Interval(id="progress-interval", interval=1000, n_intervals=0),
246
+ dcc.Download(id="download-pdf")
 
247
  ]),
248
  className="mt-4"
249
  )
 
254
  Output("download-button", "disabled"),
255
  Output("progress-bar", "value"),
256
  Output("progress-message", "children"),
257
+ Output("pdf-store", "data"),
258
  Input("submit-button", "n_clicks"),
259
  Input("progress-interval", "n_intervals"),
 
260
  State("url-input", "value"),
261
  State("depth-slider", "value"),
262
  State("progress-store", "data"),
263
  prevent_initial_call=True
264
  )
265
+ def update_progress(submit_clicks, n_intervals, url, depth, progress_data):
266
  global generated_file
267
  ctx = dash.callback_context
268
  if not ctx.triggered:
 
272
 
273
  if triggered_id == "submit-button":
274
  if not url:
275
+ return False, True, 0, "Please enter a URL", None
276
+
 
 
 
277
  # Start the background task
278
  task_id = str(uuid.uuid4())
279
  executor.submit(background_task, url, depth, task_id)
280
 
281
+ return True, True, 0, "Processing... Please wait.", None
282
 
283
+ elif triggered_id == "progress-interval":
284
  progress = progress_data['progress']
285
  message = progress_data['message']
286
 
287
  if isinstance(message, str) and message.startswith("Error"):
288
+ return False, True, 100, message, None
289
+ elif progress == 100 and generated_file:
290
+ # Encode the PDF file as base64
291
+ encoded_pdf = base64.b64encode(generated_file).decode('utf-8')
292
+ return False, False, 100, "PDF ready for download!", {'content': encoded_pdf, 'filename': f"website_content_{int(time.time())}.pdf"}
293
  elif progress > 0:
294
+ return True, True, progress, message, None
 
 
 
 
 
 
 
 
 
 
 
 
 
295
 
296
+ return False, True, 0, "", None
 
 
 
 
 
 
297
 
298
  @app.callback(
299
  Output("download-pdf", "data"),
 
320
  logger.info("Background task completed successfully")
321
  generated_file = pdf_content
322
  app.layout.children[1].data = {'progress': 100, 'message': "PDF generation complete. Ready for download!"}
 
 
323
  except Exception as e:
324
  logger.error(f"Error in background task: {str(e)}")
325
  app.layout.children[1].data = {'progress': 100, 'message': f"Error: {str(e)}"}
326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  if __name__ == '__main__':
328
  print("Starting the Dash application...")
329
  app.run(debug=True, host='0.0.0.0', port=7860)