Spaces:
Paused
Paused
Update app.py
Browse files
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
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
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 |
-
|
|
|
|
|
|
|
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...")
|