bluenevus commited on
Commit
bb44c88
·
1 Parent(s): 6f89173

Update app.py via AI Editor

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -153,7 +153,6 @@ app.layout = dbc.Container(
153
  [
154
  dcc.Store(id='session-id-store', storage_type='session'),
155
  dcc.Store(id='session-store', storage_type='session'),
156
- html.Div(id='dummy-div', style={'display': 'none'}),
157
  dbc.Row(
158
  [
159
  dbc.Col(
@@ -203,17 +202,39 @@ app.layout = dbc.Container(
203
  className="p-4"
204
  )
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
  @app.callback(
207
  Output('session-id-store', 'data'),
208
- Input('dummy-div', 'children'),
209
  prevent_initial_call=False
210
  )
211
- def ensure_session_id(_):
212
- sid = get_session_id_from_cookie()
213
- if not sid:
 
 
 
 
 
 
 
 
 
 
214
  sid = str(uuid.uuid4())
215
- flask.g.session_id = sid
216
- return sid
217
 
218
  @app.callback(
219
  Output('file-info', 'children'),
@@ -232,7 +253,6 @@ def ensure_session_id(_):
232
  def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, session_data, session_id):
233
  trigger = ctx.triggered_id
234
 
235
- # Always use session id from store
236
  if not session_id:
237
  session_id = str(uuid.uuid4())
238
  flask.g.session_id = session_id
@@ -397,26 +417,6 @@ def download_zip_file(session_id, filename):
397
  logging.error(f"ZIP file not found for download: {file_path}")
398
  return "File not found", 404
399
 
400
- @app.callback(
401
- Output('dummy-div', 'children'),
402
- Input('session-id-store', 'data'),
403
- prevent_initial_call=False
404
- )
405
- def set_cookie_on_load(session_id):
406
- if not session_id:
407
- session_id = str(uuid.uuid4())
408
- resp = flask.make_response("")
409
- resp.set_cookie('session-id', session_id, max_age=60*60*24*3)
410
- flask.g.session_id = session_id
411
- return ""
412
-
413
- @app.server.before_request
414
- def persist_session_cookie():
415
- session_id = get_session_id_from_cookie()
416
- if not session_id:
417
- session_id = str(uuid.uuid4())
418
- flask.g.session_id = session_id
419
-
420
  if __name__ == '__main__':
421
  print("Starting the Dash application...")
422
  app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)
 
153
  [
154
  dcc.Store(id='session-id-store', storage_type='session'),
155
  dcc.Store(id='session-store', storage_type='session'),
 
156
  dbc.Row(
157
  [
158
  dbc.Col(
 
202
  className="p-4"
203
  )
204
 
205
+ @app.server.before_request
206
+ def persist_session_cookie():
207
+ session_id = flask.request.cookies.get('session-id')
208
+ if not session_id:
209
+ session_id = str(uuid.uuid4())
210
+ resp = flask.make_response("")
211
+ resp.set_cookie('session-id', session_id, max_age=60*60*24*3)
212
+ flask.g.session_id = session_id
213
+ # Attach the response only if needed
214
+ flask.request.session_id_set = session_id
215
+ else:
216
+ flask.g.session_id = session_id
217
+
218
  @app.callback(
219
  Output('session-id-store', 'data'),
220
+ Input('session-id-store', 'data'),
221
  prevent_initial_call=False
222
  )
223
+ def ensure_session_id(session_id):
224
+ # On first load, set session-id-store from cookie or generate new
225
+ try:
226
+ sid = session_id
227
+ if not sid:
228
+ if hasattr(flask.request, 'session_id_set'):
229
+ sid = flask.request.session_id_set
230
+ else:
231
+ sid = get_or_create_session_id()
232
+ flask.g.session_id = sid
233
+ return sid
234
+ except Exception as e:
235
+ logging.error(f"Error ensuring session id: {e}")
236
  sid = str(uuid.uuid4())
237
+ return sid
 
238
 
239
  @app.callback(
240
  Output('file-info', 'children'),
 
253
  def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, session_data, session_id):
254
  trigger = ctx.triggered_id
255
 
 
256
  if not session_id:
257
  session_id = str(uuid.uuid4())
258
  flask.g.session_id = session_id
 
417
  logging.error(f"ZIP file not found for download: {file_path}")
418
  return "File not found", 404
419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
  if __name__ == '__main__':
421
  print("Starting the Dash application...")
422
  app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)