Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from dash.exceptions import PreventUpdate
|
|
| 11 |
import PyPDF2
|
| 12 |
import docx
|
| 13 |
import chardet
|
|
|
|
| 14 |
|
| 15 |
# Initialize the Dash app
|
| 16 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
|
@@ -130,6 +131,7 @@ def parse_file_content(contents, filename):
|
|
| 130 |
State('upload-files', 'filename'),
|
| 131 |
State('file-list', 'children')
|
| 132 |
)
|
|
|
|
| 133 |
def update_output(list_of_contents, list_of_names, existing_files):
|
| 134 |
global uploaded_files
|
| 135 |
if list_of_contents is not None:
|
|
@@ -138,7 +140,7 @@ def update_output(list_of_contents, list_of_names, existing_files):
|
|
| 138 |
file_content = parse_file_content(content, name)
|
| 139 |
uploaded_files[name] = file_content
|
| 140 |
new_files.append(html.Div([
|
| 141 |
-
html.Button('×', id={'type': 'remove-file', 'index': name}, style={'marginRight': '5px', 'fontSize': '10px'}),
|
| 142 |
html.Span(name)
|
| 143 |
]))
|
| 144 |
if existing_files is None:
|
|
@@ -155,9 +157,12 @@ def update_output(list_of_contents, list_of_names, existing_files):
|
|
| 155 |
def remove_file(n_clicks, existing_files):
|
| 156 |
global uploaded_files
|
| 157 |
ctx = dash.callback_context
|
| 158 |
-
if not ctx.triggered:
|
| 159 |
raise PreventUpdate
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
| 161 |
uploaded_files.pop(removed_file, None)
|
| 162 |
return [file for file in existing_files if file['props']['children'][1]['props']['children'] != removed_file]
|
| 163 |
|
|
|
|
| 11 |
import PyPDF2
|
| 12 |
import docx
|
| 13 |
import chardet
|
| 14 |
+
import json
|
| 15 |
|
| 16 |
# Initialize the Dash app
|
| 17 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
|
|
|
| 131 |
State('upload-files', 'filename'),
|
| 132 |
State('file-list', 'children')
|
| 133 |
)
|
| 134 |
+
# Update the file list creation in the update_output function
|
| 135 |
def update_output(list_of_contents, list_of_names, existing_files):
|
| 136 |
global uploaded_files
|
| 137 |
if list_of_contents is not None:
|
|
|
|
| 140 |
file_content = parse_file_content(content, name)
|
| 141 |
uploaded_files[name] = file_content
|
| 142 |
new_files.append(html.Div([
|
| 143 |
+
html.Button('×', id={'type': 'remove-file', 'index': name}, n_clicks=0, style={'marginRight': '5px', 'fontSize': '10px'}),
|
| 144 |
html.Span(name)
|
| 145 |
]))
|
| 146 |
if existing_files is None:
|
|
|
|
| 157 |
def remove_file(n_clicks, existing_files):
|
| 158 |
global uploaded_files
|
| 159 |
ctx = dash.callback_context
|
| 160 |
+
if not ctx.triggered or not any(n_clicks):
|
| 161 |
raise PreventUpdate
|
| 162 |
+
|
| 163 |
+
triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
| 164 |
+
removed_file = json.loads(triggered_id)['index']
|
| 165 |
+
|
| 166 |
uploaded_files.pop(removed_file, None)
|
| 167 |
return [file for file in existing_files if file['props']['children'][1]['props']['children'] != removed_file]
|
| 168 |
|