@@ -340,12 +340,35 @@ def market_inspector_page():
340
340
available_attrs = get_perp_market_attributes ()
341
341
342
342
# 3) Let user select which market
343
- selected_market = st .selectbox (
343
+ market_indices = [m .data .market_index for m in markets ]
344
+
345
+ if not market_indices :
346
+ st .warning (f"No { market_type_choice } markets available to display." )
347
+ # Ensure selected_attrs is cleared if no markets are available for the chosen type
348
+ if "selected_attrs" in st .session_state : # Reset selected attributes if market type changes or no markets
349
+ st .session_state .selected_attrs = []
350
+ return # Stop further processing for this render if no markets
351
+
352
+ market_lookup = {m .data .market_index : m for m in markets }
353
+
354
+ # Use a unique key for the selectbox based on market_type_choice
355
+ # to ensure it resets if the options change significantly (e.g., switching market types)
356
+ selectbox_key = f"market_selectbox_{ market_type_choice } "
357
+
358
+ selected_market_index = st .selectbox (
344
359
"Select Market:" ,
345
- markets ,
346
- format_func = display_name ,
360
+ market_indices ,
361
+ format_func = lambda index : display_name (market_lookup [index ]),
362
+ key = selectbox_key
347
363
)
348
364
365
+ selected_market = market_lookup .get (selected_market_index ) # Reassign selected_market
366
+
367
+ if not selected_market :
368
+ # This case should ideally not be hit if market_indices is not empty and selectbox works as expected
369
+ st .error ("Failed to retrieve selected market data. Please ensure a market is selected or try refreshing." )
370
+ return
371
+
349
372
# 4) Let user pick which attributes to show (multi-select)
350
373
st .write ("Select which attributes you would like to see:" )
351
374
@@ -378,18 +401,44 @@ def on_attribute_selection_change():
378
401
# 5) Display results
379
402
if not selected_attrs :
380
403
st .info ("Please select at least one attribute to display." )
381
- with st .expander ("All markets" ):
404
+ with st .expander ("All markets (Serialized Data) " ): # Clarified title
382
405
st .write ("Perp markets:" )
383
- perp_markets = sorted (perp_market_map .values (), key = lambda m : m .data .market_index )
384
- perps = pd .concat ([pd .DataFrame (serialize_perp_market (x .data )).T for x in perp_markets ], axis = 1 )
385
- perps .columns = [m .data .market_index for m in perp_markets ]
386
- st .write (perps )
406
+ perp_markets_list = sorted (perp_market_map .values (), key = lambda m : m .data .market_index )
407
+ if perp_markets_list :
408
+ try :
409
+ perp_data_for_df = [serialize_perp_market (m .data ) for m in perp_markets_list ]
410
+ if perp_data_for_df : # Ensure list is not empty before concat
411
+ df_perp = pd .concat (perp_data_for_df , axis = 0 ).reset_index (drop = True )
412
+ if 'market_index' in df_perp .columns :
413
+ # Ensure market_index is the first column for better readability
414
+ df_perp = df_perp [['market_index' ] + [col for col in df_perp .columns if col != 'market_index' ]]
415
+ st .dataframe (df_perp )
416
+ else :
417
+ st .write ("No data to display for perp markets." )
418
+ except Exception as e :
419
+ st .error (f"Error displaying perp markets table: { e } " )
420
+ st .caption ("Raw data might contain non-serializable fields or other issues." )
421
+ else :
422
+ st .write ("No perp markets found." )
387
423
388
424
st .write ("Spot markets:" )
389
- spot_markets = sorted (spot_market_map .values (), key = lambda m : m .data .market_index )
390
- spots = pd .concat ([pd .DataFrame (serialize_spot_market (x .data )).T for x in spot_markets ], axis = 1 )
391
- spots .columns = [m .data .market_index for m in spot_markets ]
392
- st .write (spots )
425
+ spot_markets_list = sorted (spot_market_map .values (), key = lambda m : m .data .market_index )
426
+ if spot_markets_list :
427
+ try :
428
+ spot_data_for_df = [serialize_spot_market (m .data ) for m in spot_markets_list ]
429
+ if spot_data_for_df : # Ensure list is not empty before concat
430
+ df_spot = pd .concat (spot_data_for_df , axis = 0 ).reset_index (drop = True )
431
+ if 'market_index' in df_spot .columns :
432
+ # Ensure market_index is the first column for better readability
433
+ df_spot = df_spot [['market_index' ] + [col for col in df_spot .columns if col != 'market_index' ]]
434
+ st .dataframe (df_spot )
435
+ else :
436
+ st .write ("No data to display for spot markets." )
437
+ except Exception as e :
438
+ st .error (f"Error displaying spot markets table: { e } " )
439
+ st .caption ("Raw data might contain non-serializable fields or other issues." )
440
+ else :
441
+ st .write ("No spot markets found." )
393
442
return
394
443
395
444
st .write (f"**Market Index:** { selected_market .data .market_index } " )
@@ -440,8 +489,42 @@ def on_attribute_selection_change():
440
489
# Wrap in code block
441
490
st .markdown (f"```\n { formatted_line } \n ```" )
442
491
443
- with st .expander ("All markets" ):
492
+ with st .expander ("All markets (Serialized Data) " ): # Clarified title and reused logic
444
493
st .write ("Perp markets:" )
445
- st .write (pd .DataFrame (sorted (perp_market_map .values (), key = lambda m : m .data .market_index )))
494
+ # Use a different variable name to avoid conflicts if any part of the script is re-run in a weird way
495
+ perp_markets_list_bottom = sorted (perp_market_map .values (), key = lambda m : m .data .market_index )
496
+ if perp_markets_list_bottom :
497
+ try :
498
+ perp_data_for_df_bottom = [serialize_perp_market (m .data ) for m in perp_markets_list_bottom ]
499
+ if perp_data_for_df_bottom : # Ensure list is not empty before concat
500
+ df_perp_bottom = pd .concat (perp_data_for_df_bottom , axis = 0 ).reset_index (drop = True )
501
+ if 'market_index' in df_perp_bottom .columns :
502
+ # Ensure market_index is the first column
503
+ df_perp_bottom = df_perp_bottom [['market_index' ] + [col for col in df_perp_bottom .columns if col != 'market_index' ]]
504
+ st .dataframe (df_perp_bottom )
505
+ else :
506
+ st .write ("No data to display for perp markets." )
507
+ except Exception as e :
508
+ st .error (f"Error displaying perp markets table (bottom): { e } " )
509
+ st .caption ("Raw data might contain non-serializable fields or other issues." )
510
+ else :
511
+ st .write ("No perp markets found." )
512
+
446
513
st .write ("Spot markets:" )
447
- st .write (pd .DataFrame (sorted (spot_market_map .values (), key = lambda m : m .data .market_index )))
514
+ spot_markets_list_bottom = sorted (spot_market_map .values (), key = lambda m : m .data .market_index )
515
+ if spot_markets_list_bottom :
516
+ try :
517
+ spot_data_for_df_bottom = [serialize_spot_market (m .data ) for m in spot_markets_list_bottom ]
518
+ if spot_data_for_df_bottom : # Ensure list is not empty before concat
519
+ df_spot_bottom = pd .concat (spot_data_for_df_bottom , axis = 0 ).reset_index (drop = True )
520
+ if 'market_index' in df_spot_bottom .columns :
521
+ # Ensure market_index is the first column
522
+ df_spot_bottom = df_spot_bottom [['market_index' ] + [col for col in df_spot_bottom .columns if col != 'market_index' ]]
523
+ st .dataframe (df_spot_bottom )
524
+ else :
525
+ st .write ("No data to display for spot markets." )
526
+ except Exception as e :
527
+ st .error (f"Error displaying spot markets table (bottom): { e } " )
528
+ st .caption ("Raw data might contain non-serializable fields or other issues." )
529
+ else :
530
+ st .write ("No spot markets found." )
0 commit comments