Returns all code cells from the jupyter notebook at 'path'. If get_bash is True, only bash cells are returned, otherwise only python cells are returned. which_cells lets you supply a list of cell indicies to return.
(path, get_bash, which_cells: list[int] | None = None)
| 83 | |
| 84 | |
| 85 | def get_jupyter_code(path, get_bash, which_cells: list[int] | None = None) -> str: |
| 86 | """Returns all code cells from the jupyter notebook at 'path'. |
| 87 | |
| 88 | If get_bash is True, only bash cells are returned, otherwise only python |
| 89 | cells are returned. which_cells lets you supply a list of cell indicies to |
| 90 | return. |
| 91 | |
| 92 | """ |
| 93 | output = f"```{'bash' if get_bash else 'python'}\n" |
| 94 | with open(path, "r") as f: |
| 95 | j = json.load(f) |
| 96 | i = -1 |
| 97 | for cell in j["cells"]: |
| 98 | is_code = cell["cell_type"] == "code" |
| 99 | if len(cell["source"]) == 0: |
| 100 | continue |
| 101 | is_bash = "bash" in cell["source"][0] |
| 102 | is_copyright = "Copyright" in cell["source"][0] |
| 103 | if is_code and is_bash == get_bash and not is_copyright: |
| 104 | i += 1 |
| 105 | if which_cells is not None: |
| 106 | if i not in which_cells: |
| 107 | continue |
| 108 | for line in cell["source"]: |
| 109 | is_print = "print_readable" in line |
| 110 | is_bash_line = "bash" in line |
| 111 | is_setup_path = "setup_path.sh" in line |
| 112 | if not (is_print or is_bash_line or is_setup_path): |
| 113 | output += line |
| 114 | output += "\n" |
| 115 | output += "```\n" |
| 116 | return output |
| 117 | |
| 118 | |
| 119 | def generate_document(job: DocumentationJob): |
no test coverage detected