Skip to content

Commit f762546

Browse files
jukentjnmorley
andauthored
Update cookbook gallery (#69)
Co-authored-by: jnmorley <[email protected]>
1 parent c798897 commit f762546

11 files changed

+196
-179
lines changed

.github/ISSUE_TEMPLATE/update-cookbook-gallery.yaml

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ name: Update Cookbook Gallery
22
description: For submitting a new Cookbook repository for acceptance into the gallery
33
title: 'Update Gallery with new Cookbook'
44
labels: ['content', 'cookbook-gallery-submission']
5-
assignees:
5+
assignees:
66
- ProjectPythiaCookbooks/education
77

88
body:
99
- type: markdown
1010
attributes:
1111
value: |
12-
Thank you for your contribution to the Project Pythia Cookbook Gallery!
12+
Thank you for your contribution to the Project Pythia Cookbook Gallery!
1313
- type: input
1414
id: root-repo-name
1515
attributes:
@@ -25,10 +25,9 @@ body:
2525
multiple: true
2626
options:
2727
- 'yes'
28-
- 'no'
28+
- 'no'
2929
- type: textarea
3030
id: comments
3131
attributes:
3232
label: Additional comments
3333
description: Please add any additional comments on this issue here.
34-

.github/workflows/collect-user-submission.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ def create_submission(self):
2424
return self
2525

2626
def _create_submission_input(self):
27-
text = self.data['issue']['body']
27+
text = self.data["issue"]["body"]
2828

2929
left = "### Root Repository Name"
3030
right = "### Did you check"
31-
repo = text[text.index(left)+len(left):text.index(right)].strip()
32-
31+
repo = text[text.index(left) + len(left) : text.index(right)].strip()
32+
3333
self.submission = Submission(repo=repo)
3434

3535

36-
if __name__ == '__main__':
36+
if __name__ == "__main__":
3737

38-
issue = IssueInfo(gh_event_path=os.environ['GITHUB_EVENT_PATH']).create_submission()
38+
issue = IssueInfo(gh_event_path=os.environ["GITHUB_EVENT_PATH"]).create_submission()
3939
input = issue.submission.dict()
40-
with open('cookbook-submission-input.txt', 'w') as f:
41-
f.write(input['repo'])
40+
with open("cookbook-submission-input.txt", "w") as f:
41+
f.write(input["repo"])

cookbook-submission-input.txt

Whitespace-only changes.

environment.yml

-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ dependencies:
1212
- sphinx-panels
1313
- pip:
1414
- sphinx-pythia-theme
15-

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ select = B,C,E,F,W,T4,B9
77

88
[isort]
99
known_first_party=
10-
known_third_party=frontmatter,gallery_generator,markdown_it,pydantic,truncatehtml,yaml
10+
known_third_party=gallery_generator,pydantic,truncatehtml,yaml
1111
multi_line_output=3
1212
include_trailing_comma=True
1313
force_grid_wrap=0

site/_extensions/cookbook_gallery_generator.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@
33

44
def main(app):
55

6-
with open('cookbook_gallery.txt') as fid:
6+
with open("cookbook_gallery.txt") as fid:
77
all_items = fid.readlines()
88

99
repo_dicts = generate_repo_dicts(all_items)
1010

11-
title = 'Cookbooks Gallery'
11+
title = "Cookbooks Gallery"
1212

13-
subtext = ''
14-
with open('cookbook_gallery_subtext.md') as fid:
13+
subtext = ""
14+
with open("cookbook_gallery_subtext.md") as fid:
1515
for line in fid:
1616
subtext = subtext + line
1717

18-
submit_btn_link = 'https://github.com/ProjectPythiaCookbooks/projectpythiacookbooks.github.io/issues/new?assignees=ProjectPythiaCookbooks%2Feducation&labels=content%2Ccookbook-gallery-submission&template=update-cookbook-gallery.yaml&title=Update+Gallery+with+new+Cookbook'
19-
submit_btn_txt = 'Submit a new Cookbook'
20-
menu_html = generate_menu(repo_dicts, submit_btn_txt=submit_btn_txt, submit_btn_link=submit_btn_link)
21-
22-
build_from_repos(repo_dicts, 'index', title=title, subtext=subtext, menu_html=menu_html)
18+
submit_btn_link = "https://github.com/ProjectPythiaCookbooks/projectpythiacookbooks.github.io/issues/new?assignees=ProjectPythiaCookbooks%2Feducation&labels=content%2Ccookbook-gallery-submission&template=update-cookbook-gallery.yaml&title=Update+Gallery+with+new+Cookbook"
19+
submit_btn_txt = "Submit a new Cookbook"
20+
menu_html = generate_menu(
21+
repo_dicts, submit_btn_txt=submit_btn_txt, submit_btn_link=submit_btn_link
22+
)
23+
24+
build_from_repos(
25+
repo_dicts, "index", title=title, subtext=subtext, menu_html=menu_html
26+
)
2327

2428

2529
def setup(app):
26-
app.connect('builder-inited', main)
30+
app.connect("builder-inited", main)

site/_extensions/gallery_generator.py

+65-50
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import itertools
22
import pathlib
3-
from textwrap import dedent
43
import urllib.request
5-
import yaml
4+
from textwrap import dedent
65

6+
import yaml
77
from truncatehtml import truncate
88

99

@@ -13,50 +13,54 @@ def _generate_status_badge_html(repo, github_url):
1313
<a class="reference external" href="https://binder-staging.2i2c.cloud/v2/gh/ProjectPythiaTutorials/{repo}.git/main"><img alt="Binder" src="https://binder-staging.2i2c.cloud/badge_logo.svg" /></a>
1414
"""
1515

16+
1617
def generate_repo_dicts(all_items):
1718

1819
repo_dicts = []
1920
for item in all_items:
2021
repo = item.strip()
21-
github_url = f'https://github.com/ProjectPythiaCookbooks/{repo}'
22-
cookbook_url = f'https://cookbooks.projectpythia.org/{repo}/README.html'
23-
24-
config_url = f'https://raw.githubusercontent.com/ProjectPythiaCookbooks/{repo}/main/_config.yml'
22+
github_url = f"https://github.com/ProjectPythiaCookbooks/{repo}"
23+
cookbook_url = f"https://cookbooks.projectpythia.org/{repo}/README.html"
24+
25+
config_url = f"https://raw.githubusercontent.com/ProjectPythiaCookbooks/{repo}/main/_config.yml"
2526
config = urllib.request.urlopen(config_url)
2627
config_dict = yaml.safe_load(config)
2728

28-
cookbook_title = config_dict['title']
29-
authors = config_dict['author']
30-
thumbnail = config_dict['thumbnail']
31-
description = config_dict['description']
32-
tag_dict = {k: v for k, v in config_dict['tags'].items() if v[0] != None}
33-
34-
repo_dict = {'repo': repo,
35-
'github_url': github_url,
36-
'cookbook_url': cookbook_url,
37-
'cookbook_title': cookbook_title,
38-
'authors': authors,
39-
'thumbnail': thumbnail,
40-
'description': description,
41-
'tags': tag_dict
42-
}
29+
cookbook_title = config_dict["title"]
30+
authors = config_dict["author"]
31+
thumbnail = config_dict["thumbnail"]
32+
description = config_dict["description"]
33+
tag_dict = {k: v for k, v in config_dict["tags"].items() if v[0] != None}
34+
35+
repo_dict = {
36+
"repo": repo,
37+
"github_url": github_url,
38+
"cookbook_url": cookbook_url,
39+
"cookbook_title": cookbook_title,
40+
"authors": authors,
41+
"thumbnail": thumbnail,
42+
"description": description,
43+
"tags": tag_dict,
44+
}
4345

4446
repo_dicts.append(repo_dict)
45-
47+
4648
return repo_dicts
4749

4850

4951
def _generate_sorted_tag_keys(repo_dicts):
5052

51-
key_set = set(itertools.chain(*[repo_dict['tags'].keys() for repo_dict in repo_dicts]))
53+
key_set = set(
54+
itertools.chain(*[repo_dict["tags"].keys() for repo_dict in repo_dicts])
55+
)
5256
return sorted(key_set)
5357

5458

5559
def _generate_tag_set(repo_dicts, tag_key=None):
5660

5761
tag_set = set()
5862
for repo_dict in repo_dicts:
59-
for k, e in repo_dict['tags'].items():
63+
for k, e in repo_dict["tags"].items():
6064
if tag_key and k != tag_key:
6165
continue
6266
for t in e:
@@ -70,7 +74,7 @@ def _generate_tag_menu(repo_dicts, tag_key):
7074
tag_set = _generate_tag_set(repo_dicts, tag_key)
7175
tag_list = sorted(tag_set)
7276

73-
options = ''.join(
77+
options = "".join(
7478
f'<li><label class="dropdown-item checkbox {tag_key}"><input type="checkbox" rel={tag.replace(" ", "-")} onchange="change();">&nbsp;{tag}</label></li>'
7579
for tag in tag_list
7680
)
@@ -96,43 +100,54 @@ def generate_menu(repo_dicts, submit_btn_txt=None, submit_btn_link=None):
96100
menu_html += '<div class="d-flex gallery-menu">\n'
97101
if submit_btn_txt:
98102
menu_html += f'<div><a role="button" class="btn btn-primary btn-sm mx-1" href={submit_btn_link}>{submit_btn_txt}</a></div>\n'
99-
menu_html += '</div>\n'
103+
menu_html += "</div>\n"
100104
menu_html += '<div class="ml-auto d-flex">\n'
101105
menu_html += '<div><button class="btn btn-link btn-sm mx-1" onclick="clearCbs()">Clear all filters</button></div>\n'
102106
for tag_key in key_list:
103-
menu_html += _generate_tag_menu(repo_dicts, tag_key) + '\n'
104-
menu_html += '</div>\n'
105-
menu_html += '</div>\n'
107+
menu_html += _generate_tag_menu(repo_dicts, tag_key) + "\n"
108+
menu_html += "</div>\n"
109+
menu_html += "</div>\n"
106110
menu_html += '<script>$(document).on("click",function(){$(".collapse").collapse("hide");}); </script>\n'
107111
return menu_html
108112

109113

110-
def build_from_repos(repo_dicts, filename, title='Gallery', subtitle=None, subtext=None, menu_html='', max_descr_len=300):
114+
def build_from_repos(
115+
repo_dicts,
116+
filename,
117+
title="Gallery",
118+
subtitle=None,
119+
subtext=None,
120+
menu_html="",
121+
max_descr_len=300,
122+
):
111123

112124
# Build the gallery file
113125
panels_body = []
114126
for repo_dict in repo_dicts:
115-
repo = repo_dict['repo']
116-
github_url = repo_dict['github_url']
127+
repo = repo_dict["repo"]
128+
github_url = repo_dict["github_url"]
117129
status_badges = _generate_status_badge_html(repo, github_url)
118130

119-
cookbook_url = repo_dict['cookbook_url']
120-
cookbook_title = repo_dict['cookbook_title']
131+
cookbook_url = repo_dict["cookbook_url"]
132+
cookbook_title = repo_dict["cookbook_title"]
121133

122-
authors = repo_dict['authors']
134+
authors = repo_dict["authors"]
123135
authors_str = f"<strong>Author:</strong> {authors}"
124136

125-
thumbnail = repo_dict['thumbnail']
126-
thumbnail_url = f'https://raw.githubusercontent.com/ProjectPythiaCookbooks/{repo}/main/{thumbnail}'
127-
128-
tag_dict = repo_dict['tags']
129-
tag_list = sorted((itertools.chain(*tag_dict.values())))
130-
tag_list_f = [tag.replace(' ', '-') for tag in tag_list]
131-
tags = [f'<span class="badge bg-primary mybadges">{tag}</span>' for tag in tag_list_f]
132-
tags = '\n'.join(tags)
133-
tag_class_str = ' '.join(tag_list_f)
137+
thumbnail = repo_dict["thumbnail"]
138+
thumbnail_url = f"https://raw.githubusercontent.com/ProjectPythiaCookbooks/{repo}/main/{thumbnail}"
134139

135-
description = repo_dict['description']
140+
tag_dict = repo_dict["tags"]
141+
tag_list = sorted((itertools.chain(*tag_dict.values())))
142+
tag_list_f = [tag.replace(" ", "-") for tag in tag_list]
143+
tags = [
144+
f'<span class="badge bg-primary mybadges">{tag}</span>'
145+
for tag in tag_list_f
146+
]
147+
tags = "\n".join(tags)
148+
tag_class_str = " ".join(tag_list_f)
149+
150+
description = repo_dict["description"]
136151
ellipsis_str = '<a class="modal-btn"> ... more</a>'
137152
short_description = truncate(description, max_descr_len, ellipsis=ellipsis_str)
138153

@@ -150,7 +165,7 @@ def build_from_repos(repo_dicts, filename, title='Gallery', subtitle=None, subte
150165
</div>
151166
"""
152167
else:
153-
modal_str = ''
168+
modal_str = ""
154169

155170
panels_body.append(
156171
f"""\
@@ -177,10 +192,10 @@ def build_from_repos(repo_dicts, filename, title='Gallery', subtitle=None, subte
177192
"""
178193
)
179194

180-
panels_body = '\n'.join(panels_body)
195+
panels_body = "\n".join(panels_body)
181196

182-
stitle = f'#### {subtitle}' if subtitle else ''
183-
stext = subtext if subtext else ''
197+
stitle = f"#### {subtitle}" if subtitle else ""
198+
stext = subtext if subtext else ""
184199

185200
panels = f"""
186201
# {title}
@@ -204,4 +219,4 @@ def build_from_repos(repo_dicts, filename, title='Gallery', subtitle=None, subte
204219
<script src="/_static/custom.js"></script>
205220
"""
206221

207-
pathlib.Path(f'{filename}.md').write_text(panels)
222+
pathlib.Path(f"{filename}.md").write_text(panels)

0 commit comments

Comments
 (0)