StanDataCamp commited on
Commit
78085ee
·
1 Parent(s): addd5d9

Implement theme-aware dark mode styling and enhance theme update functionality in app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -7
app.py CHANGED
@@ -137,18 +137,78 @@ with gr.Blocks(
137
  .explanation-box * {
138
  color: #1a1a1a !important;
139
  }
140
- /* Dark mode styling */
141
- .dark .explanation-box {
 
 
 
 
 
142
  background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%) !important;
143
- border: 1px solid #334155;
144
  }
145
- .dark .explanation-box,
146
- .dark .explanation-box * {
147
  color: #f1f5f9 !important;
148
  }
149
- .dark .explanation-box strong,
150
- .dark .explanation-box b {
151
  color: #fbbf24 !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  }
153
  """
154
  ) as app:
 
137
  .explanation-box * {
138
  color: #1a1a1a !important;
139
  }
140
+ .explanation-box strong,
141
+ .explanation-box b {
142
+ color: #1a1a1a !important;
143
+ font-weight: bold;
144
+ }
145
+ /* Dark mode styling - using theme-aware class */
146
+ .theme-dark .explanation-box {
147
  background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%) !important;
148
+ border: 1px solid #334155 !important;
149
  }
150
+ .theme-dark .explanation-box,
151
+ .theme-dark .explanation-box * {
152
  color: #f1f5f9 !important;
153
  }
154
+ .theme-dark .explanation-box strong,
155
+ .theme-dark .explanation-box b {
156
  color: #fbbf24 !important;
157
+ font-weight: bold;
158
+ }
159
+ """,
160
+ js="""
161
+ function() {
162
+ // Function to update theme class
163
+ function updateTheme() {
164
+ const gradioApp = document.querySelector('gradio-app');
165
+ const body = document.body;
166
+ const html = document.documentElement;
167
+
168
+ // Check multiple possible dark mode indicators
169
+ const isDark = body.classList.contains('dark') ||
170
+ html.classList.contains('dark') ||
171
+ gradioApp?.classList.contains('dark') ||
172
+ window.matchMedia('(prefers-color-scheme: dark)').matches;
173
+
174
+ // Apply theme class to body for consistent styling
175
+ if (isDark) {
176
+ body.classList.add('theme-dark');
177
+ body.classList.remove('theme-light');
178
+ } else {
179
+ body.classList.add('theme-light');
180
+ body.classList.remove('theme-dark');
181
+ }
182
+ }
183
+
184
+ // Update theme immediately
185
+ updateTheme();
186
+
187
+ // Watch for theme changes
188
+ const observer = new MutationObserver(updateTheme);
189
+ observer.observe(document.documentElement, {
190
+ attributes: true,
191
+ attributeFilter: ['class', 'data-theme']
192
+ });
193
+ observer.observe(document.body, {
194
+ attributes: true,
195
+ attributeFilter: ['class', 'data-theme']
196
+ });
197
+
198
+ // Also check when gradio-app loads
199
+ const checkGradioApp = setInterval(() => {
200
+ const app = document.querySelector('gradio-app');
201
+ if (app) {
202
+ observer.observe(app, {
203
+ attributes: true,
204
+ attributeFilter: ['class', 'data-theme']
205
+ });
206
+ clearInterval(checkGradioApp);
207
+ }
208
+ }, 100);
209
+
210
+ // Listen for system theme changes
211
+ window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateTheme);
212
  }
213
  """
214
  ) as app: