Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -228,8 +228,7 @@ async def process_url(url, depth, progress_callback):
|
|
| 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(
|
| 234 |
dbc.CardBody([
|
| 235 |
html.H1("Website to PDF Converter", className="text-center mb-4"),
|
|
@@ -238,11 +237,7 @@ app.layout = dbc.Container([
|
|
| 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=True),
|
| 241 |
-
html.Div(
|
| 242 |
-
dbc.Progress(id="progress-bar", value=0, className="mb-3"),
|
| 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"
|
|
@@ -250,47 +245,31 @@ app.layout = dbc.Container([
|
|
| 250 |
], fluid=True)
|
| 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"),
|
|
|
|
|
|
|
| 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
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 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, 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, True, 100, message, None
|
| 288 |
-
elif progress == 100 and generated_file:
|
| 289 |
-
return False, False, 100, "PDF ready for download!", generated_file
|
| 290 |
-
elif progress > 0:
|
| 291 |
-
return True, True, progress, message, None
|
| 292 |
|
| 293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
@app.callback(
|
| 296 |
Output("download-pdf", "data"),
|
|
@@ -301,7 +280,6 @@ def update_progress(submit_clicks, n_intervals, url, depth, progress_data):
|
|
| 301 |
def download_pdf(n_clicks, pdf_data):
|
| 302 |
if pdf_data is None:
|
| 303 |
raise PreventUpdate
|
| 304 |
-
|
| 305 |
return dcc.send_bytes(pdf_data, f"website_content_{int(time.time())}.pdf")
|
| 306 |
|
| 307 |
def background_task(url, depth, task_id):
|
|
|
|
| 228 |
|
| 229 |
# App layout
|
| 230 |
app.layout = dbc.Container([
|
| 231 |
+
dcc.Store(id='pdf-store', storage_type='memory'),
|
|
|
|
| 232 |
dbc.Card(
|
| 233 |
dbc.CardBody([
|
| 234 |
html.H1("Website to PDF Converter", className="text-center mb-4"),
|
|
|
|
| 237 |
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"),
|
| 238 |
dbc.Button("Convert to PDF", id="submit-button", color="primary", className="mb-3 w-100"),
|
| 239 |
dbc.Button("Download PDF", id="download-button", color="secondary", className="mb-3 w-100", disabled=True),
|
| 240 |
+
html.Div(id="status-message", className="text-center mb-3"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
dcc.Download(id="download-pdf")
|
| 242 |
]),
|
| 243 |
className="mt-4"
|
|
|
|
| 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 |
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):
|