openhands commited on
Commit
8980c87
·
1 Parent(s): cb0a3ed

Add dark mode support for Plotly chart backgrounds

Browse files

Use JavaScript to dynamically update Plotly chart backgrounds when
dark mode is toggled. Sets paper_bgcolor, plot_bgcolor, font color,
and grid colors appropriately for dark/light modes.

Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -135,47 +135,51 @@ const tooltipInterval = setInterval(() => {
135
  </script>
136
  """
137
 
138
- # JavaScript to swap OpenHands logos based on dark mode
139
- logo_swap_script = """
140
  <script>
141
- function updateOpenHandsLogos() {
142
  const isDark = document.body.classList.contains('dark');
143
 
144
- // Find all Plotly chart images
145
- const images = document.querySelectorAll('.js-plotly-plot image');
 
 
 
 
 
 
 
 
 
 
 
146
 
 
 
147
  images.forEach(img => {
148
  const href = img.getAttribute('href') || img.getAttribute('xlink:href') || '';
149
-
150
- // Check if this is an OpenHands logo
151
  if (href.includes('openhands=lightlogo')) {
152
- // Light logo - show in light mode, hide in dark mode
153
  img.style.display = isDark ? 'none' : '';
154
  } else if (href.includes('openhands=darklogo')) {
155
- // Dark logo - show in dark mode, hide in light mode
156
  img.style.display = isDark ? '' : 'none';
157
  }
158
  });
159
  }
160
 
161
- // Run on page load
162
  document.addEventListener('DOMContentLoaded', () => {
163
- // Initial update after a short delay to let Plotly render
164
- setTimeout(updateOpenHandsLogos, 500);
165
 
166
- // Watch for dark mode changes using MutationObserver on body class
167
  const observer = new MutationObserver((mutations) => {
168
  mutations.forEach((mutation) => {
169
  if (mutation.attributeName === 'class') {
170
- updateOpenHandsLogos();
171
  }
172
  });
173
  });
174
-
175
  observer.observe(document.body, { attributes: true });
176
 
177
- // Also periodically check for new charts being added
178
- setInterval(updateOpenHandsLogos, 1000);
179
  });
180
  </script>
181
  """
@@ -306,7 +310,7 @@ logger.info("Creating Gradio application")
306
  demo = gr.Blocks(
307
  theme=theme,
308
  css=final_css,
309
- head=posthog_script + scroll_script + redirect_script + tooltip_script + logo_swap_script,
310
  title="OpenHands Index",
311
  )
312
 
 
135
  </script>
136
  """
137
 
138
+ # JavaScript to handle dark mode for Plotly charts and OpenHands logos
139
+ dark_mode_script = """
140
  <script>
141
+ function updateChartsForDarkMode() {
142
  const isDark = document.body.classList.contains('dark');
143
 
144
+ // Update Plotly chart backgrounds
145
+ const plots = document.querySelectorAll('.js-plotly-plot');
146
+ plots.forEach(plot => {
147
+ if (plot._fullLayout) {
148
+ Plotly.relayout(plot, {
149
+ 'paper_bgcolor': isDark ? '#1f1f1f' : 'white',
150
+ 'plot_bgcolor': isDark ? '#1f1f1f' : 'white',
151
+ 'font.color': isDark ? '#e0e0e0' : '#333',
152
+ 'xaxis.gridcolor': isDark ? '#444' : '#eee',
153
+ 'yaxis.gridcolor': isDark ? '#444' : '#eee'
154
+ });
155
+ }
156
+ });
157
 
158
+ // Swap OpenHands logos based on theme
159
+ const images = document.querySelectorAll('.js-plotly-plot image');
160
  images.forEach(img => {
161
  const href = img.getAttribute('href') || img.getAttribute('xlink:href') || '';
 
 
162
  if (href.includes('openhands=lightlogo')) {
 
163
  img.style.display = isDark ? 'none' : '';
164
  } else if (href.includes('openhands=darklogo')) {
 
165
  img.style.display = isDark ? '' : 'none';
166
  }
167
  });
168
  }
169
 
 
170
  document.addEventListener('DOMContentLoaded', () => {
171
+ setTimeout(updateChartsForDarkMode, 500);
 
172
 
 
173
  const observer = new MutationObserver((mutations) => {
174
  mutations.forEach((mutation) => {
175
  if (mutation.attributeName === 'class') {
176
+ updateChartsForDarkMode();
177
  }
178
  });
179
  });
 
180
  observer.observe(document.body, { attributes: true });
181
 
182
+ setInterval(updateChartsForDarkMode, 1000);
 
183
  });
184
  </script>
185
  """
 
310
  demo = gr.Blocks(
311
  theme=theme,
312
  css=final_css,
313
+ head=posthog_script + scroll_script + redirect_script + tooltip_script + dark_mode_script,
314
  title="OpenHands Index",
315
  )
316