bluenevus commited on
Commit
e777af8
·
verified ·
1 Parent(s): 49e035d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -33
app.py CHANGED
@@ -12,9 +12,6 @@ import PyPDF2
12
 
13
  app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
14
 
15
- # Global variable to track conversion progress
16
- conversion_progress = 0
17
-
18
  app.layout = dbc.Container([
19
  html.H1("Auto-Wiki", className="my-4"),
20
  dcc.Upload(
@@ -37,11 +34,10 @@ app.layout = dbc.Container([
37
  accept='.docx,.pdf'
38
  ),
39
  html.Div(id='upload-output'),
40
- dbc.Progress(id="upload-progress", label="Upload Progress", style={"visibility": "hidden"}),
41
- dbc.Progress(id="conversion-progress", label="Conversion Progress", style={"visibility": "hidden"}),
42
  dbc.Button("Convert and Download", id="convert-button", color="primary", className="mt-3", disabled=True),
43
- dcc.Download(id="download-zip"),
44
- dcc.Interval(id='interval-component', interval=500, n_intervals=0)
45
  ])
46
 
47
  def process_docx(contents, filename):
@@ -64,9 +60,8 @@ def process_pdf(contents, filename):
64
  return '\n\n'.join(full_text)
65
 
66
  def process_files(contents, filenames):
67
- global conversion_progress
68
  processed_files = []
69
- for i, (c, n) in enumerate(zip(contents, filenames)):
70
  if n.lower().endswith('.docx'):
71
  text = process_docx(c, n)
72
  elif n.lower().endswith('.pdf'):
@@ -75,7 +70,6 @@ def process_files(contents, filenames):
75
  continue # Skip unsupported file types
76
  md = markdown.markdown(text)
77
  processed_files.append((n.replace('.docx', '.md').replace('.pdf', '.md'), md))
78
- conversion_progress = (i + 1) / len(contents) * 100
79
  time.sleep(0.1) # Simulate processing time
80
 
81
  zip_buffer = io.BytesIO()
@@ -88,20 +82,16 @@ def process_files(contents, filenames):
88
  @app.callback(
89
  [Output('upload-output', 'children'),
90
  Output('convert-button', 'disabled'),
91
- Output('upload-progress', 'value'),
92
- Output('upload-progress', 'style'),
93
- Output('conversion-progress', 'value'),
94
- Output('conversion-progress', 'style'),
95
  Output('download-zip', 'data')],
96
  [Input('upload-data', 'contents'),
97
  Input('upload-data', 'filename'),
98
- Input('convert-button', 'n_clicks'),
99
- Input('interval-component', 'n_intervals')],
100
  [State('upload-data', 'contents'),
101
  State('upload-data', 'filename')]
102
  )
103
- def update_output(list_of_contents, list_of_names, n_clicks, n_intervals, contents, filenames):
104
- global conversion_progress
105
  ctx = callback_context
106
  if not ctx.triggered:
107
  return no_update
@@ -114,27 +104,18 @@ def update_output(list_of_contents, list_of_names, n_clicks, n_intervals, conten
114
  html.Hr()
115
  ]) for name in list_of_names
116
  ]
117
- return children, False, 100, {"visibility": "visible"}, 0, {"visibility": "hidden"}, None
118
  return no_update
119
 
120
  if ctx.triggered[0]['prop_id'] == 'convert-button.n_clicks':
121
- if n_clicks is None:
122
- return no_update
123
-
124
- if not contents:
125
  return no_update
126
 
127
- conversion_progress = 0
128
- thread = threading.Thread(target=process_files, args=(contents, filenames))
129
- thread.start()
130
 
131
- return no_update, True, 100, {"visibility": "visible"}, 0, {"visibility": "visible"}, None
132
-
133
- if ctx.triggered[0]['prop_id'] == 'interval-component.n_intervals':
134
- if conversion_progress == 100:
135
- return no_update, False, 100, {"visibility": "visible"}, 100, {"visibility": "visible"}, dcc.send_bytes(process_files(contents, filenames), "converted_files.zip")
136
- else:
137
- return no_update, True, 100, {"visibility": "visible"}, conversion_progress, {"visibility": "visible"}, None
138
 
139
  return no_update
140
 
 
12
 
13
  app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
14
 
 
 
 
15
  app.layout = dbc.Container([
16
  html.H1("Auto-Wiki", className="my-4"),
17
  dcc.Upload(
 
34
  accept='.docx,.pdf'
35
  ),
36
  html.Div(id='upload-output'),
37
+ dbc.Spinner(html.Div(id="upload-spinner"), color="primary", type="grow", style={"display": "none"}),
38
+ dbc.Spinner(html.Div(id="conversion-spinner"), color="secondary", type="grow", style={"display": "none"}),
39
  dbc.Button("Convert and Download", id="convert-button", color="primary", className="mt-3", disabled=True),
40
+ dcc.Download(id="download-zip")
 
41
  ])
42
 
43
  def process_docx(contents, filename):
 
60
  return '\n\n'.join(full_text)
61
 
62
  def process_files(contents, filenames):
 
63
  processed_files = []
64
+ for c, n in zip(contents, filenames):
65
  if n.lower().endswith('.docx'):
66
  text = process_docx(c, n)
67
  elif n.lower().endswith('.pdf'):
 
70
  continue # Skip unsupported file types
71
  md = markdown.markdown(text)
72
  processed_files.append((n.replace('.docx', '.md').replace('.pdf', '.md'), md))
 
73
  time.sleep(0.1) # Simulate processing time
74
 
75
  zip_buffer = io.BytesIO()
 
82
  @app.callback(
83
  [Output('upload-output', 'children'),
84
  Output('convert-button', 'disabled'),
85
+ Output('upload-spinner', 'style'),
86
+ Output('conversion-spinner', 'style'),
 
 
87
  Output('download-zip', 'data')],
88
  [Input('upload-data', 'contents'),
89
  Input('upload-data', 'filename'),
90
+ Input('convert-button', 'n_clicks')],
 
91
  [State('upload-data', 'contents'),
92
  State('upload-data', 'filename')]
93
  )
94
+ def update_output(list_of_contents, list_of_names, n_clicks, contents, filenames):
 
95
  ctx = callback_context
96
  if not ctx.triggered:
97
  return no_update
 
104
  html.Hr()
105
  ]) for name in list_of_names
106
  ]
107
+ return children, False, {"display": "none"}, {"display": "none"}, None
108
  return no_update
109
 
110
  if ctx.triggered[0]['prop_id'] == 'convert-button.n_clicks':
111
+ if n_clicks is None or not contents:
 
 
 
112
  return no_update
113
 
114
+ def process_and_download():
115
+ zip_data = process_files(contents, filenames)
116
+ return dcc.send_bytes(zip_data, "converted_files.zip")
117
 
118
+ return no_update, True, {"display": "none"}, {"display": "block"}, process_and_download()
 
 
 
 
 
 
119
 
120
  return no_update
121