bluenevus commited on
Commit
a2fd8f1
·
verified ·
1 Parent(s): e144e58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -37
app.py CHANGED
@@ -65,6 +65,7 @@ def generate_url_list(links):
65
 
66
  app.layout = dbc.Container([
67
  dcc.Store(id='add-url-status', data=''),
 
68
  dbc.Modal([
69
  dbc.ModalHeader("Add New URL"),
70
  dbc.ModalBody([
@@ -109,35 +110,30 @@ def toggle_modal(n1, n2, is_open):
109
  return is_open, dash.no_update, dash.no_update
110
 
111
  @callback(
112
- Output('url-list', 'children'),
113
- Input('url-list', 'id')
114
- )
115
- def initialize_url_list(id):
116
- print("Initializing URL list") # Debug print
117
- links = get_links()
118
- return generate_url_list(links)
119
-
120
- @callback(
121
- [Output('url-list', 'children', allow_duplicate=True),
122
  Output('add-url-feedback', 'children'),
123
  Output('add-url-status', 'data')],
124
- [Input('add-url-button', 'n_clicks'),
125
- Input('add-url-status', 'data'),
126
  Input({'type': 'delete-button', 'index': dash.ALL}, 'n_clicks')],
127
  [State('new-url-name', 'value'),
128
  State('new-url-link', 'value')],
129
- prevent_initial_call=True
130
  )
131
- def update_url_list(add_clicks, status, delete_clicks, new_name, new_link):
132
  ctx = dash.callback_context
133
  if not ctx.triggered:
134
  raise dash.exceptions.PreventUpdate
135
 
136
  triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
137
  print(f"Triggered by: {triggered_id}") # Debug print
138
- print(f"New name: {new_name}, New link: {new_link}") # Debug print
139
 
140
- if triggered_id == 'add-url-button' and new_name and new_link:
 
 
 
 
 
141
  try:
142
  conn = get_db_connection()
143
  c = conn.cursor()
@@ -169,25 +165,12 @@ def update_url_list(add_clicks, status, delete_clicks, new_name, new_link):
169
  status = 'error'
170
  finally:
171
  conn.close()
172
- else:
173
- feedback = None
174
- status = ''
175
 
176
  links = get_links()
177
  url_list = generate_url_list(links)
178
 
179
  return url_list, feedback, status
180
 
181
- @callback(
182
- [Output('new-url-name', 'value'),
183
- Output('new-url-link', 'value')],
184
- Input('add-url-status', 'data')
185
- )
186
- def clear_inputs(status):
187
- if status == 'added':
188
- return '', ''
189
- return dash.no_update, dash.no_update
190
-
191
  @callback(
192
  Output('content-iframe', 'src'),
193
  Input({'type': 'url-button', 'index': dash.ALL}, 'n_clicks')
@@ -208,11 +191,4 @@ def update_iframe(n_clicks):
208
  return result['url']
209
  except Exception as e:
210
  print(f"Error loading URL: {e}")
211
- finally:
212
- conn.close()
213
- return dash.no_update
214
-
215
- if __name__ == '__main__':
216
- print("Starting the Dash application...")
217
- app.run(debug=True, host='0.0.0.0', port=7860)
218
- print("Dash application has finished running.")
 
65
 
66
  app.layout = dbc.Container([
67
  dcc.Store(id='add-url-status', data=''),
68
+ dcc.Store(id='trigger-load', data='load'), # New store to trigger initial load
69
  dbc.Modal([
70
  dbc.ModalHeader("Add New URL"),
71
  dbc.ModalBody([
 
110
  return is_open, dash.no_update, dash.no_update
111
 
112
  @callback(
113
+ [Output('url-list', 'children'),
 
 
 
 
 
 
 
 
 
114
  Output('add-url-feedback', 'children'),
115
  Output('add-url-status', 'data')],
116
+ [Input('trigger-load', 'data'),
117
+ Input('add-url-button', 'n_clicks'),
118
  Input({'type': 'delete-button', 'index': dash.ALL}, 'n_clicks')],
119
  [State('new-url-name', 'value'),
120
  State('new-url-link', 'value')],
121
+ prevent_initial_call=False
122
  )
123
+ def update_url_list(trigger_load, add_clicks, delete_clicks, new_name, new_link):
124
  ctx = dash.callback_context
125
  if not ctx.triggered:
126
  raise dash.exceptions.PreventUpdate
127
 
128
  triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
129
  print(f"Triggered by: {triggered_id}") # Debug print
 
130
 
131
+ feedback = None
132
+ status = ''
133
+
134
+ if triggered_id == 'trigger-load':
135
+ print("Initial load triggered")
136
+ elif triggered_id == 'add-url-button' and new_name and new_link:
137
  try:
138
  conn = get_db_connection()
139
  c = conn.cursor()
 
165
  status = 'error'
166
  finally:
167
  conn.close()
 
 
 
168
 
169
  links = get_links()
170
  url_list = generate_url_list(links)
171
 
172
  return url_list, feedback, status
173
 
 
 
 
 
 
 
 
 
 
 
174
  @callback(
175
  Output('content-iframe', 'src'),
176
  Input({'type': 'url-button', 'index': dash.ALL}, 'n_clicks')
 
191
  return result['url']
192
  except Exception as e:
193
  print(f"Error loading URL: {e}")
194
+ finally: