-
Notifications
You must be signed in to change notification settings - Fork 208
Add ThebeLab support to notebook style galleries #713
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import sys | ||
import traceback | ||
import codeop | ||
import json | ||
|
||
from sphinx.errors import ExtensionError | ||
|
||
|
@@ -170,6 +171,27 @@ def __exit__(self, type_, value, tb): | |
<br /> | ||
<br />""" | ||
|
||
# Elements for ThebeLab | ||
thebelab_badge = """\n | ||
.. container:: sphx-glr-thebelab-badge | ||
|
||
.. image:: {0} | ||
:target: # | ||
:width: 260px | ||
|
||
.. raw:: html | ||
|
||
<script type="text/x-thebe-config"> | ||
{1} | ||
</script> | ||
<script src="https://unpkg.com/thebelab@latest/lib/index.js"></script> | ||
<script> | ||
$('.sphx-glr-thebelab-badge a').click( | ||
function(){{thebelab.bootstrap(); return false;}} | ||
); | ||
</script> | ||
""" | ||
|
||
|
||
def codestr2rst(codestr, lang='python', lineno=None): | ||
"""Return reStructuredText code block from code string.""" | ||
|
@@ -947,15 +969,20 @@ def rst_blocks(script_blocks, output_blocks, file_conf, gallery_conf): | |
|
||
code_rst = codestr2rst(bcontent, lang=gallery_conf['lang'], | ||
lineno=lineno) + '\n' | ||
example_rst += ".. container:: sphx-glr-code\n\n" | ||
if is_example_notebook_like: | ||
example_rst += code_rst | ||
example_rst += code_output | ||
example_rst += indent(code_rst, ' '*4) | ||
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. this is minor, but instead of doing
Reduces one cognitive step of dividing the number by the number of spaces per tab :-) |
||
if code_output.strip(): | ||
example_rst += " .. container:: sphx-glr-output\n" | ||
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. +1 to wrapping the cell / inputs / outputs in their own containers so they can be more explicitly tagged |
||
example_rst += indent(code_output, ' '*8) | ||
else: | ||
example_rst += code_output | ||
if code_output.strip(): | ||
example_rst += "\n .. container:: sphx-glr-output\n" | ||
example_rst += indent(code_output, ' '*8) | ||
if 'sphx-glr-script-out' in code_output: | ||
# Add some vertical space after output | ||
example_rst += "\n\n|\n\n" | ||
example_rst += code_rst | ||
example_rst += indent(code_rst, ' '*4) | ||
else: | ||
block_separator = '\n\n' if not bcontent.endswith('\n') else '\n' | ||
example_rst += bcontent + block_separator | ||
|
@@ -997,16 +1024,26 @@ def save_rst_example(example_rst, example_file, time_elapsed, | |
example_rst += ("**Estimated memory usage:** {0: .0f} MB\n\n" | ||
.format(memory_used)) | ||
|
||
# Generate a binder URL if specified | ||
binder_badge_rst = '' | ||
# Generate a binder or ThebeLab URL if specified/required | ||
badge_rst = '' | ||
if gallery_conf['thebelab'] and "sphx-glr-code" in example_rst: | ||
badge_rel_path = os.path.relpath( | ||
os.path.join(glr_path_static(), 'thebelab_badge.svg'), | ||
gallery_conf['src_dir']) | ||
conf = copy.deepcopy(gallery_conf['thebelab']) | ||
conf['kernelOptions']['path'] += '/' + os.path.relpath( | ||
os.path.dirname(example_file), | ||
gallery_conf['src_dir']) | ||
badge_rst += thebelab_badge.format( | ||
"/" + badge_rel_path.replace(os.path.sep, '/'), | ||
json.dumps(conf)) | ||
if len(binder_conf) > 0: | ||
binder_badge_rst += gen_binder_rst(example_file, binder_conf, | ||
gallery_conf) | ||
badge_rst += gen_binder_rst(example_file, binder_conf, gallery_conf) | ||
|
||
fname = os.path.basename(example_file) | ||
example_rst += CODE_DOWNLOAD.format(fname, | ||
replace_py_ipynb(fname), | ||
binder_badge_rst, | ||
badge_rst, | ||
ref_fname) | ||
example_rst += SPHX_GLR_SIG | ||
|
||
|
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.
I can't remember - does sphinx-gallery assume jquery is installed?