Skip to content

Commit 88432f9

Browse files
committed
Hide nbconvert outputs
1 parent c8cb128 commit 88432f9

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

runbook/cli/commands/create.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from os import path
22

33
import click
4-
from nbconvert.nbconvertapp import NbConvertApp
4+
from runbook.cli.lib import nbconvert_launch_instance
55
from runbook.cli.validators import validate_create_language, validate_template
66

77

@@ -48,7 +48,7 @@ def create(ctx, filename, template, language):
4848
path.join("runbooks", "binder"),
4949
]
5050

51-
NbConvertApp().launch_instance(argv=argv)
51+
nbconvert_launch_instance(argv)
5252

5353
click.echo(
5454
click.style(

runbook/cli/commands/plan.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66

77
import click
8+
from runbook.cli.lib import nbconvert_launch_instance
89
from runbook.cli.validators import validate_plan_params, validate_runbook_file_path
910
from runbook.constants import RUNBOOK_METADATA
1011

@@ -96,9 +97,8 @@ def plan(ctx, input, embed, identifier="", params={}):
9697
# TODO: join the unified logic of create and plan
9798

9899
# TODO: hide the nbconvert verbose output?
99-
from nbconvert.nbconvertapp import NbConvertApp
100100

101-
NbConvertApp().launch_instance(argv=argv)
101+
nbconvert_launch_instance(argv)
102102

103103
for f in embed:
104104
shutil.copyfile(src=f, dst=f"{output_folder}/{path.basename(f)}")

runbook/cli/lib.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import hashlib
2+
import sys
3+
from io import StringIO
24

5+
from nbconvert.nbconvertapp import NbConvertApp
36
from ulid import ULID
47

58

@@ -14,3 +17,22 @@ def sha256sum(filename):
1417
# use ULID() directly
1518
def ts_id(length=10):
1619
return str(ULID())[0:length]
20+
21+
22+
# Suppresses nbconvert output
23+
def nbconvert_launch_instance(argv):
24+
# Temporarily redirect stdout/stderr to suppress nbconvert output
25+
stdout = sys.stdout
26+
stderr = sys.stderr
27+
sys.stdout = StringIO()
28+
sys.stderr = StringIO()
29+
try:
30+
NbConvertApp().launch_instance(argv=argv)
31+
except Exception as e:
32+
print(sys.stdout.getvalue())
33+
print(sys.stderr.getvalue())
34+
raise e
35+
finally:
36+
# Restore stdout/stderr
37+
sys.stdout = stdout
38+
sys.stderr = stderr

0 commit comments

Comments
 (0)