Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

works with ipywidgets7rc0 #16

Merged
merged 12 commits into from
Nov 30, 2017
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ The directives have the following options:
Button()
```

### Configuration

You conf.py has two extra configuration options:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be "optional", right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, optional indeed


* jupyter_sphinx_require_url: url for `require.js` (if your theme already provides this, set it to False or '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I say that I want to not use require (i.e., my theme doesn't provide it, and I don't want it on the page)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I misunderstood it them, if this param is set to a falsy, it will not include require, and choose the DEFAULT_EMBED_SCRIPT_URL default url.

Copy link
Member

@jasongrout jasongrout Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the docs there seemed to indicate that requirejs must be on the page. Let's reword, then. Perhaps:

* jupyter_sphinx_require_url: url for `require.js` (if requirejs is not needed or your theme already provides this, set it to False or '')

How do I say that my theme already provides require, so I must use the require embedding URL?

Thinking about it more, how about the following options:

  1. jupyter_sphinx_use_requirejs = True / False, defaults to True
  2. jupyter_sphinx_requirejs_url = some url, or the default of '' to automatically include
  3. jupyter_sphinx_embed_url = some url, or the default of '' to automatically choose an embed url based on jupyter_sphinx_use_requirejs

Copy link
Member

@jasongrout jasongrout Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, you still can't say "use the requirejs embedding, but don't include requirejs" - so maybe the requirejs url can be set to one value to indicate to automatically include a default version, a string for a specific version, and some other value to indicate to not include it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

  • jupyter_sphinx_embed_url: if not set will be chosen based on next config value (jupyter_sphinx_embed_with_require)
  • jupyter_sphinx_embed_with_require: True/False will fill in jupyter_sphinx_embed_url if not set
  • jupyter_sphinx_require_url: url to require.js, or empty to not include it.

* jupyter_sphinx_embed_url: url for the embedding, if set to None (default) a proper default will be taken from the `ipywidgets.embed` module.

### Misc.

- For the widgets to be succesfuly rendered, this extension requires an
Expand Down
56 changes: 49 additions & 7 deletions jupyter_sphinx/embed_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
from sphinx.util.nodes import set_source_info

import ast
import logging

logger = logging.getLogger(__name__)


def exec_then_eval(code, namespace=None):
"""Exec a code block & return evaluation of the last line"""
Expand Down Expand Up @@ -219,19 +223,56 @@ def add_widget_state(app, pagename, templatename, context, doctree):
Widget.widgets = {}
context['body'] += '<script type="application/vnd.jupyter.widget-state+json">' + state_spec + '</script>'

has_embed = False
try:
import ipywidgets.embed
has_embed = True
except ImportError:
pass

def builder_inited(app):
print(setup.config.jupyter_sphinx_require_url)
require_url = app.config.jupyter_sphinx_require_url
# 3 cases
# case 1: ipywidgets 6, only embed url
# case 2: ipywidgets 7, with require
# case 3: ipywidgets 7, no require
# (ipywidgets6 with require is not supported, require_url is ignored)
if has_embed:
if require_url:
app.add_javascript(require_url)
else:
if require_url:
logger.warning('Assuming ipywidgets6, ignoring jupyter_sphinx_require_url parameter')

if has_embed:
if require_url:
embed_url = app.config.jupyter_sphinx_embed_url or ipywidgets.embed.DEFAULT_EMBED_REQUIREJS_URL
else:
embed_url = app.config.jupyter_sphinx_embed_url or ipywidgets.embed.DEFAULT_EMBED_SCRIPT_URL
else:
embed_url = app.config.jupyter_sphinx_embed_url or 'https://unpkg.com/jupyter-js-widgets@^2.0.13/dist/embed.js'
print(">>>", has_embed, require_url, embed_url)
if embed_url:
app.add_javascript(embed_url)



def setup(app):
"""
case 1: ipywidgets 6, only embed url
case 2: ipywidgets 7, with require
case 3: ipywidgets 7, no require
"""
setup.app = app
setup.config = app.config
setup.confdir = app.confdir

app.add_stylesheet('https://unpkg.com/[email protected]/css/font-awesome.min.css')
embed_url = 'https://unpkg.com/jupyter-js-widgets@^2.0.13/dist/embed.js'
try:
import ipywidgets.embed
embed_url = ipywidgets.embed.DEFAULT_EMBED_SCRIPT_URL
except ImportError:
pass
app.add_javascript(embed_url)
require_url_default = 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js'
app.add_config_value('jupyter_sphinx_require_url', require_url_default, 'html')
app.add_config_value('jupyter_sphinx_embed_url', None, 'html')


app.add_node(widget,
html=(html_visit_widget, None),
Expand All @@ -244,6 +285,7 @@ def setup(app):
app.add_directive('ipywidgets-display', IPywidgetsDisplayDirective)
app.connect('html-page-context', add_widget_state)
app.connect('env-purge-doc', purge_widget_setup)
app.connect('builder-inited', builder_inited)

return {
'version': '0.1'
Expand Down