-
Notifications
You must be signed in to change notification settings - Fork 495
/
execute_output_test.py
57 lines (44 loc) · 2.17 KB
/
execute_output_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
from voila.execute import executenb
from nbformat import read, NO_CONVERT
from copy import deepcopy
BASE_DIR = os.path.dirname(__file__)
WIDGET_MIME_TYPE_VIEW = 'application/vnd.jupyter.widget-view+json'
WIDGET_MIME_TYPE_STATE = 'application/vnd.jupyter.widget-state+json'
# based on nbconvert.preprocessors.tests.test_execute.TestExecute
# we cannot import it because pytest would then also execute thoses tests
def normalize_output(output):
"""
Normalizes outputs for comparison.
"""
output = dict(output)
if 'metadata' in output:
del output['metadata']
# tracebacks can be different per installation/python version
if 'traceback' in output:
del output['traceback']
if 'application/vnd.jupyter.widget-view+json' in output.get('data', {}):
output['data']['application/vnd.jupyter.widget-view+json']['model_id'] = '<MODEL_ID>'
def normalize_outputs(outputs):
for output in outputs:
normalize_output(output)
def test_execute_output():
path = os.path.join(BASE_DIR, "notebooks/output.ipynb")
nb = read(path, NO_CONVERT)
nb_voila = deepcopy(nb)
executenb(nb_voila)
widget_states = nb.metadata.widgets[WIDGET_MIME_TYPE_STATE]['state']
widget_states_voila = nb_voila.metadata.widgets[WIDGET_MIME_TYPE_STATE]['state']
for cell_voila, cell in zip(nb_voila.cells, nb.cells):
for output_voila, output in zip(cell_voila.outputs, cell.outputs):
if 'data' in output and WIDGET_MIME_TYPE_VIEW in output['data']:
widget_id = output['data'][WIDGET_MIME_TYPE_VIEW]['model_id']
widget_id_voila = output_voila['data'][WIDGET_MIME_TYPE_VIEW]['model_id']
widget_state = widget_states[widget_id]
widget_state_voila = widget_states_voila[widget_id_voila]
# if the widget is an output widget, it has the outputs, which we also check
assert normalize_outputs(widget_state.state.get('outputs', [])) ==\
normalize_outputs(widget_state_voila.state.get('outputs', []))
normalize_output(output_voila)
normalize_output(output)
assert output_voila == output