pratikbhavsar commited on
Commit
6dc224a
·
1 Parent(s): 7a48c13

chart loading issue

Browse files
Files changed (1) hide show
  1. tabs/leaderboard.py +154 -155
tabs/leaderboard.py CHANGED
@@ -480,7 +480,6 @@ def filter_leaderboard(df, model_type, category, sort_by):
480
 
481
 
482
  def create_leaderboard_tab(df, CATEGORIES, METHODOLOGY, HEADER_CONTENT, CARDS):
483
- # Improved CSS for responsive chart sizing
484
  chart_container_css = """
485
  <style>
486
  /* Chart container styling */
@@ -542,169 +541,169 @@ def create_leaderboard_tab(df, CATEGORIES, METHODOLOGY, HEADER_CONTENT, CARDS):
542
  </style>
543
  """
544
 
545
- with gr.Tab("Leaderboard"):
546
- gr.HTML(HEADER_CONTENT + CARDS)
547
- gr.HTML(DESCRIPTION_HTML)
548
-
549
- # Add our custom CSS
550
- gr.HTML(chart_container_css)
551
-
552
- # Filters row
553
- with gr.Row(equal_height=True):
554
- with gr.Column(scale=1):
555
- model_type = gr.Dropdown(
556
- choices=["All"] + df["Model Type"].unique().tolist(),
557
- value="All",
558
- label="Model Type",
559
- )
560
- with gr.Column(scale=1):
561
- category = gr.Dropdown(
562
- choices=list(CATEGORIES.keys()),
563
- value=list(CATEGORIES.keys())[0],
564
- label="Category",
565
- )
566
- with gr.Column(scale=1):
567
- sort_by = gr.Radio(
568
- choices=["Performance", "Cost"],
569
- value="Performance",
570
- label="Sort by",
571
- )
572
-
573
- # Content
574
- output = gr.HTML()
575
-
576
- # Performance chart - don't specify height in HTML
577
- with gr.Row():
578
- with gr.Column():
579
- gr.HTML('<div class="chart-container">')
580
- plot1 = gr.Plot(elem_id="plot1")
581
- gr.HTML("</div>")
582
-
583
- # Cost performance chart - don't specify height in HTML
584
- with gr.Row():
585
- with gr.Column():
586
- gr.HTML('<div class="chart-container">')
587
- plot2 = gr.Plot(elem_id="plot2")
588
- gr.HTML("</div>")
589
-
590
- gr.HTML(
591
- """<div class="note-box">
592
- <p style="margin: 0; font-size: 1em;">
593
- Note: API pricing for sorting by cost uses a 3-to-1 input/output ratio calculation.
594
- </p>
595
- </div>"""
596
- )
597
 
598
- gr.HTML(METHODOLOGY)
599
-
600
- # Enhanced resize script - improved to be more responsive
601
- resize_js = """
602
- <script>
603
- // Improved function to handle responsive Plotly charts
604
- function resizePlots() {
605
- // Find all plot containers
606
- const plotContainers = document.querySelectorAll('.js-plotly-plot');
607
- if (!plotContainers.length) {
608
- // If containers aren't ready yet, retry shortly
609
- setTimeout(resizePlots, 100);
610
- return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
611
  }
612
 
613
- // Get the available width for the container
614
- const containerWidth = document.querySelector('.chart-container').offsetWidth;
 
615
 
616
- plotContainers.forEach(container => {
617
- // Calculate appropriate dimensions based on container width
618
- let containerHeight;
619
-
620
- // Different height calculation based on chart type
621
- if (container.id.includes('plot1')) {
622
- // Performance chart - use sizing from reference code
623
- const barCount = container.querySelectorAll('.bars .point').length || 20; // Default if can't detect
624
- // Convert from matplotlib sizing approach: height = max(8, len(df_sorted) * 0.8) in inches * pixels per inch
625
- const heightInInches = Math.max(8, barCount * 0.8);
626
- containerHeight = heightInInches * 80; // Convert inches to pixels (approx)
627
- } else {
628
- // Cost chart - use fixed size from reference code (12x8 inches)
629
- containerHeight = 640; // 8 inches * 80 pixels per inch
630
- // Keep width proportional to container up to max width
631
- const maxWidth = 960; // 12 inches * 80 pixels per inch
632
- container.style.maxWidth = maxWidth + 'px';
633
- }
634
-
635
- // Apply dimensions
636
- container.style.width = '100%';
637
- container.style.height = containerHeight + 'px';
638
-
639
- // Find and resize the SVG elements
640
- const svgElements = container.querySelectorAll('svg');
641
- svgElements.forEach(svg => {
642
- svg.style.width = '100%';
643
- svg.style.height = containerHeight + 'px';
644
- });
645
-
646
- // Find the main SVG container and resize it
647
- const svgContainer = container.querySelector('.svg-container');
648
- if (svgContainer) {
649
- svgContainer.style.width = '100%';
650
- svgContainer.style.height = containerHeight + 'px';
651
- }
652
  });
653
 
654
- // Trigger window resize to make Plotly redraw
655
- window.dispatchEvent(new Event('resize'));
656
- }
 
 
 
 
657
 
658
- // Functions to run when content changes or window resizes
659
- function setupResizeHandlers() {
660
- // Initial resize
 
 
 
 
 
 
 
 
661
  resizePlots();
662
-
663
- // Handle window resize
664
- window.addEventListener('resize', function() {
665
- resizePlots();
666
- });
667
-
668
- // Set up a mutation observer to detect when plots are added/changed
669
- const observer = new MutationObserver(function(mutations) {
670
- mutations.forEach(function(mutation) {
671
- if (mutation.addedNodes.length ||
672
- mutation.type === 'attributes' &&
673
- mutation.target.classList.contains('js-plotly-plot')) {
674
- resizePlots();
675
- }
676
- });
677
- });
678
-
679
- // Observe the entire document for changes
680
- observer.observe(document.body, {
681
- childList: true,
682
- subtree: true,
683
- attributes: true,
684
- attributeFilter: ['style', 'class']
685
- });
686
- }
687
 
688
- // Run when DOM is fully loaded
689
- if (document.readyState === 'loading') {
690
- document.addEventListener('DOMContentLoaded', setupResizeHandlers);
691
- } else {
692
- setupResizeHandlers();
693
- }
 
 
 
 
694
 
695
- // Also resize periodically for a bit after initial load to ensure everything renders properly
696
- for (let i = 1; i <= 10; i++) {
697
- setTimeout(resizePlots, i * 500);
698
- }
699
- </script>
700
- """
701
- gr.HTML(resize_js)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
702
 
703
- for input_comp in [model_type, category, sort_by]:
704
- input_comp.change(
705
- fn=lambda m, c, s: filter_leaderboard(df, m, c, s),
706
- inputs=[model_type, category, sort_by],
707
- outputs=[output, plot1, plot2],
708
- )
709
 
710
- return output, plot1, plot2
 
480
 
481
 
482
  def create_leaderboard_tab(df, CATEGORIES, METHODOLOGY, HEADER_CONTENT, CARDS):
 
483
  chart_container_css = """
484
  <style>
485
  /* Chart container styling */
 
541
  </style>
542
  """
543
 
544
+ # Start content directly
545
+ gr.HTML(HEADER_CONTENT + CARDS)
546
+ gr.HTML(DESCRIPTION_HTML)
547
+
548
+ # Add our custom CSS
549
+ gr.HTML(chart_container_css)
550
+
551
+ # Filters row
552
+ with gr.Row(equal_height=True):
553
+ with gr.Column(scale=1):
554
+ model_type = gr.Dropdown(
555
+ choices=["All"] + df["Model Type"].unique().tolist(),
556
+ value="All",
557
+ label="Model Type",
558
+ )
559
+ with gr.Column(scale=1):
560
+ category = gr.Dropdown(
561
+ choices=list(CATEGORIES.keys()),
562
+ value=list(CATEGORIES.keys())[0],
563
+ label="Category",
564
+ )
565
+ with gr.Column(scale=1):
566
+ sort_by = gr.Radio(
567
+ choices=["Performance", "Cost"],
568
+ value="Performance",
569
+ label="Sort by",
570
+ )
571
+
572
+ # Content
573
+ output = gr.HTML()
574
+
575
+ # Performance chart - don't specify height in HTML
576
+ with gr.Row():
577
+ with gr.Column():
578
+ gr.HTML('<div class="chart-container">')
579
+ plot1 = gr.Plot(elem_id="plot1")
580
+ gr.HTML("</div>")
581
+
582
+ # Cost performance chart - don't specify height in HTML
583
+ with gr.Row():
584
+ with gr.Column():
585
+ gr.HTML('<div class="chart-container">')
586
+ plot2 = gr.Plot(elem_id="plot2")
587
+ gr.HTML("</div>")
588
+
589
+ gr.HTML(
590
+ """<div class="note-box">
591
+ <p style="margin: 0; font-size: 1em;">
592
+ Note: API pricing for sorting by cost uses a 3-to-1 input/output ratio calculation.
593
+ </p>
594
+ </div>"""
595
+ )
596
 
597
+ gr.HTML(METHODOLOGY)
598
+
599
+ # Enhanced resize script - improved to be more responsive
600
+ resize_js = """
601
+ <script>
602
+ // Improved function to handle responsive Plotly charts
603
+ function resizePlots() {
604
+ // Find all plot containers
605
+ const plotContainers = document.querySelectorAll('.js-plotly-plot');
606
+ if (!plotContainers.length) {
607
+ // If containers aren't ready yet, retry shortly
608
+ setTimeout(resizePlots, 100);
609
+ return;
610
+ }
611
+
612
+ // Get the available width for the container
613
+ const containerWidth = document.querySelector('.chart-container').offsetWidth;
614
+
615
+ plotContainers.forEach(container => {
616
+ // Calculate appropriate dimensions based on container width
617
+ let containerHeight;
618
+
619
+ // Different height calculation based on chart type
620
+ if (container.id.includes('plot1')) {
621
+ // Performance chart - use sizing from reference code
622
+ const barCount = container.querySelectorAll('.bars .point').length || 20; // Default if can't detect
623
+ // Convert from matplotlib sizing approach: height = max(8, len(df_sorted) * 0.8) in inches * pixels per inch
624
+ const heightInInches = Math.max(8, barCount * 0.8);
625
+ containerHeight = heightInInches * 80; // Convert inches to pixels (approx)
626
+ } else {
627
+ // Cost chart - use fixed size from reference code (12x8 inches)
628
+ containerHeight = 640; // 8 inches * 80 pixels per inch
629
+ // Keep width proportional to container up to max width
630
+ const maxWidth = 960; // 12 inches * 80 pixels per inch
631
+ container.style.maxWidth = maxWidth + 'px';
632
  }
633
 
634
+ // Apply dimensions
635
+ container.style.width = '100%';
636
+ container.style.height = containerHeight + 'px';
637
 
638
+ // Find and resize the SVG elements
639
+ const svgElements = container.querySelectorAll('svg');
640
+ svgElements.forEach(svg => {
641
+ svg.style.width = '100%';
642
+ svg.style.height = containerHeight + 'px';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
643
  });
644
 
645
+ // Find the main SVG container and resize it
646
+ const svgContainer = container.querySelector('.svg-container');
647
+ if (svgContainer) {
648
+ svgContainer.style.width = '100%';
649
+ svgContainer.style.height = containerHeight + 'px';
650
+ }
651
+ });
652
 
653
+ // Trigger window resize to make Plotly redraw
654
+ window.dispatchEvent(new Event('resize'));
655
+ }
656
+
657
+ // Functions to run when content changes or window resizes
658
+ function setupResizeHandlers() {
659
+ // Initial resize
660
+ resizePlots();
661
+
662
+ // Handle window resize
663
+ window.addEventListener('resize', function() {
664
  resizePlots();
665
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
666
 
667
+ // Set up a mutation observer to detect when plots are added/changed
668
+ const observer = new MutationObserver(function(mutations) {
669
+ mutations.forEach(function(mutation) {
670
+ if (mutation.addedNodes.length ||
671
+ mutation.type === 'attributes' &&
672
+ mutation.target.classList.contains('js-plotly-plot')) {
673
+ resizePlots();
674
+ }
675
+ });
676
+ });
677
 
678
+ // Observe the entire document for changes
679
+ observer.observe(document.body, {
680
+ childList: true,
681
+ subtree: true,
682
+ attributes: true,
683
+ attributeFilter: ['style', 'class']
684
+ });
685
+ }
686
+
687
+ // Run when DOM is fully loaded
688
+ if (document.readyState === 'loading') {
689
+ document.addEventListener('DOMContentLoaded', setupResizeHandlers);
690
+ } else {
691
+ setupResizeHandlers();
692
+ }
693
+
694
+ // Also resize periodically for a bit after initial load to ensure everything renders properly
695
+ for (let i = 1; i <= 10; i++) {
696
+ setTimeout(resizePlots, i * 500);
697
+ }
698
+ </script>
699
+ """
700
+ gr.HTML(resize_js)
701
 
702
+ for input_comp in [model_type, category, sort_by]:
703
+ input_comp.change(
704
+ fn=lambda m, c, s: filter_leaderboard(df, m, c, s),
705
+ inputs=[model_type, category, sort_by],
706
+ outputs=[output, plot1, plot2],
707
+ )
708
 
709
+ return output, plot1, plot2