1
1
import itertools
2
2
import pathlib
3
- from textwrap import dedent
4
3
import urllib .request
5
- import yaml
4
+ from textwrap import dedent
6
5
6
+ import yaml
7
7
from truncatehtml import truncate
8
8
9
9
@@ -13,50 +13,54 @@ def _generate_status_badge_html(repo, github_url):
13
13
<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>
14
14
"""
15
15
16
+
16
17
def generate_repo_dicts (all_items ):
17
18
18
19
repo_dicts = []
19
20
for item in all_items :
20
21
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"
25
26
config = urllib .request .urlopen (config_url )
26
27
config_dict = yaml .safe_load (config )
27
28
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
+ }
43
45
44
46
repo_dicts .append (repo_dict )
45
-
47
+
46
48
return repo_dicts
47
49
48
50
49
51
def _generate_sorted_tag_keys (repo_dicts ):
50
52
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
+ )
52
56
return sorted (key_set )
53
57
54
58
55
59
def _generate_tag_set (repo_dicts , tag_key = None ):
56
60
57
61
tag_set = set ()
58
62
for repo_dict in repo_dicts :
59
- for k , e in repo_dict [' tags' ].items ():
63
+ for k , e in repo_dict [" tags" ].items ():
60
64
if tag_key and k != tag_key :
61
65
continue
62
66
for t in e :
@@ -70,7 +74,7 @@ def _generate_tag_menu(repo_dicts, tag_key):
70
74
tag_set = _generate_tag_set (repo_dicts , tag_key )
71
75
tag_list = sorted (tag_set )
72
76
73
- options = '' .join (
77
+ options = "" .join (
74
78
f'<li><label class="dropdown-item checkbox { tag_key } "><input type="checkbox" rel={ tag .replace (" " , "-" )} onchange="change();"> { tag } </label></li>'
75
79
for tag in tag_list
76
80
)
@@ -96,43 +100,54 @@ def generate_menu(repo_dicts, submit_btn_txt=None, submit_btn_link=None):
96
100
menu_html += '<div class="d-flex gallery-menu">\n '
97
101
if submit_btn_txt :
98
102
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 "
100
104
menu_html += '<div class="ml-auto d-flex">\n '
101
105
menu_html += '<div><button class="btn btn-link btn-sm mx-1" onclick="clearCbs()">Clear all filters</button></div>\n '
102
106
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 "
106
110
menu_html += '<script>$(document).on("click",function(){$(".collapse").collapse("hide");}); </script>\n '
107
111
return menu_html
108
112
109
113
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
+ ):
111
123
112
124
# Build the gallery file
113
125
panels_body = []
114
126
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" ]
117
129
status_badges = _generate_status_badge_html (repo , github_url )
118
130
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" ]
121
133
122
- authors = repo_dict [' authors' ]
134
+ authors = repo_dict [" authors" ]
123
135
authors_str = f"<strong>Author:</strong> { authors } "
124
136
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 } "
134
139
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" ]
136
151
ellipsis_str = '<a class="modal-btn"> ... more</a>'
137
152
short_description = truncate (description , max_descr_len , ellipsis = ellipsis_str )
138
153
@@ -150,7 +165,7 @@ def build_from_repos(repo_dicts, filename, title='Gallery', subtitle=None, subte
150
165
</div>
151
166
"""
152
167
else :
153
- modal_str = ''
168
+ modal_str = ""
154
169
155
170
panels_body .append (
156
171
f"""\
@@ -177,10 +192,10 @@ def build_from_repos(repo_dicts, filename, title='Gallery', subtitle=None, subte
177
192
"""
178
193
)
179
194
180
- panels_body = ' \n ' .join (panels_body )
195
+ panels_body = " \n " .join (panels_body )
181
196
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 ""
184
199
185
200
panels = f"""
186
201
# { title }
@@ -204,4 +219,4 @@ def build_from_repos(repo_dicts, filename, title='Gallery', subtitle=None, subte
204
219
<script src="/_static/custom.js"></script>
205
220
"""
206
221
207
- pathlib .Path (f' { filename } .md' ).write_text (panels )
222
+ pathlib .Path (f" { filename } .md" ).write_text (panels )
0 commit comments