-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new output filter that excludes widgets if there is no state (#…
…1643) This allows showing a static mimetype instead of a blank widget
- Loading branch information
1 parent
c663c75
commit 35c4d07
Showing
4 changed files
with
88 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters