bluenevus commited on
Commit
5d90167
·
verified ·
1 Parent(s): 98b6be1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -30
app.py CHANGED
@@ -14,17 +14,12 @@ app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
14
  # Global variables
15
  uploaded_files = {}
16
  converted_files = {}
17
- conversion_progress = {}
18
  conversion_complete = False
19
 
20
- def convert_pdf_to_docx(pdf_path, docx_path, filename):
21
  cv = Converter(pdf_path)
22
- total_pages = cv.get_pages_number()
23
-
24
- def page_callback(current, total):
25
- conversion_progress[filename] = (current / total) * 100
26
-
27
- cv.convert(docx_path, start=0, end=None, pages=None, thread_count=3, page_callback=page_callback)
28
  cv.close()
29
 
30
  def process_contents(contents, filename):
@@ -33,9 +28,9 @@ def process_contents(contents, filename):
33
  return io.BytesIO(decoded)
34
 
35
  def convert_files(filenames):
36
- global conversion_progress, converted_files, conversion_complete
37
- total_files = len(filenames)
38
- for i, filename in enumerate(filenames):
39
  pdf_file = uploaded_files[filename]
40
  with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as temp_pdf:
41
  temp_pdf.write(pdf_file.getvalue())
@@ -45,7 +40,7 @@ def convert_files(filenames):
45
  with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as temp_docx:
46
  temp_docx_path = temp_docx.name
47
 
48
- convert_pdf_to_docx(temp_pdf_path, temp_docx_path, filename)
49
 
50
  with open(temp_docx_path, 'rb') as docx_file:
51
  converted_files[docx_filename] = io.BytesIO(docx_file.read())
@@ -53,6 +48,7 @@ def convert_files(filenames):
53
  os.unlink(temp_pdf_path)
54
  os.unlink(temp_docx_path)
55
 
 
56
  conversion_complete = True
57
 
58
  app.layout = dbc.Container([
@@ -81,7 +77,7 @@ app.layout = dbc.Container([
81
  dbc.Button("Convert and Download", id="convert-button", color="primary", className="mt-3 mb-3", disabled=True),
82
  html.Div(id='conversion-output'),
83
  dcc.Download(id="download-zip"),
84
- dcc.Interval(id='interval-component', interval=100, n_intervals=0, disabled=True)
85
  ]),
86
  className="mt-3"
87
  )
@@ -117,8 +113,7 @@ def start_conversion(n_clicks):
117
  if n_clicks is None:
118
  return True
119
 
120
- global conversion_progress, converted_files, conversion_complete
121
- conversion_progress.clear()
122
  converted_files.clear()
123
  conversion_complete = False
124
 
@@ -130,21 +125,18 @@ def start_conversion(n_clicks):
130
  Input('interval-component', 'n_intervals'),
131
  prevent_initial_call=True
132
  )
133
- def update_progress(n):
134
- progress_bars = []
135
- overall_progress = 0
136
- for filename in uploaded_files.keys():
137
- file_progress = conversion_progress.get(filename, 0)
138
- progress_bars.append(
139
- dbc.Progress(value=file_progress, label=f"{filename}: {file_progress:.0f}%", className="mb-3")
140
- )
141
- overall_progress += file_progress
142
-
143
- if uploaded_files:
144
- overall_progress /= len(uploaded_files)
145
- progress_bars.insert(0, dbc.Progress(value=overall_progress, label=f"Overall: {overall_progress:.0f}%", className="mb-3"))
146
-
147
- return progress_bars
148
 
149
  @app.callback(
150
  Output('download-zip', 'data'),
 
14
  # Global variables
15
  uploaded_files = {}
16
  converted_files = {}
17
+ current_file = ""
18
  conversion_complete = False
19
 
20
+ def convert_pdf_to_docx(pdf_path, docx_path):
21
  cv = Converter(pdf_path)
22
+ cv.convert(docx_path)
 
 
 
 
 
23
  cv.close()
24
 
25
  def process_contents(contents, filename):
 
28
  return io.BytesIO(decoded)
29
 
30
  def convert_files(filenames):
31
+ global converted_files, current_file, conversion_complete
32
+ for filename in filenames:
33
+ current_file = filename
34
  pdf_file = uploaded_files[filename]
35
  with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as temp_pdf:
36
  temp_pdf.write(pdf_file.getvalue())
 
40
  with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as temp_docx:
41
  temp_docx_path = temp_docx.name
42
 
43
+ convert_pdf_to_docx(temp_pdf_path, temp_docx_path)
44
 
45
  with open(temp_docx_path, 'rb') as docx_file:
46
  converted_files[docx_filename] = io.BytesIO(docx_file.read())
 
48
  os.unlink(temp_pdf_path)
49
  os.unlink(temp_docx_path)
50
 
51
+ current_file = ""
52
  conversion_complete = True
53
 
54
  app.layout = dbc.Container([
 
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
+ dcc.Interval(id='interval-component', interval=500, n_intervals=0, disabled=True)
81
  ]),
82
  className="mt-3"
83
  )
 
113
  if n_clicks is None:
114
  return True
115
 
116
+ global converted_files, conversion_complete
 
117
  converted_files.clear()
118
  conversion_complete = False
119
 
 
125
  Input('interval-component', 'n_intervals'),
126
  prevent_initial_call=True
127
  )
128
+ def update_status(n):
129
+ if current_file:
130
+ return [
131
+ html.Div([
132
+ dbc.Spinner(size="sm", color="primary", type="grow"),
133
+ html.Span(f" Converting: {current_file}", className="ml-2")
134
+ ], className="d-flex align-items-center")
135
+ ]
136
+ elif conversion_complete:
137
+ return [html.Div("Conversion complete! Preparing download...")]
138
+ else:
139
+ return [html.Div("Starting conversion...")]
 
 
 
140
 
141
  @app.callback(
142
  Output('download-zip', 'data'),