{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "*This notebook contains material from [nbpages](https://jckantor.github.io/nbpages) by Jeffrey Kantor (jeff at nd.edu). The text is released under the\n", "[CC-BY-NC-ND-4.0 license](https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode).\n", "The code is released under the [MIT license](https://opensource.org/licenses/MIT).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [2.2 Examples](https://jckantor.github.io/nbpages/02.02-Examples.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [2.4 Working with Data and Figures](https://jckantor.github.io/nbpages/02.04-Working-with-Data-and-Figures.html) >

\"Open

\"Download\"" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 1, "link": "[2.3 Heirarchical Tagging](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3-Heirarchical-Tagging)", "section": "2.3 Heirarchical Tagging" } }, "source": [ "# 2.3 Heirarchical Tagging" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[2.3.1 Examples of Cell Tagging](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.1-Examples-of-Cell-Tagging)", "section": "2.3.1 Examples of Cell Tagging" } }, "source": [ "## 2.3.1 Examples of Cell Tagging" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[2.3.1.1 References](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.1.1-References)", "section": "2.3.1.1 References" } }, "source": [ "### 2.3.1.1 References" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[2.3.1.1 References](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.1.1-References)", "section": "2.3.1.1 References" }, "tags": [ "author::FRB::" ] }, "source": [ "Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. *Elementary principles of chemical processes 4th Ed.* NY: Wiley, 1986." ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[2.3.1.1 References](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.1.1-References)", "section": "2.3.1.1 References" }, "tags": [ "author::JEJ::" ] }, "source": [ "Jones, James Earl. *Darth Vader's Introduction to Chemical Engineering.* Empire Press, 2034." ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[2.3.1.1 References](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.1.1-References)", "section": "2.3.1.1 References" }, "tags": [ "author::EWT::" ] }, "source": [ "Thiele, Ernest W. \"Relation between catalytic activity and size of particle.\" Industrial & Engineering Chemistry 31.7 (1939): 916-920." ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[2.3.1.2 Some Chemical Engineering Content](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.1.2-Some-Chemical-Engineering-Content)", "section": "2.3.1.2 Some Chemical Engineering Content" }, "tags": [ "author::EWT::diffusion-reaction", "concept::Thiele-Modulus", "author::FRB::Chapter-1" ] }, "source": [ "### 2.3.1.2 Some Chemical Engineering Content\n", "\n", "One 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}}$$" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 3, "link": "[2.3.1.3 Homework and Exercises](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.1.3-Homework-and-Exercises)", "section": "2.3.1.3 Homework and Exercises" } }, "source": [ "### 2.3.1.3 Homework and Exercises" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[2.3.2 Tags](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.2-Tags)", "section": "2.3.2 Tags" } }, "source": [ "## 2.3.2 Tags\n", "\n", "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 ``::``." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "nbpages": { "level": 2, "link": "[2.3.2 Tags](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.2-Tags)", "section": "2.3.2 Tags" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['author::FRB::']\n", "['author::JEJ::']\n", "['author::EWT::']\n", "['author::EWT::diffusion-reaction', 'concept::Thiele-Modulus', 'author::FRB::Chapter-1']\n" ] } ], "source": [ "import nbformat\n", "\n", "nb_filename = \"02.03-Heirarchical-Tagging.ipynb\"\n", "nb = nbformat.read(nb_filename, as_version=4)\n", "\n", "for cell in nb.cells:\n", " if \"tags\" in cell.metadata.keys():\n", " print(cell.metadata[\"tags\"])" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[2.3.3 Indexing with defaultdict](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.3-Indexing-with-defaultdict)", "section": "2.3.3 Indexing with defaultdict" } }, "source": [ "## 2.3.3 Indexing with defaultdict\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "nbpages": { "level": 2, "link": "[2.3.3 Indexing with defaultdict](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.3-Indexing-with-defaultdict)", "section": "2.3.3 Indexing with defaultdict" } }, "outputs": [ { "data": { "text/plain": [ "defaultdict(list,\n", " {('author',\n", " 'FRB',\n", " ''): [('[cell 3](02.03-Heirarchical-Tagging.ipynb#cell3)',\n", " 'Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. *Elementary principles of chemical processes 4th Ed.* NY: Wiley, 1986.')],\n", " ('author',\n", " 'JEJ',\n", " ''): [('[cell 4](02.03-Heirarchical-Tagging.ipynb#cell4)',\n", " \"Jones, James Earl. *Darth Vader's Introduction to Chemical Engineering.* Empire Press, 2034.\")],\n", " ('author',\n", " 'EWT',\n", " ''): [('[cell 5](02.03-Heirarchical-Tagging.ipynb#cell5)',\n", " 'Thiele, Ernest W. \"Relation between catalytic activity and size of particle.\" Industrial & Engineering Chemistry 31.7 (1939): 916-920.')],\n", " ('author',\n", " 'EWT',\n", " 'diffusion-reaction'): [('[cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)',\n", " \"### 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}}$$\")],\n", " ('concept',\n", " 'Thiele-Modulus'): [('[cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)',\n", " \"### 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}}$$\")],\n", " ('author',\n", " 'FRB',\n", " 'Chapter-1'): [('[cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)',\n", " \"### 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}}$$\")]})" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from collections import defaultdict\n", "\n", "index = defaultdict(list)\n", "\n", "for k, cell in enumerate(nb.cells):\n", " if \"tags\" in cell.metadata.keys():\n", " for tag in cell.metadata[\"tags\"]:\n", " key = tuple(tag.split(\"::\"))\n", " link = f\"[cell {k}]({nb_filename}#cell{k})\"\n", " txt = cell.source\n", " index[key].append((link, txt))\n", "\n", "index" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[2.3.4 Sorting](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.4-Sorting)", "section": "2.3.4 Sorting" } }, "source": [ "## 2.3.4 Sorting\n", "\n", "The keys of the dictionary can be sorted with a multi-index." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "nbpages": { "level": 2, "link": "[2.3.4 Sorting](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.4-Sorting)", "section": "2.3.4 Sorting" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('author', 'EWT', '')\n", "('author', 'EWT', 'diffusion-reaction')\n", "('author', 'FRB', '')\n", "('author', 'FRB', 'Chapter-1')\n", "('author', 'JEJ', '')\n", "('concept', 'Thiele-Modulus')\n" ] } ], "source": [ "sort_fcn = lambda key: [s.lower() for s in key]\n", "for key in sorted(index.keys(), key=sort_fcn):\n", " print(key)" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[2.3.5 Printing](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.5-Printing)", "section": "2.3.5 Printing" } }, "source": [ "## 2.3.5 Printing" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "nbpages": { "level": 2, "link": "[2.3.5 Printing](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.5-Printing)", "section": "2.3.5 Printing" }, "scrolled": false }, "outputs": [ { "data": { "text/markdown": [ "3 deep heirarchy\n", "\n", "## author\n", "\n", "### EWT\n", "Thiele, Ernest W. \"Relation between catalytic activity and size of particle.\" Industrial & Engineering Chemistry 31.7 (1939): 916-920.\n", "\n", "#### diffusion-reaction\n", "* [cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)\n", "\n", "### FRB\n", "Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. *Elementary principles of chemical processes 4th Ed.* NY: Wiley, 1986.\n", "\n", "#### Chapter-1\n", "* [cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)\n", "\n", "### JEJ\n", "Jones, James Earl. *Darth Vader's Introduction to Chemical Engineering.* Empire Press, 2034.\n", "\n", "## concept\n", "\n", "### Thiele-Modulus\n", "* [cell 6](02.03-Heirarchical-Tagging.ipynb#cell6)\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import display, Markdown\n", "\n", "n = max(len(key) for key in index.keys())\n", "md = f\"{n} deep heirarchy\\n\"\n", "\n", "prev_fields = [\"\"]*n\n", "for key in sorted(index.keys(), key=sort_fcn):\n", " lvl = 0\n", " for field in key:\n", " if field != prev_fields[lvl]:\n", " if field != \"\":\n", " md += \"\\n\" + \"#\"*(lvl+2) + f\" {field}\\n\"\n", " if lvl == len(key) - 1:\n", " for link, txt in index[key]:\n", " if field:\n", " md += f\"* {link}\\n\"\n", " else:\n", " md += f\"{txt}\\n\"\n", " prev_fields[lvl] = field\n", " lvl += 1\n", " \n", "display(Markdown(md))" ] }, { "cell_type": "markdown", "metadata": { "nbpages": { "level": 2, "link": "[2.3.6 Linking to references](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.6-Linking-to-references)", "section": "2.3.6 Linking to references" } }, "source": [ "## 2.3.6 Linking to references" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "nbpages": { "level": 2, "link": "[2.3.6 Linking to references](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.6-Linking-to-references)", "section": "2.3.6 Linking to references" } }, "outputs": [], "source": [ "references = {\n", " (\"author\", \"FRB\"): \"Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. *Elementary principles of chemical processes 4th Ed.* NY: Wiley, 1986.\",\n", " (\"author\", \"JEJ\"): \"Jones, James Earl. *Darth Vader introduces chemical engineering.*\",\n", " (\"author\", \"Smith\"): \"xkcd. [Comic Relief](https://xkcd.com)\"\n", "}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "nbpages": { "level": 2, "link": "[2.3.6 Linking to references](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.6-Linking-to-references)", "section": "2.3.6 Linking to references" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 deep heirarchy\n", "\n", "* author\n", " * EWT\n", " [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.')]()\n", "\n", " * diffusion-reaction\n", " [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}}$$\")]()\n", "\n", " Felder, Richard M., Ronald W. Rousseau, and Lisa G. Bullard. *Elementary principles of chemical processes 4th Ed.* NY: Wiley, 1986.\n", " * \n", " [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.')]()\n", "\n", " * Chapter-1\n", " [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}}$$\")]()\n", "\n", " Jones, James Earl. *Darth Vader introduces chemical engineering.*\n", " * \n", " [tagged on line ('[cell 4](02.03-Heirarchical-Tagging.ipynb#cell4)', \"Jones, James Earl. *Darth Vader's Introduction to Chemical Engineering.* Empire Press, 2034.\")]()\n", "\n", "* concept\n", " * Thiele-Modulus\n", " [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}}$$\")]()\n", "\n" ] } ], "source": [ "n = max(len(key) for key in index.keys())\n", "print(n, \"deep heirarchy\\n\")\n", "\n", "prev_fields = [\"\"]*n\n", "for key in sorted(index.keys(), key=sort_fcn):\n", " lvl = 0\n", " for field in key:\n", " if field != prev_fields[lvl]:\n", " prev_fields[lvl] = field\n", " search_key = tuple(prev_fields[0:lvl+1])\n", " if search_key in references.keys():\n", " print(\" \"*lvl + references[search_key])\n", " elif lvl == len(key)-1:\n", " print(\" \"*lvl + f\"* {field}\")\n", " else:\n", " print(\" \"*lvl + f\"* {field}\")\n", " if lvl == len(key) - 1:\n", " for val in index[key]:\n", " print(\" \"*(lvl+1) + f\"[tagged on line {val}]()\")\n", " print(\"\")\n", " lvl += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbpages": { "level": 2, "link": "[2.3.6 Linking to references](https://jckantor.github.io/nbpages/02.03-Heirarchical-Tagging.html#2.3.6-Linking-to-references)", "section": "2.3.6 Linking to references" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [2.2 Examples](https://jckantor.github.io/nbpages/02.02-Examples.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [2.4 Working with Data and Figures](https://jckantor.github.io/nbpages/02.04-Working-with-Data-and-Figures.html) >

\"Open

\"Download\"" ] } ], "metadata": { "celltoolbar": "Tags", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }