bluenevus commited on
Commit
4d04ed4
·
verified ·
1 Parent(s): 6dab01e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -34
app.py CHANGED
@@ -35,10 +35,9 @@ app.layout = dbc.Container([
35
  multiple=False
36
  ),
37
  dbc.Input(id="url-input", placeholder="Or enter PDF URL", type="text", className="mt-3"),
38
- dbc.Button("Compress", id="compress-btn", color="primary", className="mt-3"),
39
  dbc.Spinner(html.Div(id="compression-status"), color="primary", type="grow", spinner_class_name="mt-3"),
40
  dcc.Download(id="download-pdf"),
41
- dbc.Button("Download Compressed PDF", id="download-btn", color="success", className="mt-3", disabled=True),
42
  ])
43
  ]),
44
  ])
@@ -85,45 +84,31 @@ def compress_pdf(input_file, url):
85
 
86
  @callback(
87
  Output("compression-status", "children"),
88
- Output("download-btn", "disabled"),
89
  Output("download-pdf", "data"),
90
  Input("compress-btn", "n_clicks"),
91
- Input("download-btn", "n_clicks"),
92
  State("upload-pdf", "contents"),
93
  State("url-input", "value"),
94
  prevent_initial_call=True
95
  )
96
- def process_and_compress(compress_clicks, download_clicks, file_content, url):
97
- ctx = dash.callback_context
98
- if not ctx.triggered:
99
- raise PreventUpdate
100
-
101
- triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
102
-
103
- if triggered_id == "compress-btn":
104
- if file_content is None and (url is None or url.strip() == ""):
105
- return "Please provide either a file or a URL.", True, None
106
-
107
- def compression_thread():
108
- nonlocal file_content, url
109
- output_file, message = compress_pdf(file_content, url)
110
- if output_file:
111
- with open(output_file, "rb") as file:
112
- compressed_content = file.read()
113
- os.remove(output_file)
114
- return message, False, dcc.send_bytes(compressed_content, "compressed.pdf")
115
- else:
116
- return message, True, None
117
-
118
- thread = threading.Thread(target=compression_thread)
119
- thread.start()
120
- thread.join()
121
- return compression_thread()
122
-
123
- elif triggered_id == "download-btn":
124
- raise PreventUpdate
125
 
126
- raise PreventUpdate
 
 
 
127
 
128
  if __name__ == '__main__':
129
  print("Starting the Dash application...")
 
35
  multiple=False
36
  ),
37
  dbc.Input(id="url-input", placeholder="Or enter PDF URL", type="text", className="mt-3"),
38
+ dbc.Button("Compress PDF", id="compress-btn", color="primary", className="mt-3"),
39
  dbc.Spinner(html.Div(id="compression-status"), color="primary", type="grow", spinner_class_name="mt-3"),
40
  dcc.Download(id="download-pdf"),
 
41
  ])
42
  ]),
43
  ])
 
84
 
85
  @callback(
86
  Output("compression-status", "children"),
 
87
  Output("download-pdf", "data"),
88
  Input("compress-btn", "n_clicks"),
 
89
  State("upload-pdf", "contents"),
90
  State("url-input", "value"),
91
  prevent_initial_call=True
92
  )
93
+ def process_compress_and_download(n_clicks, file_content, url):
94
+ if file_content is None and (url is None or url.strip() == ""):
95
+ return "Please provide either a file or a URL.", None
96
+
97
+ def compression_thread():
98
+ nonlocal file_content, url
99
+ output_file, message = compress_pdf(file_content, url)
100
+ if output_file:
101
+ with open(output_file, "rb") as file:
102
+ compressed_content = file.read()
103
+ os.remove(output_file)
104
+ return message, dcc.send_bytes(compressed_content, "compressed.pdf")
105
+ else:
106
+ return message, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
+ thread = threading.Thread(target=compression_thread)
109
+ thread.start()
110
+ thread.join()
111
+ return compression_thread()
112
 
113
  if __name__ == '__main__':
114
  print("Starting the Dash application...")