Update app.py via AI Editor
Browse files
app.py
CHANGED
|
@@ -141,6 +141,15 @@ app = dash.Dash(__name__, external_stylesheets=external_stylesheets, suppress_ca
|
|
| 141 |
server = app.server
|
| 142 |
app.title = "Intelligent PDF Splitter"
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
app.layout = dbc.Container(
|
| 145 |
[
|
| 146 |
dcc.Store(id='session-store', storage_type='session'),
|
|
@@ -172,7 +181,7 @@ app.layout = dbc.Container(
|
|
| 172 |
dbc.Button("Clear Session", id='clear-session', color='secondary', className='mt-2 mb-2'),
|
| 173 |
dcc.Loading(
|
| 174 |
id="loading", type="default",
|
| 175 |
-
children=[html.Div(id='split-results')]
|
| 176 |
)
|
| 177 |
]
|
| 178 |
)
|
|
@@ -214,13 +223,12 @@ def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, se
|
|
| 214 |
if trigger == 'clear-session':
|
| 215 |
clean_session(session_id)
|
| 216 |
resp_data = {}
|
| 217 |
-
return "",
|
| 218 |
|
| 219 |
# Handle Delete Upload (detect ANY delete button press)
|
| 220 |
delete_pressed = False
|
| 221 |
if isinstance(trigger, dict) and trigger.get('type') == 'delete-upload-btn':
|
| 222 |
delete_pressed = True
|
| 223 |
-
# Also handle if delete_upload_n_list is not None and has a value >0
|
| 224 |
if not delete_pressed and delete_upload_n_list is not None and len(delete_upload_n_list) > 0:
|
| 225 |
if any(n is not None and n > 0 for n in delete_upload_n_list):
|
| 226 |
delete_pressed = True
|
|
@@ -234,15 +242,15 @@ def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, se
|
|
| 234 |
if os.path.exists(session_dir):
|
| 235 |
for file in os.listdir(session_dir):
|
| 236 |
os.remove(os.path.join(session_dir, file))
|
| 237 |
-
return "",
|
| 238 |
|
| 239 |
# Handle Upload
|
| 240 |
if trigger == 'upload-pdf':
|
| 241 |
if not contents:
|
| 242 |
-
return "",
|
| 243 |
|
| 244 |
if not allowed_file(filename):
|
| 245 |
-
return html.Div("Only .pdf files are allowed.", style={'color': 'red'}),
|
| 246 |
|
| 247 |
try:
|
| 248 |
header, b64data = contents.split(',', 1)
|
|
@@ -273,7 +281,7 @@ def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, se
|
|
| 273 |
return file_info, split_results, session_data
|
| 274 |
except Exception as e:
|
| 275 |
logging.error(f"Error processing PDF: {e}")
|
| 276 |
-
return html.Div(f"Error: {e}", style={'color': 'red'}),
|
| 277 |
|
| 278 |
# Restore after upload (before split)
|
| 279 |
if session_data.get('orig_filename') and not session_data.get('split_files'):
|
|
@@ -296,10 +304,10 @@ def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, se
|
|
| 296 |
if trigger == 'split-btn':
|
| 297 |
orig_filename = session_data.get('orig_filename')
|
| 298 |
if not orig_filename:
|
| 299 |
-
return html.Div("No file to split.", style={'color': 'red'}),
|
| 300 |
pdf_path = os.path.join(session_dir, orig_filename)
|
| 301 |
if not os.path.exists(pdf_path):
|
| 302 |
-
return html.Div("Uploaded file not found. Please upload again.", style={'color': 'red'}),
|
| 303 |
try:
|
| 304 |
with lock:
|
| 305 |
split_files = intelligent_pdf_split(pdf_path, session_dir)
|
|
@@ -331,7 +339,7 @@ def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, se
|
|
| 331 |
return file_info, results, session_data
|
| 332 |
except Exception as e:
|
| 333 |
logging.error(f"Error splitting PDF: {e}")
|
| 334 |
-
return html.Div(f"Error: {e}", style={'color': 'red'}),
|
| 335 |
|
| 336 |
# Restore after split
|
| 337 |
if session_data.get('split_files'):
|
|
@@ -360,7 +368,7 @@ def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, se
|
|
| 360 |
]
|
| 361 |
return file_info, results, session_data
|
| 362 |
|
| 363 |
-
return "",
|
| 364 |
|
| 365 |
@app.server.route('/download_zip/<session_id>/<filename>')
|
| 366 |
def download_zip_file(session_id, filename):
|
|
|
|
| 141 |
server = app.server
|
| 142 |
app.title = "Intelligent PDF Splitter"
|
| 143 |
|
| 144 |
+
def get_split_results_placeholder():
|
| 145 |
+
# Always render split-btn (disabled) so Dash Input/Output registration never fails
|
| 146 |
+
return dbc.Row([
|
| 147 |
+
dbc.Col(
|
| 148 |
+
dbc.Button("Split PDF", id='split-btn', color='primary', className='mb-3 mt-2', n_clicks=0, style={'width': '180px', 'fontWeight': 'bold'}, disabled=True),
|
| 149 |
+
width=12, style={'display': 'flex', 'justifyContent': 'center'}
|
| 150 |
+
)
|
| 151 |
+
])
|
| 152 |
+
|
| 153 |
app.layout = dbc.Container(
|
| 154 |
[
|
| 155 |
dcc.Store(id='session-store', storage_type='session'),
|
|
|
|
| 181 |
dbc.Button("Clear Session", id='clear-session', color='secondary', className='mt-2 mb-2'),
|
| 182 |
dcc.Loading(
|
| 183 |
id="loading", type="default",
|
| 184 |
+
children=[html.Div(id='split-results', children=get_split_results_placeholder())]
|
| 185 |
)
|
| 186 |
]
|
| 187 |
)
|
|
|
|
| 223 |
if trigger == 'clear-session':
|
| 224 |
clean_session(session_id)
|
| 225 |
resp_data = {}
|
| 226 |
+
return "", get_split_results_placeholder(), resp_data
|
| 227 |
|
| 228 |
# Handle Delete Upload (detect ANY delete button press)
|
| 229 |
delete_pressed = False
|
| 230 |
if isinstance(trigger, dict) and trigger.get('type') == 'delete-upload-btn':
|
| 231 |
delete_pressed = True
|
|
|
|
| 232 |
if not delete_pressed and delete_upload_n_list is not None and len(delete_upload_n_list) > 0:
|
| 233 |
if any(n is not None and n > 0 for n in delete_upload_n_list):
|
| 234 |
delete_pressed = True
|
|
|
|
| 242 |
if os.path.exists(session_dir):
|
| 243 |
for file in os.listdir(session_dir):
|
| 244 |
os.remove(os.path.join(session_dir, file))
|
| 245 |
+
return "", get_split_results_placeholder(), {}
|
| 246 |
|
| 247 |
# Handle Upload
|
| 248 |
if trigger == 'upload-pdf':
|
| 249 |
if not contents:
|
| 250 |
+
return "", get_split_results_placeholder(), {}
|
| 251 |
|
| 252 |
if not allowed_file(filename):
|
| 253 |
+
return html.Div("Only .pdf files are allowed.", style={'color': 'red'}), get_split_results_placeholder(), {}
|
| 254 |
|
| 255 |
try:
|
| 256 |
header, b64data = contents.split(',', 1)
|
|
|
|
| 281 |
return file_info, split_results, session_data
|
| 282 |
except Exception as e:
|
| 283 |
logging.error(f"Error processing PDF: {e}")
|
| 284 |
+
return html.Div(f"Error: {e}", style={'color': 'red'}), get_split_results_placeholder(), {}
|
| 285 |
|
| 286 |
# Restore after upload (before split)
|
| 287 |
if session_data.get('orig_filename') and not session_data.get('split_files'):
|
|
|
|
| 304 |
if trigger == 'split-btn':
|
| 305 |
orig_filename = session_data.get('orig_filename')
|
| 306 |
if not orig_filename:
|
| 307 |
+
return html.Div("No file to split.", style={'color': 'red'}), get_split_results_placeholder(), session_data
|
| 308 |
pdf_path = os.path.join(session_dir, orig_filename)
|
| 309 |
if not os.path.exists(pdf_path):
|
| 310 |
+
return html.Div("Uploaded file not found. Please upload again.", style={'color': 'red'}), get_split_results_placeholder(), {}
|
| 311 |
try:
|
| 312 |
with lock:
|
| 313 |
split_files = intelligent_pdf_split(pdf_path, session_dir)
|
|
|
|
| 339 |
return file_info, results, session_data
|
| 340 |
except Exception as e:
|
| 341 |
logging.error(f"Error splitting PDF: {e}")
|
| 342 |
+
return html.Div(f"Error: {e}", style={'color': 'red'}), get_split_results_placeholder(), session_data
|
| 343 |
|
| 344 |
# Restore after split
|
| 345 |
if session_data.get('split_files'):
|
|
|
|
| 368 |
]
|
| 369 |
return file_info, results, session_data
|
| 370 |
|
| 371 |
+
return "", get_split_results_placeholder(), session_data
|
| 372 |
|
| 373 |
@app.server.route('/download_zip/<session_id>/<filename>')
|
| 374 |
def download_zip_file(session_id, filename):
|