Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -262,38 +262,53 @@ app.layout = dbc.Container([
|
|
| 262 |
State("progress-store", "data"),
|
| 263 |
prevent_initial_call=True
|
| 264 |
)
|
| 265 |
-
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
ctx = dash.callback_context
|
| 268 |
-
|
| 269 |
-
raise PreventUpdate
|
| 270 |
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
@app.callback(
|
| 299 |
Output("download-pdf", "data"),
|
|
@@ -307,22 +322,34 @@ def download_pdf(n_clicks, pdf_data):
|
|
| 307 |
|
| 308 |
return dcc.send_bytes(base64.b64decode(pdf_data['content']), pdf_data['filename'])
|
| 309 |
|
| 310 |
-
def background_task(url, depth
|
| 311 |
-
global generated_file
|
| 312 |
-
|
| 313 |
-
def
|
| 314 |
-
|
| 315 |
-
|
|
|
|
| 316 |
|
| 317 |
try:
|
| 318 |
logger.info(f"Starting background task for URL: {url}, depth: {depth}")
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
logger.info("Background task completed successfully")
|
| 321 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 326 |
|
| 327 |
if __name__ == '__main__':
|
| 328 |
print("Starting the Dash application...")
|
|
|
|
| 262 |
State("progress-store", "data"),
|
| 263 |
prevent_initial_call=True
|
| 264 |
)
|
| 265 |
+
@app.callback(
|
| 266 |
+
Output("submit-button", "disabled"),
|
| 267 |
+
Output("download-button", "disabled"),
|
| 268 |
+
Output("progress-bar", "value"),
|
| 269 |
+
Output("progress-message", "children"),
|
| 270 |
+
Output("pdf-store", "data"),
|
| 271 |
+
Input("submit-button", "n_clicks"),
|
| 272 |
+
Input("progress-interval", "n_intervals"),
|
| 273 |
+
State("url-input", "value"),
|
| 274 |
+
State("depth-slider", "value"),
|
| 275 |
+
prevent_initial_call=True
|
| 276 |
+
)
|
| 277 |
+
def update_progress(submit_clicks, n_intervals, url, depth):
|
| 278 |
+
global generated_file, progress_data
|
| 279 |
ctx = dash.callback_context
|
| 280 |
+
triggered_id = ctx.triggered[0]['prop_id'].split('.')[0] if ctx.triggered else None
|
|
|
|
| 281 |
|
| 282 |
+
with data_lock:
|
| 283 |
+
current_progress = progress_data['progress']
|
| 284 |
+
message = progress_data['message']
|
| 285 |
+
pdf_ready = generated_file is not None
|
| 286 |
|
| 287 |
if triggered_id == "submit-button":
|
| 288 |
if not url:
|
| 289 |
return False, True, 0, "Please enter a URL", None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
+
with data_lock:
|
| 292 |
+
progress_data = {'progress': 0, 'message': "Processing... Please wait."}
|
| 293 |
+
|
| 294 |
+
executor.submit(background_task, url, depth)
|
| 295 |
return True, True, 0, "Processing... Please wait.", None
|
| 296 |
|
| 297 |
elif triggered_id == "progress-interval":
|
| 298 |
+
if pdf_ready:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
encoded_pdf = base64.b64encode(generated_file).decode('utf-8')
|
| 300 |
+
return (
|
| 301 |
+
False, False, 100,
|
| 302 |
+
"PDF ready for download!",
|
| 303 |
+
{'content': encoded_pdf, 'filename': f"website_content_{int(time.time())}.pdf"}
|
| 304 |
+
)
|
| 305 |
+
return (
|
| 306 |
+
True, True, current_progress,
|
| 307 |
+
message,
|
| 308 |
+
None
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
return dash.no_update
|
| 312 |
|
| 313 |
@app.callback(
|
| 314 |
Output("download-pdf", "data"),
|
|
|
|
| 322 |
|
| 323 |
return dcc.send_bytes(base64.b64decode(pdf_data['content']), pdf_data['filename'])
|
| 324 |
|
| 325 |
+
def background_task(url, depth):
|
| 326 |
+
global generated_file, progress_data
|
| 327 |
+
|
| 328 |
+
def update_progress_safe(message, progress):
|
| 329 |
+
with data_lock:
|
| 330 |
+
progress_data['progress'] = progress
|
| 331 |
+
progress_data['message'] = message
|
| 332 |
|
| 333 |
try:
|
| 334 |
logger.info(f"Starting background task for URL: {url}, depth: {depth}")
|
| 335 |
+
|
| 336 |
+
async def run_async():
|
| 337 |
+
return await process_url(url, depth, lambda msg: update_progress_safe(msg, int(float(msg.split()[-1].strip('%')))))
|
| 338 |
+
|
| 339 |
+
pdf_content = asyncio.run(run_async())
|
| 340 |
+
|
| 341 |
+
with data_lock:
|
| 342 |
+
generated_file = pdf_content
|
| 343 |
+
progress_data['progress'] = 100
|
| 344 |
+
progress_data['message'] = "PDF generation complete. Ready for download!"
|
| 345 |
+
|
| 346 |
logger.info("Background task completed successfully")
|
| 347 |
+
|
|
|
|
| 348 |
except Exception as e:
|
| 349 |
logger.error(f"Error in background task: {str(e)}")
|
| 350 |
+
with data_lock:
|
| 351 |
+
progress_data['message'] = f"Error: {str(e)}"
|
| 352 |
+
progress_data['progress'] = 100
|
| 353 |
|
| 354 |
if __name__ == '__main__':
|
| 355 |
print("Starting the Dash application...")
|