Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -226,6 +226,7 @@ async def process_url(url, depth, progress_callback):
|
|
| 226 |
logger.error(f"Error in process_url: {str(e)}")
|
| 227 |
return f"An error occurred: {str(e)}"
|
| 228 |
|
|
|
|
| 229 |
# App layout
|
| 230 |
app.layout = dbc.Container([
|
| 231 |
dcc.Store(id='pdf-store'),
|
|
@@ -237,7 +238,7 @@ app.layout = dbc.Container([
|
|
| 237 |
dbc.Input(id="url-input", type="text", placeholder="Enter website URL (e.g., https://www.gradio.app/docs)", className="mb-3"),
|
| 238 |
dcc.Slider(id="depth-slider", min=1, max=10, step=1, value=3, marks={i: str(i) for i in range(1, 11)}, className="mb-3"),
|
| 239 |
dbc.Button("Convert to PDF", id="submit-button", color="primary", className="mb-3 w-100"),
|
| 240 |
-
dbc.Button("Download PDF", id="download-button", color="secondary", className="mb-3 w-100", disabled
|
| 241 |
html.Div([
|
| 242 |
dbc.Progress(id="progress-bar", value=0, className="mb-3"),
|
| 243 |
html.Div(id="progress-message", className="text-center"),
|
|
@@ -251,7 +252,6 @@ app.layout = dbc.Container([
|
|
| 251 |
|
| 252 |
@app.callback(
|
| 253 |
Output("submit-button", "disabled"),
|
| 254 |
-
Output("download-button", "disabled"),
|
| 255 |
Output("progress-bar", "value"),
|
| 256 |
Output("progress-message", "children"),
|
| 257 |
Output("pdf-store", "data"),
|
|
@@ -271,26 +271,39 @@ def update_output(n_clicks, n_intervals, url, depth, progress_data):
|
|
| 271 |
|
| 272 |
if triggered_id == "submit-button":
|
| 273 |
if not url:
|
| 274 |
-
return False,
|
| 275 |
|
| 276 |
# Start the background task
|
| 277 |
task_id = str(uuid.uuid4())
|
| 278 |
executor.submit(background_task, url, depth, task_id)
|
| 279 |
|
| 280 |
-
return True,
|
| 281 |
|
| 282 |
elif triggered_id == "progress-interval":
|
| 283 |
progress = progress_data['progress']
|
| 284 |
message = progress_data['message']
|
| 285 |
|
| 286 |
if isinstance(message, str) and message.startswith("Error"):
|
| 287 |
-
return False,
|
| 288 |
elif progress == 100 and generated_file:
|
| 289 |
-
return False,
|
| 290 |
elif progress > 0:
|
| 291 |
-
return True,
|
|
|
|
|
|
|
| 292 |
|
| 293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
@app.callback(
|
| 296 |
Output("download-pdf", "data"),
|
|
|
|
| 226 |
logger.error(f"Error in process_url: {str(e)}")
|
| 227 |
return f"An error occurred: {str(e)}"
|
| 228 |
|
| 229 |
+
# App layout
|
| 230 |
# App layout
|
| 231 |
app.layout = dbc.Container([
|
| 232 |
dcc.Store(id='pdf-store'),
|
|
|
|
| 238 |
dbc.Input(id="url-input", type="text", placeholder="Enter website URL (e.g., https://www.gradio.app/docs)", className="mb-3"),
|
| 239 |
dcc.Slider(id="depth-slider", min=1, max=10, step=1, value=3, marks={i: str(i) for i in range(1, 11)}, className="mb-3"),
|
| 240 |
dbc.Button("Convert to PDF", id="submit-button", color="primary", className="mb-3 w-100"),
|
| 241 |
+
dbc.Button("Download PDF", id="download-button", color="secondary", className="mb-3 w-100"), # Removed disabled property
|
| 242 |
html.Div([
|
| 243 |
dbc.Progress(id="progress-bar", value=0, className="mb-3"),
|
| 244 |
html.Div(id="progress-message", className="text-center"),
|
|
|
|
| 252 |
|
| 253 |
@app.callback(
|
| 254 |
Output("submit-button", "disabled"),
|
|
|
|
| 255 |
Output("progress-bar", "value"),
|
| 256 |
Output("progress-message", "children"),
|
| 257 |
Output("pdf-store", "data"),
|
|
|
|
| 271 |
|
| 272 |
if triggered_id == "submit-button":
|
| 273 |
if not url:
|
| 274 |
+
return False, 0, "Please enter a URL", None
|
| 275 |
|
| 276 |
# Start the background task
|
| 277 |
task_id = str(uuid.uuid4())
|
| 278 |
executor.submit(background_task, url, depth, task_id)
|
| 279 |
|
| 280 |
+
return True, 0, "Processing... Please wait.", None
|
| 281 |
|
| 282 |
elif triggered_id == "progress-interval":
|
| 283 |
progress = progress_data['progress']
|
| 284 |
message = progress_data['message']
|
| 285 |
|
| 286 |
if isinstance(message, str) and message.startswith("Error"):
|
| 287 |
+
return False, 100, message, None
|
| 288 |
elif progress == 100 and generated_file:
|
| 289 |
+
return False, 100, "PDF ready for download!", generated_file
|
| 290 |
elif progress > 0:
|
| 291 |
+
return True, progress, message, None
|
| 292 |
+
|
| 293 |
+
return False, 0, "", None
|
| 294 |
|
| 295 |
+
@app.callback(
|
| 296 |
+
Output("download-pdf", "data"),
|
| 297 |
+
Input("download-button", "n_clicks"),
|
| 298 |
+
State("pdf-store", "data"),
|
| 299 |
+
prevent_initial_call=True
|
| 300 |
+
)
|
| 301 |
+
def download_pdf(n_clicks, pdf_data):
|
| 302 |
+
if pdf_data is None:
|
| 303 |
+
# If PDF is not ready, return None to prevent download
|
| 304 |
+
return None
|
| 305 |
+
|
| 306 |
+
return dcc.send_bytes(pdf_data, f"website_content_{int(time.time())}.pdf")
|
| 307 |
|
| 308 |
@app.callback(
|
| 309 |
Output("download-pdf", "data"),
|