|
4 | 4 | import plotly.express as px |
5 | 5 | import re |
6 | 6 | import subprocess |
7 | | -from datetime import datetime, timedelta |
| 7 | +from datetime import datetime |
8 | 8 |
|
9 | 9 | 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' |
24 | 25 | } |
25 | 26 |
|
26 | 27 | def get_file_counts_over_time(start_date, columns): |
@@ -66,44 +67,92 @@ def get_file_counts_over_time(start_date, columns): |
66 | 67 |
|
67 | 68 | return pd.DataFrame(data, columns=columns) |
68 | 69 |
|
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): |
70 | 82 | """ |
71 | 83 | Plots the number of files over time. |
72 | 84 |
|
73 | 85 | Args: |
74 | 86 | df (pd.DataFrame): A pandas DataFrame which should contain `xlabel` and `ylabel` as columns |
75 | 87 | xlabel (str): The column from `df` to use as the `x`-axis |
76 | 88 | 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` |
77 | 92 | line_color (str): Colour of plotted graph |
| 93 | + line_width (float): Width of plotted line in `px` |
78 | 94 | title (dict): Dictionary specifying graph title and style |
79 | 95 | 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" |
80 | 97 | """ |
| 98 | + out_path = f"{out_path}_{theme}.html" |
81 | 99 | 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} |
86 | 118 | ) |
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 | + |
90 | 136 | with open(out_path, "w") as f: |
91 | | - f.write(fig_html) |
| 137 | + f.write(styled_html) |
92 | 138 |
|
93 | 139 | if __name__ == "__main__": |
94 | 140 | github_url = "https://github.com/google-deepmind/formal-conjectures" |
95 | 141 | print(f"Generating growth plots for: {github_url}") |
96 | 142 |
|
97 | 143 | columns = [conf['xlabel'], conf['ylabel']] |
98 | 144 | 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