|
| 1 | +""" |
| 2 | +Automatically add tags to notebook based on which PyMC3 classes are used. |
| 3 | +
|
| 4 | +E.g. if a notebook contains a section like |
| 5 | +
|
| 6 | + :::{post} 30 Aug, 2021 |
| 7 | + :tags: glm, mcmc, exploratory analysis |
| 8 | + :category: beginner |
| 9 | + ::: |
| 10 | +
|
| 11 | +in a markdown cell, and uses the class pymc3.Categorical, then this script |
| 12 | +will change that part of the markdown cell to: |
| 13 | +
|
| 14 | + :::{post} 30 Aug, 2021 |
| 15 | + :tags: glm, mcmc, exploratory analysis, pymc3.Categorical |
| 16 | + :category: beginner |
| 17 | + ::: |
| 18 | +
|
| 19 | +Example of how to run this: |
| 20 | +
|
| 21 | + python scripts/add_tags.py examples/getting_started.ipynb |
| 22 | +
|
| 23 | +""" |
| 24 | +import sys |
| 25 | +from myst_parser.main import to_tokens, MdParserConfig |
| 26 | +import subprocess |
| 27 | +import os |
| 28 | +import json |
| 29 | +import argparse |
| 30 | + |
| 31 | + |
| 32 | +def main(argv=None): |
| 33 | + parser = argparse.ArgumentParser() |
| 34 | + parser.add_argument("files", nargs="*") |
| 35 | + args = parser.parse_args(argv) |
| 36 | + |
| 37 | + for file in args.files: |
| 38 | + # Find which PyMC3 classes are used in the code. |
| 39 | + output = subprocess.run( |
| 40 | + [ |
| 41 | + "nbqa", |
| 42 | + "scripts.find_pm_classes", |
| 43 | + file, |
| 44 | + ], |
| 45 | + stdout=subprocess.PIPE, |
| 46 | + text=True, |
| 47 | + ) |
| 48 | + classes = {f"pymc3.{obj}" for obj in output.stdout.splitlines()} |
| 49 | + |
| 50 | + # Tokenize the notebook's markdown cells. |
| 51 | + with open(file, encoding="utf-8") as fd: |
| 52 | + content = fd.read() |
| 53 | + notebook = json.loads(content) |
| 54 | + markdown_cells = "\n".join( |
| 55 | + [ |
| 56 | + "\n".join(cell["source"]) |
| 57 | + for cell in notebook["cells"] |
| 58 | + if cell["cell_type"] == "markdown" |
| 59 | + ] |
| 60 | + ) |
| 61 | + config = MdParserConfig(enable_extensions=["dollarmath", "colon_fence"]) |
| 62 | + tokens = to_tokens(markdown_cells, config=config) |
| 63 | + |
| 64 | + # Find a ```{post} or :::{post} code block, and look for a line |
| 65 | + # starting with tags: or :tags:. |
| 66 | + tags = None |
| 67 | + for token in tokens: |
| 68 | + if token.tag == "code" and token.info.startswith("{post}"): |
| 69 | + for line in token.content.splitlines(): |
| 70 | + if line.startswith("tags: "): |
| 71 | + line_start = "tags: " |
| 72 | + original_line = line |
| 73 | + tags = {tag.strip() for tag in line[len(line_start) :].split(",")} |
| 74 | + break |
| 75 | + elif line.startswith(":tags: "): |
| 76 | + line_start = ":tags: " |
| 77 | + original_line = line |
| 78 | + tags = {tag.strip() for tag in line[len(line_start) :].split(",")} |
| 79 | + break |
| 80 | + |
| 81 | + # If tags were found, then append any PyMC3 classes which might have |
| 82 | + # been missed. |
| 83 | + if tags is not None: |
| 84 | + new_tags = ", ".join(sorted(tags.union(classes))) |
| 85 | + new_line = f"{line_start}{new_tags}" |
| 86 | + content = content.replace(original_line, new_line) |
| 87 | + with open(file, "w", encoding="utf-8") as fd: |
| 88 | + fd.write(content) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + exit(main()) |
0 commit comments