-
Notifications
You must be signed in to change notification settings - Fork 65
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
Changes from 10 commits
71390b4
32ff196
1185845
c08d66e
1c644da
d26c0b9
79da608
e31950f
7e9790a
d2b8b9e
e1f3dcb
c4c663c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,13 @@ The directives have the following options: | |
Button() | ||
``` | ||
|
||
### Configuration | ||
|
||
You conf.py has two extra configuration options: | ||
|
||
* jupyter_sphinx_require_url: url for `require.js` (if your theme already provides this, set it to False or '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about:
|
||
* 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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""" | ||
|
@@ -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), | ||
|
@@ -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' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be "optional", right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, optional indeed