|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import dataclasses |
| 4 | +import json |
| 5 | +import pathlib |
| 6 | +import subprocess |
| 7 | + |
| 8 | +import nbconvert.preprocessors |
| 9 | +import nbformat |
| 10 | + |
| 11 | +from .console import console |
| 12 | + |
| 13 | + |
| 14 | +@dataclasses.dataclass |
| 15 | +class Unison: |
| 16 | + def get_conda_kernel_path(self, env_name) -> str | None: |
| 17 | + command = ['conda', 'env', 'list', '--json'] |
| 18 | + try: |
| 19 | + output = subprocess.check_output(command).decode('ascii') |
| 20 | + envs = json.loads(output)['envs'] |
| 21 | + |
| 22 | + for env in envs: |
| 23 | + env = pathlib.Path(env) |
| 24 | + if env.stem == env_name: |
| 25 | + return env |
| 26 | + except Exception: |
| 27 | + pass |
| 28 | + |
| 29 | + finally: |
| 30 | + return None |
| 31 | + |
| 32 | + def set_kernel_name( |
| 33 | + self, |
| 34 | + notebook_in: str | pathlib.Path, |
| 35 | + kernel_name: str, |
| 36 | + *, |
| 37 | + notebook_out: str | pathlib.Path = None, |
| 38 | + ) -> None: |
| 39 | + if notebook_out is None: |
| 40 | + notebook_out = notebook_in |
| 41 | + |
| 42 | + notebook = nbformat.read(notebook_in, as_version=nbformat.NO_CONVERT) |
| 43 | + notebook['metadata']['kernelspec']['name'] = kernel_name |
| 44 | + nbformat.write(notebook, notebook_out) |
| 45 | + |
| 46 | + def get_kernel_name(self, notebook_in: str | pathlib.Path) -> str: |
| 47 | + notebook = nbformat.read(notebook_in, as_version=nbformat.NO_CONVERT) |
| 48 | + return notebook['metadata']['kernelspec']['name'] |
| 49 | + |
| 50 | + def clear_outputs( |
| 51 | + self, notebook_in: str | pathlib.Path, notebook_out: str | pathlib.Path = None |
| 52 | + ) -> None: |
| 53 | + if notebook_out is None: |
| 54 | + notebook_out = notebook_in |
| 55 | + |
| 56 | + notebook = nbformat.read(notebook_in, as_version=nbformat.NO_CONVERT) |
| 57 | + if not isinstance(notebook['cells'], list): |
| 58 | + raise TypeError('Notebook cells must be a list') |
| 59 | + |
| 60 | + cells = [] |
| 61 | + for cell in notebook['cells']: |
| 62 | + if cell['cell_type'] == 'code': |
| 63 | + cell['execution_count'] = None |
| 64 | + cell['outputs'] = [] |
| 65 | + cells.append(cell) |
| 66 | + notebook['cells'] = cells |
| 67 | + nbformat.write(notebook, notebook_out) |
| 68 | + |
| 69 | + def execute( |
| 70 | + self, |
| 71 | + notebook_in: str | pathlib.Path, |
| 72 | + output_dir: str | pathlib.Path = '.', |
| 73 | + *, |
| 74 | + timeout: int = None, |
| 75 | + ) -> None | str: |
| 76 | + |
| 77 | + with open(notebook_in, encoding='utf-8') as f: |
| 78 | + notebook = nbformat.read(f, as_version=nbformat.NO_CONVERT) |
| 79 | + |
| 80 | + executor = nbconvert.preprocessors.ExecutePreprocessor(timeout=timeout) |
| 81 | + |
| 82 | + try: |
| 83 | + output = executor.preprocess(notebook, {'metadata': {'path': './'}}) |
| 84 | + except nbconvert.preprocessors.CellExecutionError: |
| 85 | + output = None |
| 86 | + msg = f'Error executing the notebook "{notebook_in}".\n' |
| 87 | + msg += f'See notebook "{notebook_in}" for the traceback.\n' |
| 88 | + console.print(msg) |
| 89 | + |
| 90 | + finally: |
| 91 | + notebook_out = pathlib.Path(output_dir) / notebook_in.stem |
| 92 | + with open(notebook_out, 'w', encoding='utf-8') as f: |
| 93 | + nbformat.write(notebook, f) |
| 94 | + |
| 95 | + console.print(f'Notebook "{notebook_out}" saved.') |
| 96 | + |
| 97 | + return output |
0 commit comments