bluenevus commited on
Commit
b2ab0f1
·
verified ·
1 Parent(s): bdfc164

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -17
app.py CHANGED
@@ -4,6 +4,7 @@ import zipfile
4
  import logging
5
  from threading import Thread
6
  import json
 
7
 
8
  import dash
9
  import dash_bootstrap_components as dbc
@@ -21,6 +22,7 @@ app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
21
  # Global variables
22
  generated_file = None
23
  progress = 0
 
24
 
25
  # Layout
26
  app.layout = dbc.Container([
@@ -49,9 +51,13 @@ app.layout = dbc.Container([
49
  ], className="my-3"),
50
  dbc.Button("Split PDF", id='split-button', color="primary", className="mt-3", disabled=True),
51
  dbc.Progress(id='progress-bar', className="my-3"),
52
- dbc.Button("Download ZIP", id='download-button', color="success", className="mt-3"),
 
 
 
53
  dcc.Download(id="download-zip"),
54
  html.Div(id='log-output', style={'whiteSpace': 'pre-line'}),
 
55
  ], fluid=True)
56
 
57
  @callback(
@@ -96,8 +102,9 @@ def manage_ranges(add_clicks, remove_clicks, existing_ranges):
96
  return existing_ranges
97
 
98
  @callback(
99
- Output('progress-bar', 'value'),
100
- Output('log-output', 'children'),
 
101
  Input('split-button', 'n_clicks'),
102
  State('upload-pdf', 'contents'),
103
  State('upload-pdf', 'filename'),
@@ -105,7 +112,7 @@ def manage_ranges(add_clicks, remove_clicks, existing_ranges):
105
  prevent_initial_call=True
106
  )
107
  def split_pdf(n_clicks, contents, filename, ranges):
108
- global progress
109
  if not contents or not ranges:
110
  logger.warning("Split PDF clicked but no content or ranges provided")
111
  raise PreventUpdate
@@ -115,14 +122,15 @@ def split_pdf(n_clicks, contents, filename, ranges):
115
  logger.info(f"Processing {len(ranges)} ranges")
116
 
117
  progress = 0 # Reset progress
 
118
 
119
  thread = Thread(target=process_pdf, args=(contents, filename, ranges))
120
  thread.start()
121
 
122
- return 0, "PDF splitting process started. Check console for detailed logs."
123
 
124
  def process_pdf(contents, filename, ranges):
125
- global progress, generated_file
126
  try:
127
  # Decode PDF content
128
  content_type, content_string = contents.split(',')
@@ -149,6 +157,7 @@ def process_pdf(contents, filename, ranges):
149
  zf.writestr(f'split_{i+1}.pdf', output.getvalue())
150
 
151
  progress = (i + 1) / len(ranges) * 100
 
152
 
153
  zip_buffer.seek(0)
154
  generated_file = zip_buffer.getvalue()
@@ -156,23 +165,26 @@ def process_pdf(contents, filename, ranges):
156
  except Exception as e:
157
  logger.error(f"Error processing PDF: {str(e)}")
158
  progress = -1
 
 
159
 
160
  @callback(
161
- Output('progress-bar', 'value', allow_duplicate=True),
162
- Output('log-output', 'children', allow_duplicate=True),
163
- Input('progress-bar', 'value'),
 
164
  prevent_initial_call=True
165
  )
166
- def update_progress(value):
167
- global progress
168
- if progress == 100:
169
- logger.info("PDF splitting completed")
170
- return 100, "PDF splitting completed. Click 'Download ZIP' to get your files."
 
171
  elif progress == -1:
172
- logger.error("PDF splitting failed")
173
- return 0, "Error occurred during PDF splitting. Check console for details."
174
  else:
175
- return progress, f"Processing... {progress:.0f}% complete"
176
 
177
  @callback(
178
  Output("download-zip", "data"),
 
4
  import logging
5
  from threading import Thread
6
  import json
7
+ import time
8
 
9
  import dash
10
  import dash_bootstrap_components as dbc
 
22
  # Global variables
23
  generated_file = None
24
  progress = 0
25
+ is_processing = False
26
 
27
  # Layout
28
  app.layout = dbc.Container([
 
51
  ], className="my-3"),
52
  dbc.Button("Split PDF", id='split-button', color="primary", className="mt-3", disabled=True),
53
  dbc.Progress(id='progress-bar', className="my-3"),
54
+ dbc.Spinner(id="processing-spinner", color="primary", type="border", children=[
55
+ html.Div(id='processing-status')
56
+ ]),
57
+ dbc.Button("Download ZIP", id='download-button', color="success", className="mt-3", disabled=True),
58
  dcc.Download(id="download-zip"),
59
  html.Div(id='log-output', style={'whiteSpace': 'pre-line'}),
60
+ dcc.Interval(id='interval-component', interval=1000, n_intervals=0), # 1 second interval
61
  ], fluid=True)
62
 
63
  @callback(
 
102
  return existing_ranges
103
 
104
  @callback(
105
+ Output('processing-status', 'children'),
106
+ Output('split-button', 'disabled', allow_duplicate=True),
107
+ Output('download-button', 'disabled'),
108
  Input('split-button', 'n_clicks'),
109
  State('upload-pdf', 'contents'),
110
  State('upload-pdf', 'filename'),
 
112
  prevent_initial_call=True
113
  )
114
  def split_pdf(n_clicks, contents, filename, ranges):
115
+ global progress, is_processing
116
  if not contents or not ranges:
117
  logger.warning("Split PDF clicked but no content or ranges provided")
118
  raise PreventUpdate
 
122
  logger.info(f"Processing {len(ranges)} ranges")
123
 
124
  progress = 0 # Reset progress
125
+ is_processing = True
126
 
127
  thread = Thread(target=process_pdf, args=(contents, filename, ranges))
128
  thread.start()
129
 
130
+ return "Processing started...", True, True
131
 
132
  def process_pdf(contents, filename, ranges):
133
+ global progress, generated_file, is_processing
134
  try:
135
  # Decode PDF content
136
  content_type, content_string = contents.split(',')
 
157
  zf.writestr(f'split_{i+1}.pdf', output.getvalue())
158
 
159
  progress = (i + 1) / len(ranges) * 100
160
+ time.sleep(0.1) # Simulate some processing time
161
 
162
  zip_buffer.seek(0)
163
  generated_file = zip_buffer.getvalue()
 
165
  except Exception as e:
166
  logger.error(f"Error processing PDF: {str(e)}")
167
  progress = -1
168
+ finally:
169
+ is_processing = False
170
 
171
  @callback(
172
+ Output('progress-bar', 'value'),
173
+ Output('processing-status', 'children', allow_duplicate=True),
174
+ Output('download-button', 'disabled', allow_duplicate=True),
175
+ Input('interval-component', 'n_intervals'),
176
  prevent_initial_call=True
177
  )
178
+ def update_progress(n):
179
+ global progress, is_processing, generated_file
180
+ if is_processing:
181
+ return progress, f"Processing... {progress:.0f}% complete", True
182
+ elif progress == 100 and generated_file is not None:
183
+ return 100, "PDF splitting completed. Click 'Download ZIP' to get your files.", False
184
  elif progress == -1:
185
+ return 0, "Error occurred during PDF splitting. Please try again.", True
 
186
  else:
187
+ raise PreventUpdate
188
 
189
  @callback(
190
  Output("download-zip", "data"),