Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit ba887a6

Browse files
committed
Add main module
1 parent 0eca7b9 commit ba887a6

7 files changed

+122
-1
lines changed

ci/environment-docs.yml

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ dependencies:
1111
- sphinx-autobuild
1212
- sphinx-copybutton
1313
- sphinx-inline-tabs
14+
- rich
15+
- typer
16+
- jupyter_client
17+
- nbformat
18+
- nbconvert
1419
- pip:
1520
- sphinxext-opengraph
1621
- -e ..

ci/environment.yml

+5
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ channels:
55
dependencies:
66
- pytest-cov
77
- pre-commit
8+
- rich
9+
- typer
10+
- jupyter_client
11+
- nbformat
12+
- nbconvert

ci/upstream-dev-environment.yml

+5
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ channels:
55
dependencies:
66
- pytest-cov
77
- pre-commit
8+
- rich
9+
- typer
10+
- jupyter_client
11+
- nbformat
12+
- nbconvert

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ target-version = ['py38']
44
skip-string-normalization = true
55

66
[build-system]
7-
requires = ["setuptools>=45", "wheel", "setuptools_scm>=6.2"]
7+
requires = ["setuptools>=45", "wheel", "setuptools_scm>=6.2", "setuptools_scm[toml]>=6.2"]

requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rich>=11.2.0
2+
typer>=0.4.0
3+
nbformat
4+
jupyter-client

unison/console.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from __future__ import annotations
2+
3+
import rich.console
4+
5+
console = rich.console.Console()

unison/main.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)