Skip to content

Commit f37196d

Browse files
jukentpre-commit-ci[bot]dcamron
authored
Create Cookbook Gallery (#243)
* add cookbook gallery generator script * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add cookbook gallery generator script * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update cookbook gal and add yaml * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add cookbook gall gen to extensions in conf.py * sample yaml entry * update cookbook gallery generator * fix * rm menu_html * add to index * replace links to placeholder with gallery * Update portal/conf.py Co-authored-by: Drew Camron <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Drew Camron <[email protected]>
1 parent 9a69683 commit f37196d

File tree

6 files changed

+134
-16
lines changed

6 files changed

+134
-16
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import pathlib
2+
from textwrap import dedent
3+
4+
import yaml
5+
from truncatehtml import truncate
6+
7+
8+
def build_from_items(items, filename, title='Gallery', subtitle=None, menu_html='', max_descr_len=300):
9+
10+
# Build the gallery file
11+
panels_body = []
12+
for item in items:
13+
if not item.get('thumbnail'):
14+
item['thumbnail'] = '/_static/images/ebp-logo.png'
15+
thumbnail = item['thumbnail']
16+
17+
author_strs = set()
18+
institution_strs = set()
19+
for a in item['authors']:
20+
author_name = a.get('name', 'Anonymous')
21+
author_email = a.get('email', None)
22+
if author_email:
23+
_str = f'<a href="mailto:{author_email}">{author_name}</a>'
24+
else:
25+
_str = author_name
26+
author_strs.add(_str)
27+
28+
institution_name = a.get('institution', None)
29+
if institution_name:
30+
institution_url = a.get('institution_url', None)
31+
if institution_url:
32+
_str = f'<a href="{institution_url}">{institution_name}</a>'
33+
else:
34+
_str = institution_name
35+
institution_strs.add(_str)
36+
37+
authors_str = f"<strong>Author:</strong> {', '.join(author_strs)}"
38+
if institution_strs:
39+
institutions_str = f"<strong>Institution:</strong> {' '.join(institution_strs)}"
40+
else:
41+
institutions_str = ''
42+
43+
ellipsis_str = '<a class="modal-btn"> ... more</a>'
44+
short_description = truncate(item['description'], max_descr_len, ellipsis=ellipsis_str)
45+
46+
if ellipsis_str in short_description:
47+
modal_str = f"""
48+
<div class="modal">
49+
<div class="content">
50+
<img src="{thumbnail}" class="modal-img" />
51+
<h3 class="display-3">{item["title"]}</h3>
52+
{authors_str}
53+
<br/>
54+
{institutions_str}
55+
<p class="my-2">{item['description']}</p>
56+
<p class="mt-3 mb-0"><a href="{item["url"]}" class="btn btn-outline-primary btn-block">Visit Website</a></p>
57+
</div>
58+
</div>
59+
"""
60+
else:
61+
modal_str = ''
62+
63+
panels_body.append(
64+
f"""\
65+
---
66+
:column:
67+
68+
<div class="d-flex gallery-card">
69+
<img src="{thumbnail}" class="gallery-thumbnail" />
70+
<div class="container">
71+
<a href="{item["url"]}" class="text-decoration-none"><h4 class="display-4 p-0">{item["title"]}</h4></a>
72+
<p class="card-subtitle">{authors_str}<br/>{institutions_str}</p>
73+
<p class="my-2">{short_description}</p>
74+
</div>
75+
</div>
76+
{modal_str}
77+
78+
"""
79+
)
80+
81+
panels_body = '\n'.join(panels_body)
82+
83+
if subtitle:
84+
stitle = f'<span class="display-3">Displaying "{subtitle}" tags</span>'
85+
else:
86+
stitle = ''
87+
88+
panels = f"""
89+
# {title}
90+
91+
{stitle}
92+
93+
{menu_html}
94+
95+
````{{panels}}
96+
:column: col-12
97+
:card: +mb-4 w-100
98+
:header: d-none
99+
:body: p-3 m-0
100+
:footer: p-1
101+
102+
{dedent(panels_body)}
103+
````
104+
105+
<div class="modal-backdrop"></div>
106+
<script src="/_static/custom.js"></script>
107+
"""
108+
109+
pathlib.Path(f'{filename}.md').write_text(panels)
110+
111+
112+
def main(app):
113+
114+
with open('cookbook_gallery.yaml') as fid:
115+
all_items = yaml.safe_load(fid)
116+
117+
title = 'Pythia Cookbooks Gallery'
118+
build_from_items(all_items, 'cookbook-gallery', title=title)
119+
120+
121+
def setup(app):
122+
app.connect('builder-inited', main)

portal/_static/thumbnails/cmip6.png

177 KB
Loading

portal/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'myst_nb',
3636
'sphinx_panels',
3737
'resource_gallery_generator',
38+
'cookbook_gallery_generator',
3839
]
3940

4041
# Define what extensions will parse which kind of source file
@@ -81,6 +82,7 @@
8182
html_sidebars = {
8283
'index': [],
8384
'resource-gallery': [],
85+
'cookbook-gallery': [],
8486
}
8587

8688
# HTML Theme-specific Options
@@ -101,7 +103,7 @@
101103
{'name': 'Home', 'url': 'https://projectpythia.org'},
102104
{'name': 'Foundations', 'url': 'https://foundations.projectpythia.org'},
103105
{'name': 'Resources', 'url': 'https://projectpythia.org/resource-gallery.html'},
104-
{'name': 'Cookbooks', 'url': 'https://projectpythia.org/cookbooks.html'},
106+
{'name': 'Cookbooks', 'url': 'https://projectpythia.org/cookbook-gallery.html'},
105107
{'name': 'Community', 'url': 'https://projectpythia.org/#join-us'},
106108
],
107109
'page_layouts': {'index': 'page-banner.html', 'resource-gallery': 'page-standalone.html'},

portal/cookbook_gallery.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- title: CMIP6
2+
url: http://gallery.pangeo.io/repos/pangeo-gallery/cmip6/
3+
description: |
4+
Examples of analysis of Google Cloud CMIP6 data using Pangeo tools.
5+
authors:
6+
- name: Max Grover
7+
thumbnail: /_static/thumbnails/cmip6.png

portal/cookbooks.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

portal/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ the example data used by the [Pythia Foundations Book](https://foundations.proje
9999
Coming soon!!! Pythia Cookbooks are collections of more advanced and domain-specific example workflows building on top of Pythia Foundations.
100100

101101
<span class="d-flex justify-content-center pt-1 pb-3">
102-
<a href="cookbooks.html" role="button" class="btn btn-primary btn-lg">
102+
<a href="cookbook-gallery.html" role="button" class="btn btn-primary btn-lg">
103103
Visit Pythia Cookbooks
104104
</a>
105105
</span>
@@ -382,6 +382,6 @@ maxdepth: 1
382382
about.md
383383
contributing.md
384384
code_of_conduct.md
385-
cookbooks.md
385+
cookbook-gallery.md
386386
resource-gallery.md
387387
```

0 commit comments

Comments
 (0)