Skip to content

Commit

Permalink
Merge pull request #792 from vidartf/fix-widgets
Browse files Browse the repository at this point in the history
Update widgets CDN for ipywidgets 7 w/fallback
  • Loading branch information
SylvainCorlay authored Aug 7, 2018
2 parents 3a1eca8 + cb27fe1 commit d8bccf4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions nbconvert/nbconvertapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ def _postprocessor_class_changed(self, change):
if new:
self.postprocessor_factory = import_item(new)

ipywidgets_base_url = Unicode("https://unpkg.com/",
help="URL base for ipywidgets package").tag(config=True)


export_format = Unicode(
'html',
Expand Down Expand Up @@ -361,6 +364,8 @@ def init_single_notebook_resources(self, notebook_filename):

resources['output_files_dir'] = output_files_dir

resources['ipywidgets_base_url'] = self.ipywidgets_base_url

return resources

def export_single_notebook(self, notebook_filename, resources, input_buffer=None):
Expand Down
33 changes: 29 additions & 4 deletions nbconvert/templates/html/full.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,38 @@
{% set nb_title = nb.metadata.get('title', '') or resources['metadata']['name'] %}
<title>{{nb_title}}</title>

{%- if "widgets" in nb.metadata -%}
<script src="https://unpkg.com/jupyter-js-widgets@2.0.*/dist/embed.js"></script>
{%- endif-%}

<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

{% block ipywidgets %}
{%- if "widgets" in nb.metadata -%}
<script>
(function() {
function addWidgetsRenderer() {
var mimeElement = document.querySelector('script[type="application/vnd.jupyter.widget-view+json"]');
var scriptElement = document.createElement('script');
var widgetRendererSrc = '{{ resources.ipywidgets_base_url }}@jupyter-widgets/html-manager@*/dist/embed-amd.js';
var widgetState;
// Fallback for older version:
try {
widgetState = mimeElement && JSON.parse(mimeElement.innerHTML);
if (widgetState && (widgetState.version_major < 2 || !widgetState.version_major)) {
widgetRendererSrc = '{{ resources.ipywidgets_base_url }}jupyter-js-widgets@*/dist/embed.js';
}
} catch(e) {}
scriptElement.src = widgetRendererSrc;
document.body.appendChild(scriptElement);
}
document.addEventListener('DOMContentLoaded', addWidgetsRenderer);
}());
</script>
{%- endif -%}
{% endblock ipywidgets %}

{% for css in resources.inlining.css -%}
<style type="text/css">
{{ css }}
Expand Down

0 comments on commit d8bccf4

Please sign in to comment.