Skip to content

Commit a75cee8

Browse files
load brand_yml on demand
should not be hard dependency of quarto-python
1 parent b6b58c4 commit a75cee8

File tree

1 file changed

+95
-60
lines changed

1 file changed

+95
-60
lines changed

quarto/theme.py

Lines changed: 95 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,74 @@
11
from functools import partial
2-
from brand_yml import Brand
2+
33

44
def theme_colors_altair(bg, fg):
55
return {
6-
'config': {
7-
'view': {'stroke': 'transparent'},
8-
'axis': {
9-
'domainColor': fg,
10-
'labelColor': fg,
11-
'titleColor': fg,
6+
"config": {
7+
"view": {"stroke": "transparent"},
8+
"axis": {
9+
"domainColor": fg,
10+
"labelColor": fg,
11+
"titleColor": fg,
1212
},
13-
'legend': {
14-
'labelColor': fg,
15-
'titleColor': fg,
13+
"legend": {
14+
"labelColor": fg,
15+
"titleColor": fg,
1616
},
17-
'background': bg, # Background color
17+
"background": bg, # Background color
1818
}
1919
}
2020

21+
2122
def theme_brand_altair(brand_yml):
22-
brand = Brand.from_yaml(brand_yml)
23-
bg = brand.color.background
24-
fg = brand.color.foreground
25-
return partial(theme_colors_altair, bg, fg)
23+
from brand_yml import Brand
24+
25+
brand = Brand.from_yaml(brand_yml)
26+
bg = brand.color.background
27+
fg = brand.color.foreground
28+
return partial(theme_colors_altair, bg, fg)
29+
2630

2731
# background fill is incomplete
2832
def theme_colors_bokeh(bg, fg):
2933
from bokeh.io import curdoc
3034
from bokeh.themes import Theme
31-
curdoc().theme = Theme(json={'attrs': {
32-
'figure': {
33-
'background_fill_color': bg, # just plot area
34-
},
35-
'Title': {
36-
'background_fill_color': bg,
37-
'text_color': fg,
38-
},
39-
'Axis': {
40-
'background_fill_color': bg,
41-
'axis_label_text_color': fg,
42-
'major_label_text_color': fg,
43-
},
44-
}})
35+
36+
curdoc().theme = Theme(
37+
json={
38+
"attrs": {
39+
"figure": {
40+
"background_fill_color": bg, # just plot area
41+
},
42+
"Title": {
43+
"background_fill_color": bg,
44+
"text_color": fg,
45+
},
46+
"Axis": {
47+
"background_fill_color": bg,
48+
"axis_label_text_color": fg,
49+
"major_label_text_color": fg,
50+
},
51+
}
52+
}
53+
)
54+
4555

4656
def theme_brand_bokeh(brand_yml):
57+
from brand_yml import Brand
58+
4759
brand = Brand.from_yaml(brand_yml)
4860
fg = brand.color.foreground
4961
bg = brand.color.background
5062
return partial(theme_colors_bokeh, bg, fg)
5163

5264

5365
def theme_colors_great_tables(bg, fg):
54-
return {
55-
'table_background_color': bg,
56-
'table_font_color': fg
57-
}
66+
return {"table_background_color": bg, "table_font_color": fg}
67+
68+
5869
def theme_brand_great_tables(brand_yml):
70+
from brand_yml import Brand
71+
5972
brand = Brand.from_yaml(brand_yml)
6073
fg = brand.color.foreground
6174
bg = brand.color.background
@@ -65,6 +78,7 @@ def theme_brand_great_tables(brand_yml):
6578
def theme_colors_matplotlib(bg, fg, primary):
6679
import matplotlib as mpl
6780
from cycler import cycler
81+
6882
mpl.rcParams["axes.facecolor"] = bg
6983
mpl.rcParams["axes.edgecolor"] = fg
7084
mpl.rcParams["axes.labelcolor"] = fg
@@ -75,10 +89,12 @@ def theme_colors_matplotlib(bg, fg, primary):
7589
mpl.rcParams["xtick.color"] = fg
7690
mpl.rcParams["ytick.color"] = fg
7791
if primary:
78-
mpl.rcParams["axes.prop_cycle"] = cycler('color', [primary])
92+
mpl.rcParams["axes.prop_cycle"] = cycler("color", [primary])
7993

8094

8195
def theme_brand_matplotlib(brand_yml):
96+
from brand_yml import Brand
97+
8298
brand = Brand.from_yaml(brand_yml)
8399
return partial(
84100
theme_colors_matplotlib,
@@ -90,43 +106,59 @@ def theme_brand_matplotlib(brand_yml):
90106

91107
def theme_colors_plotnine(bg, fg):
92108
from plotnine import theme, element_rect, element_text
109+
93110
return theme(
94-
plot_background=element_rect(fill=bg, size=0),
95-
text=element_text(color=fg)
111+
plot_background=element_rect(fill=bg, size=0), text=element_text(color=fg)
96112
)
97113

114+
98115
def theme_brand_plotnine(brand_yml):
116+
from brand_yml import Brand
117+
99118
brand = Brand.from_yaml(brand_yml)
100119
return theme_colors_plotnine(brand.color.background, brand.color.foreground)
101120

102121

103122
def theme_colors_plotly(bg, fg):
104123
import plotly.graph_objects as go
105-
return go.layout.Template({'layout': {
106-
'paper_bgcolor': bg,
107-
'plot_bgcolor': bg,
108-
'font_color': fg,
109-
}})
124+
125+
return go.layout.Template(
126+
{
127+
"layout": {
128+
"paper_bgcolor": bg,
129+
"plot_bgcolor": bg,
130+
"font_color": fg,
131+
}
132+
}
133+
)
134+
110135

111136
def theme_brand_plotly(brand_yml):
137+
from brand_yml import Brand
138+
112139
brand = Brand.from_yaml(brand_yml)
113140
return theme_colors_plotly(brand.color.background, brand.color.foreground)
114141

115142

116143
def theme_colors_pygal(_bg, fg, primary, secondary):
117144
from pygal.style import Style
145+
118146
return Style(
119-
background='transparent',
120-
plot_background='transparent',
147+
background="transparent",
148+
plot_background="transparent",
121149
foreground=fg,
122150
foreground_strong=primary,
123-
foreground_subtle=secondary or '#630C0D',
124-
opacity='.6',
125-
opacity_hover='.9',
126-
transition='400ms ease-in',
127-
colors=('#E853A0', '#E8537A', '#E95355', '#E87653', '#E89B53'))
151+
foreground_subtle=secondary or "#630C0D",
152+
opacity=".6",
153+
opacity_hover=".9",
154+
transition="400ms ease-in",
155+
colors=("#E853A0", "#E8537A", "#E95355", "#E87653", "#E89B53"),
156+
)
157+
128158

129159
def theme_brand_pygal(brand_yml):
160+
from brand_yml import Brand
161+
130162
brand = Brand.from_yaml(brand_yml)
131163
return theme_colors_pygal(
132164
brand.color.background,
@@ -139,18 +171,21 @@ def theme_brand_pygal(brand_yml):
139171
def theme_colors_seaborn(bg, fg):
140172
# seaborn accepts matplotlib rcparams
141173
return {
142-
"axes.facecolor": bg,
143-
"axes.edgecolor": fg,
144-
"axes.labelcolor": fg,
145-
"axes.titlecolor": fg,
146-
"figure.facecolor": bg,
147-
"figure.edgecolor": fg,
148-
"text.color": fg,
149-
"xtick.color": fg,
150-
"ytick.color": fg,
151-
"grid.color": fg,
174+
"axes.facecolor": bg,
175+
"axes.edgecolor": fg,
176+
"axes.labelcolor": fg,
177+
"axes.titlecolor": fg,
178+
"figure.facecolor": bg,
179+
"figure.edgecolor": fg,
180+
"text.color": fg,
181+
"xtick.color": fg,
182+
"ytick.color": fg,
183+
"grid.color": fg,
152184
}
153185

186+
154187
def theme_brand_seaborn(brand_yml):
155-
brand = Brand.from_yaml(brand_yml)
156-
return theme_colors_seaborn(brand.color.background, brand.color.foreground)
188+
from brand_yml import Brand
189+
190+
brand = Brand.from_yaml(brand_yml)
191+
return theme_colors_seaborn(brand.color.background, brand.color.foreground)

0 commit comments

Comments
 (0)