Skip to content

Commit 0178c09

Browse files
committed
add hide_errors argument in App to control error traceback display (#489)
1 parent 61eb69a commit 0178c09

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed

mercury/apps/nb/nbrun.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(
2525
is_presentation=False,
2626
reveal_theme="white",
2727
stop_on_error=False,
28+
hide_errors=True,
2829
user_info=None,
2930
):
3031
self.exporter = Exporter(show_code, show_prompt, is_presentation, reveal_theme)
@@ -38,10 +39,14 @@ def __init__(
3839
init_code += f"os.environ['MERCURY_USER_INFO']='{user_info}'"
3940
self.shell.run(init_code)
4041
self.stop_on_error = stop_on_error
42+
self.hide_errors = hide_errors
4143

4244
def set_stop_on_error(self, new_stop_on_error):
4345
self.stop_on_error = new_stop_on_error
4446

47+
def set_hide_errors(self, new_hide_errors):
48+
self.hide_errors = new_hide_errors
49+
4550
def set_show_code(self, new_show_code):
4651
self.exporter.set_show_code(new_show_code)
4752

@@ -80,6 +85,9 @@ def run_cell(self, cell, counter=None):
8085
return False
8186
if self.stop_on_error and output.get("output_type", "") == "error":
8287
return False
88+
if self.hide_errors and output.get("output_type", "") == "error":
89+
cell.outputs = []
90+
break
8391

8492
except Exception as e:
8593
pass

mercury/apps/nb/tests/test_nbrun.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,16 @@ def test_stop_on_error(self):
4343
nbrun = NbRun(stop_on_error=True)
4444
nbrun.run_notebook(nb)
4545
self.assertTrue(len(nb.cells[2].outputs) == 0)
46+
47+
48+
def test_hide_errors(self):
49+
nb = test_notebook(code=["not-working"])
50+
nb = dict2nb(nb)
51+
52+
nbrun = NbRun(hide_errors=False)
53+
nbrun.run_notebook(nb)
54+
self.assertTrue(len(nb.cells[0].outputs) > 0)
55+
56+
nbrun = NbRun(hide_errors=True)
57+
nbrun.run_notebook(nb)
58+
self.assertTrue(len(nb.cells[0].outputs) == 0)

mercury/apps/nbworker/nb.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ def init_notebook(self):
349349
is_presentation=self.is_presentation(),
350350
reveal_theme=self.reveal_theme(),
351351
stop_on_error=self.stop_on_error(),
352+
hide_errors=self.hide_errors(),
352353
user_info=self.get_user_info(),
353354
)
354355

@@ -387,6 +388,7 @@ def init_notebook(self):
387388
)
388389
self.nbrun.set_is_presentation(nb_params.get("output", "app") == "slides")
389390
self.nbrun.set_stop_on_error(nb_params.get("stop_on_error", False))
391+
self.nbrun.set_hide_errors(nb_params.get("hide_errors", True))
390392

391393
log.info(params)
392394
log.info(f"Exporter show_code {self.nbrun.exporter.show_code}")

mercury/apps/nbworker/rest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def update_notebook(self, new_params):
8787
"allow_download",
8888
"allow_share",
8989
"stop_on_error",
90+
"hide_errors"
9091
]:
9192
if new_params.get(property) is not None and nb_params.get(
9293
property
@@ -161,6 +162,17 @@ def stop_on_error(self):
161162
except Exception:
162163
log.exception("Exception when check if stop_on_error")
163164
return False
165+
166+
def hide_errors(self):
167+
try:
168+
hide_errors = str(
169+
json.loads(self.notebook.params).get("hide_errors", "true")
170+
).lower()
171+
log.info(f"Check if hide_errors ({hide_errors})")
172+
return hide_errors == "true"
173+
except Exception:
174+
log.exception("Exception when check if hide_errors")
175+
return False
164176

165177
def worker_state(self):
166178
return self.state

mercury/apps/ws/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def parse_params(nb, params={}):
135135
"allow_download",
136136
"allow_share",
137137
"stop_on_error",
138+
"hide_errors"
138139
]:
139140
if view.get(property) is not None:
140141
params[property] = view.get(property)
@@ -164,6 +165,8 @@ def parse_params(nb, params={}):
164165
params["allow_share"] = True
165166
if params.get("stop_on_error") is None:
166167
params["stop_on_error"] = False
168+
if params.get("hide_errors") is None:
169+
params["hide_errors"] = True
167170

168171
if no_outputs:
169172
params["version"] = "2"

mercury/widgets/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class App:
7878
The default is False, meaning the notebook will execute all cells even with
7979
errors.
8080
81+
hide_errors : bool, default True
82+
If True, the notebook will not display errors and exceptions from notebook
83+
execution. Please set to False if you would like to see error messages and
84+
traceback in the notebook.
85+
8186
Examples
8287
--------
8388
Constructing Mercury App with `title` and `description` arguments.
@@ -106,6 +111,7 @@ def __init__(
106111
allow_download=True,
107112
allow_share=True,
108113
stop_on_error=False,
114+
hide_errors=True,
109115
):
110116
self.code_uid = WidgetsManager.get_code_uid("App")
111117
self.title = title
@@ -122,6 +128,7 @@ def __init__(
122128
self.allow_download = allow_download
123129
self.allow_share = allow_share
124130
self.stop_on_error = stop_on_error
131+
self.hide_errors = hide_errors
125132
display(self)
126133

127134
def __repr__(self):
@@ -149,6 +156,7 @@ def _repr_mimebundle_(self, **kwargs):
149156
"allow_download": self.allow_download,
150157
"allow_share": self.allow_share,
151158
"stop_on_error": self.stop_on_error,
159+
"hide_errors": self.hide_errors,
152160
"model_id": "mercury-app",
153161
"code_uid": self.code_uid,
154162
}

0 commit comments

Comments
 (0)