forked from jupyter/nbconvert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgetsdatatypefilter.py
69 lines (51 loc) · 2.43 KB
/
widgetsdatatypefilter.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
58
59
60
61
62
63
64
65
66
67
68
69
"""Filter used to select the first preferred output format available,
excluding interactive widget format if the widget state is not available.
The filter contained in the file allows the converter templates to select
the output format that is most valuable to the active export format. The
value of the different formats is set via
NbConvertBase.display_data_priority
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
from warnings import warn
from ..utils.base import NbConvertBase
__all__ = ['WidgetsDataTypeFilter']
WIDGET_VIEW_MIMETYPE = 'application/vnd.jupyter.widget-view+json'
WIDGET_STATE_MIMETYPE = 'application/vnd.jupyter.widget-state+json'
class WidgetsDataTypeFilter(NbConvertBase):
""" Returns the preferred display format, excluding the widget output if
there is no widget state available """
def __init__(self, notebook_metadata=None, **kwargs):
metadata = notebook_metadata or {}
self.widgets_state = (
metadata['widgets'][WIDGET_STATE_MIMETYPE]['state'] if
metadata.get('widgets') is not None else {}
)
super().__init__(**kwargs)
def __call__(self, output):
""" Return the first available format in the priority.
Produces a UserWarning if no compatible mimetype is found.
`output` is dict with structure {mimetype-of-element: value-of-element}
"""
for fmt in self.display_data_priority:
if fmt in output:
# If there is no widget state available, we skip this mimetype
if (
fmt == WIDGET_VIEW_MIMETYPE and
output[WIDGET_VIEW_MIMETYPE]['model_id'] not in self.widgets_state
):
continue
return [fmt]
warn("Your element with mimetype(s) {mimetypes}"
" is not able to be represented.".format(
mimetypes=output.keys())
)
return []