Skip to content

Commit be24adb

Browse files
apply body.quarto-dark if default theme is dark
1 parent 3da7d36 commit be24adb

File tree

3 files changed

+208
-1
lines changed

3 files changed

+208
-1
lines changed

src/format/html/format-html-bootstrap.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,18 @@ function bootstrapHtmlFinalizer(format: Format, flags: PandocFlags) {
10601060
}
10611061
}
10621062

1063+
let initialLightDarkClass = "quarto-light";
1064+
// some logic duplicated from resolveThemeLayer
1065+
const theme = format.metadata.theme;
1066+
if (theme && !Array.isArray(theme) && typeof theme === "object") {
1067+
const keys = Object.keys(theme);
1068+
if(keys.length > 1 && keys[0] === "dark") {
1069+
initialLightDarkClass = "quarto-dark"
1070+
}
1071+
}
1072+
10631073
// start body with .quarto-light for proper display when JS is disabled
1064-
doc.body.classList.add("quarto-light");
1074+
doc.body.classList.add(initialLightDarkClass);
10651075

10661076
// If there is no margin content and no toc in the right margin
10671077
// then lower the z-order so everything else can get on top
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
---
2+
title: "jupyter dark mode - matplotlib"
3+
engine: jupyter
4+
format:
5+
html:
6+
theme:
7+
dark: slate
8+
light: united
9+
keep-md: true
10+
_quarto:
11+
tests:
12+
html:
13+
ensureHtmlElements:
14+
-
15+
- 'body.quarto-dark'
16+
- 'div.cell div.light-content'
17+
- 'div.cell div.dark-content'
18+
- []
19+
---
20+
21+
```{python}
22+
#| echo: false
23+
import yaml
24+
import tempfile
25+
import os
26+
27+
def apply_mpl_colors(bgcolor, fgcolor, primarycolor):
28+
fd, name = tempfile.mkstemp("mplstyle")
29+
os.close(fd)
30+
with open(name, "w") as out:
31+
out.write("axes.facecolor: \"%s\"\n" % bgcolor)
32+
out.write("axes.edgecolor: \"%s\"\n" % fgcolor)
33+
out.write("axes.labelcolor: \"%s\"\n" % fgcolor)
34+
out.write("axes.titlecolor: \"%s\"\n" % fgcolor)
35+
out.write("figure.facecolor: \"%s\"\n" % bgcolor)
36+
out.write("figure.edgecolor: \"%s\"\n" % fgcolor)
37+
out.write("text.color: \"%s\"\n" % fgcolor)
38+
out.write("xtick.color: \"%s\"\n" % fgcolor)
39+
out.write("ytick.color: \"%s\"\n" % fgcolor)
40+
# seems to require named color, is there a better way?
41+
out.write("axes.prop_cycle: cycler('color', ['%s'])" % primarycolor)
42+
plt.style.use(name)
43+
os.unlink(name)
44+
45+
def united_colors():
46+
apply_mpl_colors("#ffffff", "#333333", "red")
47+
48+
def slate_colors():
49+
apply_mpl_colors("#282B30", "#aaaaaa", "white")
50+
```
51+
52+
### No crossref or caption
53+
```{python}
54+
#| echo: false
55+
#| renderings: [light, dark]
56+
import numpy as np
57+
import matplotlib.pyplot as plt
58+
59+
# Parameters for the normal distribution
60+
mean = 0
61+
std_dev = 1
62+
63+
# Generate data
64+
x = np.linspace(mean - 4*std_dev, mean + 4*std_dev, 1000)
65+
y = (1/(std_dev * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - mean) / std_dev)**2)
66+
67+
# Plotting
68+
united_colors()
69+
plt.figure(figsize=(8, 5))
70+
plt.plot(x, y, label='Normal Distribution')
71+
plt.title('Normal Distribution Curve')
72+
plt.xlabel('X-axis')
73+
plt.ylabel('Probability Density')
74+
plt.legend()
75+
plt.grid(True)
76+
plt.show()
77+
78+
slate_colors()
79+
plt.figure(figsize=(8, 5))
80+
plt.plot(x, y, label='Normal Distribution')
81+
plt.title('Normal Distribution Curve')
82+
plt.xlabel('X-axis')
83+
plt.ylabel('Probability Density')
84+
plt.legend()
85+
plt.grid(True)
86+
plt.show()
87+
```
88+
89+
### With crossref but no caption
90+
91+
::: {#fig-matplotlib-line}
92+
```{python}
93+
#| echo: false
94+
#| renderings: [light, dark]
95+
import matplotlib.pyplot as plt
96+
97+
united_colors()
98+
plt.title("Hello")
99+
plt.plot([1,2,3])
100+
plt.grid(True)
101+
plt.show(block=False)
102+
103+
slate_colors()
104+
plt.figure()
105+
plt.title("Hello")
106+
plt.plot([1,2,3])
107+
plt.grid(True)
108+
plt.show(block=False)
109+
```
110+
:::
111+
112+
### With caption but no crossref
113+
114+
::: {}
115+
```{python}
116+
#| echo: false
117+
#| renderings: [light, dark]
118+
119+
# author: "anthropic claude-3-5-sonnet-20240620"
120+
import numpy as np
121+
import matplotlib.pyplot as plt
122+
123+
# Generate data points
124+
x = np.linspace(0, 2 * np.pi, 100)
125+
y = np.sin(x)
126+
127+
united_colors()
128+
plt.figure(figsize=(10, 6))
129+
plt.plot(x, y)
130+
plt.title('Sine Wave')
131+
plt.xlabel('x')
132+
plt.ylabel('sin(x)')
133+
plt.grid(True)
134+
plt.axhline(y=0, color='k', linestyle='--')
135+
plt.axvline(x=0, color='k', linestyle='--')
136+
plt.show()
137+
138+
slate_colors()
139+
plt.figure(figsize=(10, 6))
140+
plt.plot(x, y)
141+
plt.title('Sine Wave')
142+
plt.xlabel('x')
143+
plt.ylabel('sin(x)')
144+
plt.grid(True)
145+
plt.axhline(y=0, color='k', linestyle='--')
146+
plt.axvline(x=0, color='k', linestyle='--')
147+
plt.show()
148+
```
149+
matplotlib sine wave
150+
151+
:::
152+
153+
### With crossref and caption
154+
155+
::: {#fig-matplotlib-cosine}
156+
```{python}
157+
#| echo: false
158+
#| renderings: [dark, light]
159+
import numpy as np
160+
import matplotlib.pyplot as plt
161+
162+
# Generate data points
163+
x = np.linspace(0, 2 * np.pi, 100)
164+
y = np.cos(x)
165+
166+
# Create the plot
167+
slate_colors()
168+
plt.figure(figsize=(10, 6))
169+
plt.plot(x, y)
170+
plt.title('Cosine Wave')
171+
plt.xlabel('x')
172+
plt.ylabel('cos(x)')
173+
plt.grid(True)
174+
plt.axhline(y=0, color='k', linestyle='--')
175+
plt.axvline(x=0, color='k', linestyle='--')
176+
plt.show()
177+
178+
united_colors()
179+
plt.figure(figsize=(10, 6))
180+
plt.plot(x, y)
181+
plt.title('Cosine Wave')
182+
plt.xlabel('x')
183+
plt.ylabel('cos(x)')
184+
plt.grid(True)
185+
plt.axhline(y=0, color='k', linestyle='--')
186+
plt.axvline(x=0, color='k', linestyle='--')
187+
plt.show()
188+
```
189+
190+
matplotlib cosine wave
191+
:::
192+
193+
Here's a [link](https://example.com).
194+
195+
196+
{{< lipsum 3 >}}

tests/docs/smoke-all/dark-mode/matplotlib-brandless.qmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ _quarto:
1212
html:
1313
ensureHtmlElements:
1414
-
15+
- 'body.quarto-light'
1516
- 'div.cell div.light-content'
1617
- 'div.cell div.dark-content'
1718
- []

0 commit comments

Comments
 (0)