Update app.py via AI Editor
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import dash
|
2 |
import dash_bootstrap_components as dbc
|
3 |
-
from dash import html, dcc, Input, Output, State, ctx
|
4 |
import flask
|
5 |
import uuid
|
6 |
import os
|
@@ -14,7 +14,6 @@ import re
|
|
14 |
import zipfile
|
15 |
import base64
|
16 |
|
17 |
-
# Configure logging
|
18 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
|
19 |
|
20 |
SESSION_DATA = {}
|
@@ -196,12 +195,12 @@ app.layout = dbc.Container(
|
|
196 |
Input('upload-pdf', 'contents'),
|
197 |
State('upload-pdf', 'filename'),
|
198 |
Input('clear-session', 'n_clicks'),
|
199 |
-
Input({'type': 'delete-upload-btn', 'index':
|
200 |
Input('split-btn', 'n_clicks'),
|
201 |
State('session-store', 'data'),
|
202 |
prevent_initial_call='initial_duplicate'
|
203 |
)
|
204 |
-
def handle_upload(contents, filename, clear_n,
|
205 |
trigger = ctx.triggered_id
|
206 |
session_id = get_session_id()
|
207 |
flask.g.session_id = session_id
|
@@ -211,12 +210,14 @@ def handle_upload(contents, filename, clear_n, delete_upload_n, split_n, session
|
|
211 |
if session_data is None:
|
212 |
session_data = {}
|
213 |
|
|
|
214 |
if trigger == 'clear-session':
|
215 |
clean_session(session_id)
|
216 |
resp_data = {}
|
217 |
return "", "", resp_data
|
218 |
|
219 |
-
|
|
|
220 |
orig_filename = session_data.get('orig_filename', '')
|
221 |
pdf_path = os.path.join(session_dir, orig_filename)
|
222 |
if os.path.exists(pdf_path):
|
@@ -227,6 +228,7 @@ def handle_upload(contents, filename, clear_n, delete_upload_n, split_n, session
|
|
227 |
os.remove(os.path.join(session_dir, file))
|
228 |
return "", "", {}
|
229 |
|
|
|
230 |
if trigger == 'upload-pdf':
|
231 |
if not contents:
|
232 |
return "", "", {}
|
@@ -265,8 +267,8 @@ def handle_upload(contents, filename, clear_n, delete_upload_n, split_n, session
|
|
265 |
logging.error(f"Error processing PDF: {e}")
|
266 |
return html.Div(f"Error: {e}", style={'color': 'red'}), "", {}
|
267 |
|
|
|
268 |
if session_data.get('orig_filename') and not session_data.get('split_files'):
|
269 |
-
# If user refreshes after upload but before split, restore file info and split button
|
270 |
file_info = dbc.Row([
|
271 |
dbc.Col(html.Div(f"Uploaded: {session_data['orig_filename']}"), width=9, style={'display': 'flex', 'alignItems': 'center'}),
|
272 |
dbc.Col(
|
@@ -282,6 +284,7 @@ def handle_upload(contents, filename, clear_n, delete_upload_n, split_n, session
|
|
282 |
])
|
283 |
return file_info, split_results, session_data
|
284 |
|
|
|
285 |
if trigger == 'split-btn':
|
286 |
orig_filename = session_data.get('orig_filename')
|
287 |
if not orig_filename:
|
@@ -322,7 +325,7 @@ def handle_upload(contents, filename, clear_n, delete_upload_n, split_n, session
|
|
322 |
logging.error(f"Error splitting PDF: {e}")
|
323 |
return html.Div(f"Error: {e}", style={'color': 'red'}), "", session_data
|
324 |
|
325 |
-
# Restore
|
326 |
if session_data.get('split_files'):
|
327 |
split_files = session_data['split_files']
|
328 |
orig_filename = session_data.get('orig_filename', '')
|
|
|
1 |
import dash
|
2 |
import dash_bootstrap_components as dbc
|
3 |
+
from dash import html, dcc, Input, Output, State, ctx, ALL
|
4 |
import flask
|
5 |
import uuid
|
6 |
import os
|
|
|
14 |
import zipfile
|
15 |
import base64
|
16 |
|
|
|
17 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
|
18 |
|
19 |
SESSION_DATA = {}
|
|
|
195 |
Input('upload-pdf', 'contents'),
|
196 |
State('upload-pdf', 'filename'),
|
197 |
Input('clear-session', 'n_clicks'),
|
198 |
+
Input({'type': 'delete-upload-btn', 'index': ALL}, 'n_clicks'),
|
199 |
Input('split-btn', 'n_clicks'),
|
200 |
State('session-store', 'data'),
|
201 |
prevent_initial_call='initial_duplicate'
|
202 |
)
|
203 |
+
def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, session_data):
|
204 |
trigger = ctx.triggered_id
|
205 |
session_id = get_session_id()
|
206 |
flask.g.session_id = session_id
|
|
|
210 |
if session_data is None:
|
211 |
session_data = {}
|
212 |
|
213 |
+
# Handle Clear Session
|
214 |
if trigger == 'clear-session':
|
215 |
clean_session(session_id)
|
216 |
resp_data = {}
|
217 |
return "", "", resp_data
|
218 |
|
219 |
+
# Handle Delete Upload (detect ANY delete button press)
|
220 |
+
if isinstance(trigger, dict) and trigger.get('type') == 'delete-upload-btn':
|
221 |
orig_filename = session_data.get('orig_filename', '')
|
222 |
pdf_path = os.path.join(session_dir, orig_filename)
|
223 |
if os.path.exists(pdf_path):
|
|
|
228 |
os.remove(os.path.join(session_dir, file))
|
229 |
return "", "", {}
|
230 |
|
231 |
+
# Handle Upload
|
232 |
if trigger == 'upload-pdf':
|
233 |
if not contents:
|
234 |
return "", "", {}
|
|
|
267 |
logging.error(f"Error processing PDF: {e}")
|
268 |
return html.Div(f"Error: {e}", style={'color': 'red'}), "", {}
|
269 |
|
270 |
+
# Restore after upload (before split)
|
271 |
if session_data.get('orig_filename') and not session_data.get('split_files'):
|
|
|
272 |
file_info = dbc.Row([
|
273 |
dbc.Col(html.Div(f"Uploaded: {session_data['orig_filename']}"), width=9, style={'display': 'flex', 'alignItems': 'center'}),
|
274 |
dbc.Col(
|
|
|
284 |
])
|
285 |
return file_info, split_results, session_data
|
286 |
|
287 |
+
# Handle Split
|
288 |
if trigger == 'split-btn':
|
289 |
orig_filename = session_data.get('orig_filename')
|
290 |
if not orig_filename:
|
|
|
325 |
logging.error(f"Error splitting PDF: {e}")
|
326 |
return html.Div(f"Error: {e}", style={'color': 'red'}), "", session_data
|
327 |
|
328 |
+
# Restore after split
|
329 |
if session_data.get('split_files'):
|
330 |
split_files = session_data['split_files']
|
331 |
orig_filename = session_data.get('orig_filename', '')
|