Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -60,7 +60,15 @@ app.layout = dbc.Container([
|
|
60 |
html.Div(id='log-output', style={'whiteSpace': 'pre-line'}),
|
61 |
], fluid=True)
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
@callback(
|
66 |
Output('progress-bar', 'value'),
|
@@ -74,6 +82,7 @@ app.layout = dbc.Container([
|
|
74 |
prevent_initial_call=True
|
75 |
)
|
76 |
def split_pdf(n_clicks, contents, filename, ranges):
|
|
|
77 |
if not contents or not ranges:
|
78 |
logger.warning("Split PDF clicked but no content or ranges provided")
|
79 |
raise PreventUpdate
|
@@ -82,11 +91,49 @@ def split_pdf(n_clicks, contents, filename, ranges):
|
|
82 |
ranges = [r for r in ranges if r] # Filter out empty ranges
|
83 |
logger.info(f"Processing {len(ranges)} ranges")
|
84 |
|
|
|
|
|
85 |
thread = Thread(target=process_pdf, args=(contents, filename, ranges))
|
86 |
thread.start()
|
87 |
|
88 |
return 0, True, "PDF splitting process started. Check console for detailed logs.", "Processing..."
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
@callback(
|
91 |
Output('progress-bar', 'value', allow_duplicate=True),
|
92 |
Output('download-button', 'disabled', allow_duplicate=True),
|
@@ -102,7 +149,7 @@ def update_progress(value):
|
|
102 |
return 100, False, "PDF splitting completed. Click 'Download ZIP' to get your files.", ""
|
103 |
elif progress == -1:
|
104 |
logger.error("PDF splitting failed")
|
105 |
-
return 0,
|
106 |
else:
|
107 |
return progress, True, f"Processing... {progress:.0f}% complete", "Processing..."
|
108 |
|
|
|
60 |
html.Div(id='log-output', style={'whiteSpace': 'pre-line'}),
|
61 |
], fluid=True)
|
62 |
|
63 |
+
@callback(
|
64 |
+
Output('split-button', 'disabled'),
|
65 |
+
Input('upload-pdf', 'contents')
|
66 |
+
)
|
67 |
+
def enable_split_button(contents):
|
68 |
+
if contents:
|
69 |
+
logger.info("PDF uploaded, enabling Split PDF button")
|
70 |
+
return False
|
71 |
+
return True
|
72 |
|
73 |
@callback(
|
74 |
Output('progress-bar', 'value'),
|
|
|
82 |
prevent_initial_call=True
|
83 |
)
|
84 |
def split_pdf(n_clicks, contents, filename, ranges):
|
85 |
+
global progress
|
86 |
if not contents or not ranges:
|
87 |
logger.warning("Split PDF clicked but no content or ranges provided")
|
88 |
raise PreventUpdate
|
|
|
91 |
ranges = [r for r in ranges if r] # Filter out empty ranges
|
92 |
logger.info(f"Processing {len(ranges)} ranges")
|
93 |
|
94 |
+
progress = 0 # Reset progress
|
95 |
+
|
96 |
thread = Thread(target=process_pdf, args=(contents, filename, ranges))
|
97 |
thread.start()
|
98 |
|
99 |
return 0, True, "PDF splitting process started. Check console for detailed logs.", "Processing..."
|
100 |
|
101 |
+
def process_pdf(contents, filename, ranges):
|
102 |
+
global progress, generated_file
|
103 |
+
try:
|
104 |
+
# Decode PDF content
|
105 |
+
content_type, content_string = contents.split(',')
|
106 |
+
decoded = base64.b64decode(content_string)
|
107 |
+
|
108 |
+
# Read the PDF
|
109 |
+
pdf = PdfReader(io.BytesIO(decoded))
|
110 |
+
total_pages = len(pdf.pages)
|
111 |
+
|
112 |
+
# Create a ZIP file in memory
|
113 |
+
zip_buffer = io.BytesIO()
|
114 |
+
with zipfile.ZipFile(zip_buffer, 'w') as zf:
|
115 |
+
for i, page_range in enumerate(ranges):
|
116 |
+
start, end = map(int, page_range.split('-'))
|
117 |
+
writer = PdfWriter()
|
118 |
+
|
119 |
+
for page_num in range(start - 1, min(end, total_pages)):
|
120 |
+
writer.add_page(pdf.pages[page_num])
|
121 |
+
|
122 |
+
# Save the split PDF to the ZIP file
|
123 |
+
output = io.BytesIO()
|
124 |
+
writer.write(output)
|
125 |
+
output.seek(0)
|
126 |
+
zf.writestr(f'split_{i+1}.pdf', output.getvalue())
|
127 |
+
|
128 |
+
progress = (i + 1) / len(ranges) * 100
|
129 |
+
|
130 |
+
zip_buffer.seek(0)
|
131 |
+
generated_file = zip_buffer.getvalue()
|
132 |
+
progress = 100
|
133 |
+
except Exception as e:
|
134 |
+
logger.error(f"Error processing PDF: {str(e)}")
|
135 |
+
progress = -1
|
136 |
+
|
137 |
@callback(
|
138 |
Output('progress-bar', 'value', allow_duplicate=True),
|
139 |
Output('download-button', 'disabled', allow_duplicate=True),
|
|
|
149 |
return 100, False, "PDF splitting completed. Click 'Download ZIP' to get your files.", ""
|
150 |
elif progress == -1:
|
151 |
logger.error("PDF splitting failed")
|
152 |
+
return 0, True, "Error occurred during PDF splitting. Check console for details.", ""
|
153 |
else:
|
154 |
return progress, True, f"Processing... {progress:.0f}% complete", "Processing..."
|
155 |
|