bluenevus commited on
Commit
eb83bba
·
verified ·
1 Parent(s): 0736491

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -21
app.py CHANGED
@@ -15,6 +15,7 @@ app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
15
  uploaded_files = {}
16
  converted_files = {}
17
  conversion_progress = {}
 
18
 
19
  def convert_pdf_to_docx(pdf_path, docx_path):
20
  cv = Converter(pdf_path)
@@ -27,7 +28,7 @@ def process_contents(contents, filename):
27
  return io.BytesIO(decoded)
28
 
29
  def convert_files(filenames):
30
- global conversion_progress, converted_files
31
  total_files = len(filenames)
32
  for i, filename in enumerate(filenames):
33
  pdf_file = uploaded_files[filename]
@@ -50,6 +51,7 @@ def convert_files(filenames):
50
  conversion_progress[filename] = (i + 1) / total_files * 100
51
 
52
  conversion_progress['overall'] = 100
 
53
 
54
  app.layout = dbc.Container([
55
  dbc.Card(
@@ -76,7 +78,8 @@ app.layout = dbc.Container([
76
  html.Div(id='upload-output'),
77
  dbc.Button("Convert and Download", id="convert-button", color="primary", className="mt-3 mb-3", disabled=True),
78
  html.Div(id='conversion-output'),
79
- dcc.Download(id="download-zip")
 
80
  ]),
81
  className="mt-3"
82
  )
@@ -104,36 +107,50 @@ def update_output(list_of_contents, list_of_names):
104
  return [], True
105
 
106
  @app.callback(
107
- Output('conversion-output', 'children'),
108
- Output('download-zip', 'data'),
109
  Input('convert-button', 'n_clicks'),
110
  prevent_initial_call=True
111
  )
112
- def convert_and_download(n_clicks):
113
  if n_clicks is None:
114
- return [], None
115
 
116
- global conversion_progress, converted_files
117
  conversion_progress.clear()
118
  converted_files.clear()
119
  conversion_progress['overall'] = 0
 
120
 
121
  threading.Thread(target=convert_files, args=(list(uploaded_files.keys()),)).start()
 
122
 
123
- while conversion_progress.get('overall', 0) < 100:
124
- time.sleep(0.1)
125
- progress_bars = [
126
- dbc.Progress(value=conversion_progress.get(filename, 0), label=f"{filename}: {conversion_progress.get(filename, 0):.0f}%", className="mb-3")
127
- for filename in uploaded_files.keys()
128
- ]
129
- yield progress_bars, None
130
-
131
- with io.BytesIO() as zip_buffer:
132
- with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
133
- for filename, file_content in converted_files.items():
134
- zip_file.writestr(filename, file_content.getvalue())
135
-
136
- return [html.Div("Conversion complete! Downloading ZIP file...")], dcc.send_bytes(zip_buffer.getvalue(), "converted_files.zip")
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  if __name__ == '__main__':
139
  print("Starting the Dash application...")
 
15
  uploaded_files = {}
16
  converted_files = {}
17
  conversion_progress = {}
18
+ conversion_complete = False
19
 
20
  def convert_pdf_to_docx(pdf_path, docx_path):
21
  cv = Converter(pdf_path)
 
28
  return io.BytesIO(decoded)
29
 
30
  def convert_files(filenames):
31
+ global conversion_progress, converted_files, conversion_complete
32
  total_files = len(filenames)
33
  for i, filename in enumerate(filenames):
34
  pdf_file = uploaded_files[filename]
 
51
  conversion_progress[filename] = (i + 1) / total_files * 100
52
 
53
  conversion_progress['overall'] = 100
54
+ conversion_complete = True
55
 
56
  app.layout = dbc.Container([
57
  dbc.Card(
 
78
  html.Div(id='upload-output'),
79
  dbc.Button("Convert and Download", id="convert-button", color="primary", className="mt-3 mb-3", disabled=True),
80
  html.Div(id='conversion-output'),
81
+ dcc.Download(id="download-zip"),
82
+ dcc.Interval(id='interval-component', interval=500, n_intervals=0, disabled=True)
83
  ]),
84
  className="mt-3"
85
  )
 
107
  return [], True
108
 
109
  @app.callback(
110
+ Output('interval-component', 'disabled'),
 
111
  Input('convert-button', 'n_clicks'),
112
  prevent_initial_call=True
113
  )
114
+ def start_conversion(n_clicks):
115
  if n_clicks is None:
116
+ return True
117
 
118
+ global conversion_progress, converted_files, conversion_complete
119
  conversion_progress.clear()
120
  converted_files.clear()
121
  conversion_progress['overall'] = 0
122
+ conversion_complete = False
123
 
124
  threading.Thread(target=convert_files, args=(list(uploaded_files.keys()),)).start()
125
+ return False
126
 
127
+ @app.callback(
128
+ Output('conversion-output', 'children'),
129
+ Input('interval-component', 'n_intervals'),
130
+ prevent_initial_call=True
131
+ )
132
+ def update_progress(n):
133
+ progress_bars = [
134
+ dbc.Progress(value=conversion_progress.get(filename, 0), label=f"{filename}: {conversion_progress.get(filename, 0):.0f}%", className="mb-3")
135
+ for filename in uploaded_files.keys()
136
+ ]
137
+ return progress_bars
138
+
139
+ @app.callback(
140
+ Output('download-zip', 'data'),
141
+ Output('interval-component', 'disabled', allow_duplicate=True),
142
+ Input('interval-component', 'n_intervals'),
143
+ prevent_initial_call=True
144
+ )
145
+ def check_conversion_complete(n):
146
+ if conversion_complete:
147
+ with io.BytesIO() as zip_buffer:
148
+ with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
149
+ for filename, file_content in converted_files.items():
150
+ zip_file.writestr(filename, file_content.getvalue())
151
+
152
+ return dcc.send_bytes(zip_buffer.getvalue(), "converted_files.zip"), True
153
+ return None, False
154
 
155
  if __name__ == '__main__':
156
  print("Starting the Dash application...")