bluenevus commited on
Commit
67a702c
·
verified ·
1 Parent(s): debaec8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
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])
@@ -124,6 +125,32 @@ def parse_file_content(contents, filename):
124
  print(f"Error processing file {filename}: {str(e)}")
125
  return "Error processing file"
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  @app.callback(
128
  Output('file-list', 'children'),
129
  Input('upload-files', 'contents'),
@@ -138,7 +165,8 @@ 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:
@@ -146,21 +174,6 @@ def update_output(list_of_contents, list_of_names, existing_files):
146
  return existing_files + new_files
147
  return existing_files
148
 
149
- @app.callback(
150
- Output('file-list', 'children', allow_duplicate=True),
151
- Input({'type': 'remove-file', 'index': dash.ALL}, 'n_clicks'),
152
- State('file-list', 'children'),
153
- prevent_initial_call=True
154
- )
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
- removed_file = ctx.triggered[0]['prop_id'].split(',')[0].split(':')[-1].strip('}')
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
-
164
  def generate_matrix_with_gpt(matrix_type, file_contents):
165
  prompt = f"""Generate a {matrix_type} based on the following project artifacts:
166
  {' '.join(file_contents)}
 
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])
 
125
  print(f"Error processing file {filename}: {str(e)}")
126
  return "Error processing file"
127
 
128
+ @app.callback(
129
+ Output('file-list', 'children', allow_duplicate=True),
130
+ Input({'type': 'remove-file', 'index': dash.ALL}, 'n_clicks'),
131
+ State('file-list', 'children'),
132
+ prevent_initial_call=True
133
+ )
134
+ def remove_file(n_clicks, existing_files):
135
+ global uploaded_files
136
+ ctx = dash.callback_context
137
+ if not ctx.triggered:
138
+ raise PreventUpdate
139
+
140
+ button_id = ctx.triggered[0]['prop_id'].split('.')[0]
141
+ removed_file = json.loads(button_id)['index']
142
+
143
+ if removed_file in uploaded_files:
144
+ del uploaded_files[removed_file]
145
+
146
+ updated_files = [
147
+ file for file in existing_files
148
+ if file['props']['children'][1]['props']['children'] != removed_file
149
+ ]
150
+
151
+ return updated_files
152
+
153
+ # Modify the update_output function to include the correct button structure
154
  @app.callback(
155
  Output('file-list', 'children'),
156
  Input('upload-files', 'contents'),
 
165
  file_content = parse_file_content(content, name)
166
  uploaded_files[name] = file_content
167
  new_files.append(html.Div([
168
+ html.Button('×', id={'type': 'remove-file', 'index': name},
169
+ style={'marginRight': '5px', 'fontSize': '10px', 'border': 'none', 'background': 'none', 'cursor': 'pointer'}),
170
  html.Span(name)
171
  ]))
172
  if existing_files is None:
 
174
  return existing_files + new_files
175
  return existing_files
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  def generate_matrix_with_gpt(matrix_type, file_contents):
178
  prompt = f"""Generate a {matrix_type} based on the following project artifacts:
179
  {' '.join(file_contents)}