Skip to content

Commit d555d7e

Browse files
authored
doc: dark theme plots and improved styling (#2387)
- Toggles graph to light/dark theme based on user browser preference - Improved styling for graph incl: fix aspect ratio to 16:9; hover over has vertical bar;
1 parent fdf0463 commit d555d7e

File tree

3 files changed

+100
-38
lines changed

3 files changed

+100
-38
lines changed

.github/workflows/build-and-docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ jobs:
130130
shell: bash
131131
run: |
132132
cd docbuild
133-
lake exe overwrite_index ./out/index.html ./out/file_counts.html | tee -a "$GITHUB_STEP_SUMMARY"
133+
lake exe overwrite_index ./out/index.html ./out/file_counts_dark.html ./out/file_counts_white.html | tee -a "$GITHUB_STEP_SUMMARY"
134134
135135
- name: Upload docs
136136
id: deployment

docbuild/scripts/overwrite_index.lean

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,22 @@ unsafe def main (args : List String) : IO Unit := do
113113
| IO.println "Usage: stats <file>
114114
overwrites the contents of the `main` tag of a html `file` with a welcome page including stats."
115115
let inputHtmlContent ← IO.FS.readFile file
116-
let .some (graphFile : String) := args[1]?
116+
let .some (graphFileDark : String) := args[1]?
117117
| IO.println "Repository growth graph not supplied, generating docs without graph."
118-
let graphHtml ← IO.FS.readFile graphFile
118+
let .some (graphFileLight : String) := args[2]?
119+
| IO.println "Repository growth graph not supplied, generating docs without graph."
120+
let graphHtmlDark ← IO.FS.readFile graphFileDark
121+
let graphHtmlLight ← IO.FS.readFile graphFileLight
122+
let graphHtml :=
123+
s!"<style>
124+
.theme-dark \{ display: none; }
125+
@media (prefers-color-scheme: dark) \{
126+
.theme-light \{ display: none; }
127+
.theme-dark \{ display: block; }
128+
}
129+
</style>
130+
<div class=\"theme-light\">{graphHtmlLight}</div>
131+
<div class=\"theme-dark\">{graphHtmlDark}</div>"
119132

120133
runWithImports do
121134
let categoryStats ← getCategoryStatsMarkdown

docbuild/scripts/plot_growth.py

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44
import plotly.express as px
55
import re
66
import subprocess
7-
from datetime import datetime, timedelta
7+
from datetime import datetime
88

99
conf = {
10-
'line_color': '#4285F4', # Color used in plotting
11-
'line_width': 3, # Width of line used in the plot
12-
'title': dict(
13-
text='Number of Lean files in Formal Conjectures', # Title text
14-
font=dict(size=15), # Title font
15-
x=0.5, # Center title
16-
xanchor='center' # Center title
17-
),
18-
'max-width': '1000px', # Width of plot in px
19-
'aspect-ratio': 2, # aspect ratio of plot
20-
'start_date': '2025-05-28', # Announcement date is '2025-05-28'
21-
'xlabel': 'Date', # x-axis label on plot
22-
'ylabel': 'Number of Lean files', # y-axis label on plot
23-
'out_path': 'docbuild/out/file_counts.html' # This is used as an arg to overwrite_index.lean in .github/workflows/build-and-docs.yml
10+
'line_color': '#4285F4',
11+
'line_width': 2,
12+
'title': dict(
13+
text='Number of Lean files in Formal Conjectures',
14+
font=dict(size=18),
15+
x=0.5,
16+
xanchor='center'
17+
),
18+
'width_pct': '90%',
19+
'height_px': 675,
20+
'max_width' : 1200, # 16:9 aspect ratio at max
21+
'start_date': '2025-05-28',
22+
'xlabel': 'Date',
23+
'ylabel': 'File Count',
24+
'out_path_prefix': 'docbuild/out/file_counts'
2425
}
2526

2627
def get_file_counts_over_time(start_date, columns):
@@ -66,44 +67,92 @@ def get_file_counts_over_time(start_date, columns):
6667

6768
return pd.DataFrame(data, columns=columns)
6869

69-
def plot_file_counts(df, xlabel, ylabel, max_width, aspect_ratio, line_color, line_width, title, out_path):
70+
def plot_file_counts(
71+
df,
72+
xlabel,
73+
ylabel,
74+
max_width,
75+
width_pct,
76+
height,
77+
line_color,
78+
line_width,
79+
title,
80+
out_path,
81+
theme):
7082
"""
7183
Plots the number of files over time.
7284
7385
Args:
7486
df (pd.DataFrame): A pandas DataFrame which should contain `xlabel` and `ylabel` as columns
7587
xlabel (str): The column from `df` to use as the `x`-axis
7688
ylabel (str): The column from `df` to use as the `y`-axis
89+
max_width (int): Maximum width of plot in `px`
90+
width_pct (str): % width of plot inside parent div
91+
height (int): Height of plot in `px`
7792
line_color (str): Colour of plotted graph
93+
line_width (float): Width of plotted line in `px`
7894
title (dict): Dictionary specifying graph title and style
7995
out_path (str): Save location of html
96+
theme (str): plotly template suffix to use as plot theme. E.g., use "dark" for "plotly_dark"
8097
"""
98+
out_path = f"{out_path}_{theme}.html"
8199
fig = px.line(df, xlabel, ylabel)
82-
fig.update_layout(title=title)
83-
fig.update_yaxes(
84-
scaleanchor="x",
85-
scaleratio=0.5
100+
101+
fig.update_layout(
102+
title=title,
103+
template=f"plotly_{theme}",
104+
hovermode='x unified',
105+
margin=dict(l=40, r=40, t=60, b=40),
106+
autosize=True
107+
)
108+
109+
fig.update_traces(
110+
line=dict(color=line_color, width=line_width, shape='hv'),
111+
marker=dict(size=6)
112+
)
113+
114+
fig_html = fig.to_html(
115+
full_html=False,
116+
include_plotlyjs='cdn',
117+
config={'responsive': True}
86118
)
87-
fig.update_traces(line_color=line_color, line_width=line_width)
88-
fig_html = f"<div style='max-width: {max_width}; aspect-ratio: {aspect_ratio};'> {
89-
fig.to_html(full_html=False, include_plotlyjs='cdn')} </div>"
119+
120+
styled_html = f"""
121+
<style>
122+
.plot-container {{
123+
width: {width_pct};
124+
max-width: {max_width}px;
125+
height: {height}px;
126+
margin: 0 auto;
127+
padding: 10px;
128+
}}
129+
</style>
130+
131+
<div class="plot-container">
132+
{fig_html}
133+
</div>
134+
"""
135+
90136
with open(out_path, "w") as f:
91-
f.write(fig_html)
137+
f.write(styled_html)
92138

93139
if __name__ == "__main__":
94140
github_url = "https://github.com/google-deepmind/formal-conjectures"
95141
print(f"Generating growth plots for: {github_url}")
96142

97143
columns = [conf['xlabel'], conf['ylabel']]
98144
df = get_file_counts_over_time(conf['start_date'], columns)
99-
plot_file_counts(
100-
df=df,
101-
xlabel=conf['xlabel'],
102-
ylabel=conf['ylabel'],
103-
max_width=conf['max-width'],
104-
aspect_ratio=conf['aspect-ratio'],
105-
line_color=conf['line_color'],
106-
line_width=conf['line_width'],
107-
title=conf['title'],
108-
out_path=conf['out_path']
109-
)
145+
for theme in ["white", "dark"]:
146+
plot_file_counts(
147+
df=df,
148+
xlabel=conf['xlabel'],
149+
ylabel=conf['ylabel'],
150+
max_width=conf['max_width'],
151+
width_pct=conf['width_pct'],
152+
height=conf['height_px'],
153+
line_color=conf['line_color'],
154+
line_width=conf['line_width'],
155+
title=conf['title'],
156+
out_path=conf['out_path_prefix'],
157+
theme=theme
158+
)

0 commit comments

Comments
 (0)