$P@D$3RV£R commited on
Commit
edf8bcb
·
1 Parent(s): 6fb025f

Fix blank screen: initialize variables before try block and add global error handler

Browse files
Files changed (1) hide show
  1. app.py +51 -4
app.py CHANGED
@@ -249,10 +249,36 @@ def index():
249
  track_visit()
250
  return redirect(url_for('tagger'))
251
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  @app.route('/tagger')
253
  def tagger():
254
- # Track visit
255
- track_visit()
 
 
 
 
256
 
257
  # Check if dataset was loaded successfully
258
  folder_sets = app.config.get("FOLDER_SETS", [])
@@ -301,9 +327,15 @@ def tagger():
301
  app.config["IMAGE_SET_INDEX"] = 0
302
  print("Reached end of folders, looping back to first folder")
303
 
 
 
 
 
 
 
 
304
  # Safely access current folder set
305
  try:
306
- directory = app.config.get('IMAGES', '')
307
  current_folder_set = folder_sets[app.config["HEAD"]]
308
 
309
  # Validate folder set structure
@@ -329,7 +361,6 @@ def tagger():
329
  app.config["IMAGE_SET_INDEX"] = 0
330
 
331
  # Get current set of 3 images (all with same file ID prefix)
332
- current_images = []
333
  if image_set_index < max_sets:
334
  current_set = image_sets[image_set_index]
335
  if not isinstance(current_set, dict):
@@ -369,6 +400,22 @@ def tagger():
369
  </html>
370
  """, 500
371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372
  labels = app.config["LABELS"]
373
  has_prev_folder = app.config["HEAD"] > 0
374
  has_next_folder = app.config["HEAD"] + 1 < len(app.config["FOLDER_SETS"])
 
249
  track_visit()
250
  return redirect(url_for('tagger'))
251
 
252
+ @app.errorhandler(Exception)
253
+ def handle_exception(e):
254
+ """Global error handler to prevent blank screens"""
255
+ print(f"Unhandled exception: {e}")
256
+ import traceback
257
+ traceback.print_exc()
258
+ return f"""
259
+ <!DOCTYPE html>
260
+ <html>
261
+ <head>
262
+ <title>Application Error</title>
263
+ <meta charset="UTF-8">
264
+ </head>
265
+ <body>
266
+ <h1>Application Error</h1>
267
+ <p>An unexpected error occurred.</p>
268
+ <p>Error: {str(e)}</p>
269
+ <p>Please check the Space logs for more details.</p>
270
+ </body>
271
+ </html>
272
+ """, 500
273
+
274
  @app.route('/tagger')
275
  def tagger():
276
+ try:
277
+ # Track visit
278
+ track_visit()
279
+ except Exception as e:
280
+ print(f"Error in track_visit: {e}")
281
+ # Continue even if tracking fails
282
 
283
  # Check if dataset was loaded successfully
284
  folder_sets = app.config.get("FOLDER_SETS", [])
 
327
  app.config["IMAGE_SET_INDEX"] = 0
328
  print("Reached end of folders, looping back to first folder")
329
 
330
+ # Initialize variables with defaults
331
+ directory = app.config.get('IMAGES', '')
332
+ current_folder_set = None
333
+ image_set_index = 0
334
+ max_sets = 0
335
+ current_images = []
336
+
337
  # Safely access current folder set
338
  try:
 
339
  current_folder_set = folder_sets[app.config["HEAD"]]
340
 
341
  # Validate folder set structure
 
361
  app.config["IMAGE_SET_INDEX"] = 0
362
 
363
  # Get current set of 3 images (all with same file ID prefix)
 
364
  if image_set_index < max_sets:
365
  current_set = image_sets[image_set_index]
366
  if not isinstance(current_set, dict):
 
400
  </html>
401
  """, 500
402
 
403
+ # Ensure we have valid data before proceeding
404
+ if current_folder_set is None:
405
+ return f"""
406
+ <!DOCTYPE html>
407
+ <html>
408
+ <head>
409
+ <title>Data Error</title>
410
+ <meta charset="UTF-8">
411
+ </head>
412
+ <body>
413
+ <h1>Data Error</h1>
414
+ <p>Unable to load folder data.</p>
415
+ </body>
416
+ </html>
417
+ """, 500
418
+
419
  labels = app.config["LABELS"]
420
  has_prev_folder = app.config["HEAD"] > 0
421
  has_next_folder = app.config["HEAD"] + 1 < len(app.config["FOLDER_SETS"])