None
This notebook contains material from nbpages by Jeffrey Kantor (jeff at nd.edu). The text is released under the CC-BY-NC-ND-4.0 license. The code is released under the MIT license.
Here we read the current notebook and display the list of tags found in the notebook. The tags are comprised of series of strings separated by a ::
.
import nbformat
nb_filename = "02.03-Heirarchical-Tagging.ipynb"
nb = nbformat.read(nb_filename, as_version=4)
for cell in nb.cells:
if "tags" in cell.metadata.keys():
print(cell.metadata["tags"])
['author::FRB::'] ['author::JEJ::'] ['author::EWT::'] ['author::EWT::diffusion-reaction', 'concept::Thiele-Modulus', 'author::FRB::Chapter-1']
Scan the tags, splitting on ::
, and using the resulting tuple for keys in an index dictionary. The dictionary values are a list of items gathered from cell. In this case we use cell numbers to create a hypothetical link to notebook cell.
from collections import defaultdict
index = defaultdict(list)
for k, cell in enumerate(nb.cells):
if "tags" in cell.metadata.keys():
for tag in cell.metadata["tags"]:
key = tuple(tag.split("::"))
link = f"[cell {k}]({nb_filename}#cell{k})"
txt = cell.source
index[key].append((link, txt))
index
defaultdict(list, {('author', 'FRB', ''): [('[cell 3](02.03-Heirarchical-Tagging.ipynb#cell3)', 'Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. *Elementary principles of chemical processes 4th Ed.* NY: Wiley, 1986.')], ('author', 'JEJ', ''): [('[cell 4](02.03-Heirarchical-Tagging.ipynb#cell4)', "Jones, James Earl. *Darth Vader's Introduction to Chemical Engineering.* Empire Press, 2034.")], ('author', 'EWT', ''): [('[cell 5](02.03-Heirarchical-Tagging.ipynb#cell5)', 'Thiele, Ernest W. "Relation between catalytic activity and size of particle." Industrial & Engineering Chemistry 31.7 (1939): 916-920.')], ('author', 'EWT', 'diffusion-reaction'): [('[cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)', "### Some Chemical Engineering Content\n\nOne of the most impactful papers in the history of reaction engineering was Ernest Thiele's analysis of the relative roles of diffusion and reaction in a catalyst particle.\n\n$$h_T^2 = \\frac{\\text{reaction rate}}{\\text{diffusion rate}}$$")], ('concept', 'Thiele-Modulus'): [('[cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)', "### Some Chemical Engineering Content\n\nOne of the most impactful papers in the history of reaction engineering was Ernest Thiele's analysis of the relative roles of diffusion and reaction in a catalyst particle.\n\n$$h_T^2 = \\frac{\\text{reaction rate}}{\\text{diffusion rate}}$$")], ('author', 'FRB', 'Chapter-1'): [('[cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)', "### Some Chemical Engineering Content\n\nOne of the most impactful papers in the history of reaction engineering was Ernest Thiele's analysis of the relative roles of diffusion and reaction in a catalyst particle.\n\n$$h_T^2 = \\frac{\\text{reaction rate}}{\\text{diffusion rate}}$$")]})
The keys of the dictionary can be sorted with a multi-index.
sort_fcn = lambda key: [s.lower() for s in key]
for key in sorted(index.keys(), key=sort_fcn):
print(key)
('author', 'EWT', '') ('author', 'EWT', 'diffusion-reaction') ('author', 'FRB', '') ('author', 'FRB', 'Chapter-1') ('author', 'JEJ', '') ('concept', 'Thiele-Modulus')
from IPython.display import display, Markdown
n = max(len(key) for key in index.keys())
md = f"{n} deep heirarchy\n"
prev_fields = [""]*n
for key in sorted(index.keys(), key=sort_fcn):
lvl = 0
for field in key:
if field != prev_fields[lvl]:
if field != "":
md += "\n" + "#"*(lvl+2) + f" {field}\n"
if lvl == len(key) - 1:
for link, txt in index[key]:
if field:
md += f"* {link}\n"
else:
md += f"{txt}\n"
prev_fields[lvl] = field
lvl += 1
display(Markdown(md))
3 deep heirarchy
Thiele, Ernest W. "Relation between catalytic activity and size of particle." Industrial & Engineering Chemistry 31.7 (1939): 916-920.
Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. Elementary principles of chemical processes 4th Ed. NY: Wiley, 1986.
Jones, James Earl. Darth Vader's Introduction to Chemical Engineering. Empire Press, 2034.
references = {
("author", "FRB"): "Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. *Elementary principles of chemical processes 4th Ed.* NY: Wiley, 1986.",
("author", "JEJ"): "Jones, James Earl. *Darth Vader introduces chemical engineering.*",
("author", "Smith"): "xkcd. [Comic Relief](https://xkcd.com)"
}
n = max(len(key) for key in index.keys())
print(n, "deep heirarchy\n")
prev_fields = [""]*n
for key in sorted(index.keys(), key=sort_fcn):
lvl = 0
for field in key:
if field != prev_fields[lvl]:
prev_fields[lvl] = field
search_key = tuple(prev_fields[0:lvl+1])
if search_key in references.keys():
print(" "*lvl + references[search_key])
elif lvl == len(key)-1:
print(" "*lvl + f"* {field}")
else:
print(" "*lvl + f"* {field}")
if lvl == len(key) - 1:
for val in index[key]:
print(" "*(lvl+1) + f"[tagged on line {val}]()")
print("")
lvl += 1
3 deep heirarchy * author * EWT [tagged on line ('[cell 5](02.03-Heirarchical-Tagging.ipynb#cell5)', 'Thiele, Ernest W. "Relation between catalytic activity and size of particle." Industrial & Engineering Chemistry 31.7 (1939): 916-920.')]() * diffusion-reaction [tagged on line ('[cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)', "### Some Chemical Engineering Content\n\nOne of the most impactful papers in the history of reaction engineering was Ernest Thiele's analysis of the relative roles of diffusion and reaction in a catalyst particle.\n\n$$h_T^2 = \\frac{\\text{reaction rate}}{\\text{diffusion rate}}$$")]() Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. *Elementary principles of chemical processes 4th Ed.* NY: Wiley, 1986. * [tagged on line ('[cell 3](02.03-Heirarchical-Tagging.ipynb#cell3)', 'Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. *Elementary principles of chemical processes 4th Ed.* NY: Wiley, 1986.')]() * Chapter-1 [tagged on line ('[cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)', "### Some Chemical Engineering Content\n\nOne of the most impactful papers in the history of reaction engineering was Ernest Thiele's analysis of the relative roles of diffusion and reaction in a catalyst particle.\n\n$$h_T^2 = \\frac{\\text{reaction rate}}{\\text{diffusion rate}}$$")]() Jones, James Earl. *Darth Vader introduces chemical engineering.* * [tagged on line ('[cell 4](02.03-Heirarchical-Tagging.ipynb#cell4)', "Jones, James Earl. *Darth Vader's Introduction to Chemical Engineering.* Empire Press, 2034.")]() * concept * Thiele-Modulus [tagged on line ('[cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)', "### Some Chemical Engineering Content\n\nOne of the most impactful papers in the history of reaction engineering was Ernest Thiele's analysis of the relative roles of diffusion and reaction in a catalyst particle.\n\n$$h_T^2 = \\frac{\\text{reaction rate}}{\\text{diffusion rate}}$$")]()