Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Jupyter examples #171

Merged
merged 7 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions plugins/render_jupyter_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,22 @@ def render_example(site, kw, in_name, out_name):
ipynb_html = lxml.html.fromstring(ipynb_raw)
code = lxml.html.tostring(ipynb_html, encoding="unicode")

data_files = set()
pattern = re.compile(r"data/([\w-]+?\.\w*)")
for cell in nb_json["cells"]:
if cell["cell_type"] != "code":
continue
for match in pattern.findall(cell["source"]):
data_files.add(match)

title = in_name.name

permalink = out_name.relative_to(kw["output_folder"])
source_link = permalink.stem # remove '.html'
context = {
"code": code,
"title": title,
"data_files": data_files,
"permalink": str(permalink),
"lang": kw["default_lang"],
"description": title,
Expand Down Expand Up @@ -169,10 +178,11 @@ def gen_tasks(self):
"jupyter-example-index.tmpl"
)
jupyter_headers = OrderedDict(
thermo=dict(name="Thermodynamics", files=[], summaries={}),
reactors=dict(name="Reactor Networks", files=[], summaries={}),
flames=dict(name="One-Dimensional Flames", files=[], summaries={}),
electrochemistry=dict(name="Electrochemistry", files=[], summaries={}),
thermo=dict(name="Thermodynamics", files=[], summaries={}, titles={}),
reactors=dict(name="Reactor Networks", files=[], summaries={}, titles={}),
flames=dict(name="One-Dimensional Flames", files=[], summaries={}, titles={}),
electrochemistry=dict(name="Electrochemistry", files=[], summaries={}, titles={}),
input=dict(name="Input Files", files=[], summaries={}, titles={}),
)

def get_b64_str(parent, img_fname):
Expand Down Expand Up @@ -201,11 +211,20 @@ def get_b64_str(parent, img_fname):
jupyter_headers[ex_category]["files"].append(jpy_ex_file)
data = json.loads(jpy_ex_file.read_text(encoding="utf-8"))
doc = ""
title = ""
for cell in data["cells"]:
if cell["cell_type"] != "markdown":
continue
if not doc:
doc = cell["source"][0].replace("#", "").strip()
doc = []
for line in cell["source"]:
if doc and title and not line.strip():
break
if not title and line.startswith("#"):
title = line.replace("#", "").strip()
else:
doc.append(line.strip())
doc = " ".join(doc)
for img_idx, cell_src in enumerate(cell["source"]):
if "img" in cell_src:
img = lxml.html.fromstring(cell_src)
Expand Down Expand Up @@ -245,6 +264,16 @@ def get_b64_str(parent, img_fname):
with cache_file.open(mode="w", encoding="utf-8") as jfile:
json.dump(data, jfile)

if not title or not doc:
self.logger.warn(
f"The Jupyter example {jpy_ex_file.name!s} doesn't have a parseable"
" title and/or summary"
)

if not title:
title = jpy_ex_file.name

jupyter_headers[ex_category]["titles"][jpy_ex_file.name] = title
jupyter_headers[ex_category]["summaries"][jpy_ex_file.name] = doc

out_name = kw["output_folder"].joinpath(
Expand Down
9 changes: 9 additions & 0 deletions themes/cantera/templates/examples.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
<small><a href="{{ source_link }}">({{ messages("Source") }})</a></small>
{% endif %}
</h1>
{% if data_files %}
Data files used in this example:
{% for i, file in enumerate(data_files) -%}
<a href="https://raw.githubusercontent.com/Cantera/cantera-jupyter/main/data/{{ file }}">{{ file }}</a>
{%- if i + 1 != data_files | length -%}
,
{% endif %}
{% endfor %}
{% endif %}
{{ code }}
{% endif %}
{% if title.endswith('.ipynb') %}
Expand Down
18 changes: 13 additions & 5 deletions themes/cantera/templates/jupyter-example-index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@
<p>Cantera examples in the form of <a href="https://jupyter.org" title="Jupyter">Jupyter</a>
notebooks. To see the rendered notebooks, browse the files below.</p>

<p><strong>Existing Cantera users</strong>: If you have Cantera and the Jupyter Notebook server
installed on your local machine, simply download any Jupyter notebook by clicking the "Source" link
at the top of each example page and you should be able to run it.</p>
<p><strong>Existing Cantera users</strong>: If you have Cantera and the Jupyter Notebook
server installed on your local machine, simply download any Jupyter notebook by clicking
the "Source" link at the top of each example page and you should be able to run it. For
some examples, you will also need to download the additional data files listed below the
"Source" link. These data files should either be placed in a subdirectory named "data"
one level up from the directory containing the notebook file, or the notebook can be
modified to point to the location of these data files on your computer. Alternatively,
you can download all of these examples and their accompanying data files by cloning the
<a href="https://github.com/Cantera/cantera-jupyter">cantera-jupyter</a> repository from
GitHub.
</p>

<p><strong>New Cantera Users</strong>: If you don't have an existing Cantera installation and you
just want to experiment, you can give Cantera a test drive in the cloud. Click on the Binder link
below to launch an interactive environment where you can run these examples. For this, there is no
installation required, but you cannot save and resume your work.</p>

<a href="https://mybinder.org:/repo/cantera/cantera-jupyter" rel="nofollow">
<a href="https://mybinder.org/v2/gh/Cantera/cantera-jupyter/main" rel="nofollow">
<img src="https://camo.githubusercontent.com/70c5b4d050d4019f4f20b170d75679a9316ac5e5/687474703a2f2f6d7962696e6465722e6f72672f62616467652e737667" alt="Binder" data-canonical-src="https://mybinder.org/badge.svg" style="max-width:100%;">
</a>

Expand All @@ -31,7 +39,7 @@ installation required, but you cannot save and resume your work.</p>
{% set fname = file.name %}
<!-- <div class="col-sm-4"> -->
<div class="card">
<a href="{{ head }}/{{ fname }}.html"><h5 class="card-header">{{ fname }}</h5></a>
<a href="{{ head }}/{{ fname }}.html"><h5 class="card-header">{{ file_dict['titles'][fname] }}</h5></a>
<div class="card-body">
<p class="card-text">{{ file_dict['summaries'][fname] }}</p>
</div>
Expand Down