Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,21 +41,16 @@ app.layout = dbc.Container([
|
|
| 41 |
},
|
| 42 |
multiple=False
|
| 43 |
),
|
| 44 |
-
html.Div(id='pdf-name'),
|
| 45 |
dbc.Card([
|
| 46 |
dbc.CardBody([
|
| 47 |
-
html.Div(id='ranges-container', children=[
|
| 48 |
-
dbc.Row([
|
| 49 |
-
dbc.Col(dbc.Input(id={'type': 'range-input', 'index': 0}, type='text', placeholder='Enter page range (e.g., 1-3)'), width=10),
|
| 50 |
-
dbc.Col(dbc.Button("Remove", id={'type': 'remove-range', 'index': 0}, color="danger", size="sm"), width=2),
|
| 51 |
-
], className="mb-2"),
|
| 52 |
-
]),
|
| 53 |
dbc.Button("Add Range", id='add-range', color="secondary", className="mt-2"),
|
| 54 |
])
|
| 55 |
], className="my-3"),
|
| 56 |
dbc.Button("Split PDF", id='split-button', color="primary", className="mt-3", disabled=True),
|
| 57 |
dbc.Progress(id='progress-bar', className="my-3"),
|
| 58 |
-
dbc.Button("Download ZIP", id='download-button', color="success", className="mt-3"),
|
| 59 |
dcc.Download(id="download-zip"),
|
| 60 |
html.Div(id='log-output', style={'whiteSpace': 'pre-line'}),
|
| 61 |
], fluid=True)
|
|
@@ -63,17 +58,22 @@ app.layout = dbc.Container([
|
|
| 63 |
@callback(
|
| 64 |
Output('pdf-name', 'children'),
|
| 65 |
Output('split-button', 'disabled'),
|
|
|
|
| 66 |
Input('upload-pdf', 'contents'),
|
| 67 |
Input('upload-pdf', 'filename')
|
| 68 |
)
|
| 69 |
def update_output(contents, filename):
|
| 70 |
if contents is not None:
|
| 71 |
logger.info(f"PDF uploaded: {filename}")
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
@callback(
|
| 76 |
-
Output('ranges-container', 'children'),
|
| 77 |
Input('add-range', 'n_clicks'),
|
| 78 |
State('ranges-container', 'children'),
|
| 79 |
prevent_initial_call=True
|
|
@@ -105,6 +105,7 @@ def remove_range(n_clicks, existing_ranges):
|
|
| 105 |
@callback(
|
| 106 |
Output('progress-bar', 'value'),
|
| 107 |
Output('log-output', 'children'),
|
|
|
|
| 108 |
Input('split-button', 'n_clicks'),
|
| 109 |
State('upload-pdf', 'contents'),
|
| 110 |
State('upload-pdf', 'filename'),
|
|
@@ -126,7 +127,7 @@ def split_pdf(n_clicks, contents, filename, ranges):
|
|
| 126 |
thread = Thread(target=process_pdf, args=(contents, filename, ranges))
|
| 127 |
thread.start()
|
| 128 |
|
| 129 |
-
return 0, "PDF splitting process started. Check console for detailed logs."
|
| 130 |
|
| 131 |
def process_pdf(contents, filename, ranges):
|
| 132 |
global progress, generated_file
|
|
@@ -167,6 +168,7 @@ def process_pdf(contents, filename, ranges):
|
|
| 167 |
@callback(
|
| 168 |
Output('progress-bar', 'value', allow_duplicate=True),
|
| 169 |
Output('log-output', 'children', allow_duplicate=True),
|
|
|
|
| 170 |
Input('progress-bar', 'value'),
|
| 171 |
prevent_initial_call=True
|
| 172 |
)
|
|
@@ -174,12 +176,12 @@ def update_progress(value):
|
|
| 174 |
global progress
|
| 175 |
if progress == 100:
|
| 176 |
logger.info("PDF splitting completed")
|
| 177 |
-
return 100, "PDF splitting completed. Click 'Download ZIP' to get your files."
|
| 178 |
elif progress == -1:
|
| 179 |
logger.error("PDF splitting failed")
|
| 180 |
-
return 0, "Error occurred during PDF splitting. Check console for details."
|
| 181 |
else:
|
| 182 |
-
return progress, f"Processing... {progress:.0f}% complete"
|
| 183 |
|
| 184 |
@callback(
|
| 185 |
Output("download-zip", "data"),
|
|
|
|
| 41 |
},
|
| 42 |
multiple=False
|
| 43 |
),
|
| 44 |
+
dbc.Spinner(html.Div(id='pdf-name'), color="primary", type="grow"),
|
| 45 |
dbc.Card([
|
| 46 |
dbc.CardBody([
|
| 47 |
+
html.Div(id='ranges-container', children=[]),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
dbc.Button("Add Range", id='add-range', color="secondary", className="mt-2"),
|
| 49 |
])
|
| 50 |
], className="my-3"),
|
| 51 |
dbc.Button("Split PDF", id='split-button', color="primary", className="mt-3", disabled=True),
|
| 52 |
dbc.Progress(id='progress-bar', className="my-3"),
|
| 53 |
+
dbc.Button("Download ZIP", id='download-button', color="success", className="mt-3", disabled=True),
|
| 54 |
dcc.Download(id="download-zip"),
|
| 55 |
html.Div(id='log-output', style={'whiteSpace': 'pre-line'}),
|
| 56 |
], fluid=True)
|
|
|
|
| 58 |
@callback(
|
| 59 |
Output('pdf-name', 'children'),
|
| 60 |
Output('split-button', 'disabled'),
|
| 61 |
+
Output('ranges-container', 'children'),
|
| 62 |
Input('upload-pdf', 'contents'),
|
| 63 |
Input('upload-pdf', 'filename')
|
| 64 |
)
|
| 65 |
def update_output(contents, filename):
|
| 66 |
if contents is not None:
|
| 67 |
logger.info(f"PDF uploaded: {filename}")
|
| 68 |
+
initial_range = dbc.Row([
|
| 69 |
+
dbc.Col(dbc.Input(id={'type': 'range-input', 'index': 0}, type='text', placeholder='Enter page range (e.g., 1-3)'), width=10),
|
| 70 |
+
dbc.Col(dbc.Button("Remove", id={'type': 'remove-range', 'index': 0}, color="danger", size="sm"), width=2),
|
| 71 |
+
], className="mb-2")
|
| 72 |
+
return html.Div(f"Uploaded: {filename}"), False, [initial_range]
|
| 73 |
+
return "", True, []
|
| 74 |
|
| 75 |
@callback(
|
| 76 |
+
Output('ranges-container', 'children', allow_duplicate=True),
|
| 77 |
Input('add-range', 'n_clicks'),
|
| 78 |
State('ranges-container', 'children'),
|
| 79 |
prevent_initial_call=True
|
|
|
|
| 105 |
@callback(
|
| 106 |
Output('progress-bar', 'value'),
|
| 107 |
Output('log-output', 'children'),
|
| 108 |
+
Output('download-button', 'disabled'),
|
| 109 |
Input('split-button', 'n_clicks'),
|
| 110 |
State('upload-pdf', 'contents'),
|
| 111 |
State('upload-pdf', 'filename'),
|
|
|
|
| 127 |
thread = Thread(target=process_pdf, args=(contents, filename, ranges))
|
| 128 |
thread.start()
|
| 129 |
|
| 130 |
+
return 0, "PDF splitting process started. Check console for detailed logs.", True
|
| 131 |
|
| 132 |
def process_pdf(contents, filename, ranges):
|
| 133 |
global progress, generated_file
|
|
|
|
| 168 |
@callback(
|
| 169 |
Output('progress-bar', 'value', allow_duplicate=True),
|
| 170 |
Output('log-output', 'children', allow_duplicate=True),
|
| 171 |
+
Output('download-button', 'disabled', allow_duplicate=True),
|
| 172 |
Input('progress-bar', 'value'),
|
| 173 |
prevent_initial_call=True
|
| 174 |
)
|
|
|
|
| 176 |
global progress
|
| 177 |
if progress == 100:
|
| 178 |
logger.info("PDF splitting completed")
|
| 179 |
+
return 100, "PDF splitting completed. Click 'Download ZIP' to get your files.", False
|
| 180 |
elif progress == -1:
|
| 181 |
logger.error("PDF splitting failed")
|
| 182 |
+
return 0, "Error occurred during PDF splitting. Check console for details.", True
|
| 183 |
else:
|
| 184 |
+
return progress, f"Processing... {progress:.0f}% complete", True
|
| 185 |
|
| 186 |
@callback(
|
| 187 |
Output("download-zip", "data"),
|