8000 Initial changes to add ThebeLab support to notebook style galleries · sphinx-gallery/sphinx-gallery@64cb030 · GitHub
[go: up one dir, main page]

Skip to content

Commit 64cb030

Browse files
committed
Initial changes to add ThebeLab support to notebook style galleries
These initial changes allow for notebook style galleries to be run with ThebeLab, allow custom configuration for ThebeLab to be passed with details on repo, etc. Documentation and tests still required.
1 parent cf01b81 commit 64cb030

File tree

9 files changed

+77
-17
lines changed

9 files changed

+77
-17
lines changed

doc/_static/theme_override.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ a.sphx-glr-backref-module-sphinx_gallery {
3232

3333
.anim-state label {
3434
display: inline-block;
35+
}
36+
37+
.thebelab-cell {
38+
font-size: 12px;
3539
}

doc/conf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ def setup(app):
361361
# each code block
362362
'capture_repr': ('_repr_html_', '__repr__'),
363363
'matplotlib_animations': True,
364+
'thebelab': {
365+
"requestKernel": True,
366+
"binderOptions": {
367+
"repo": "sphinx-gallery/sphinx-gallery.github.io",
368+
},
369+
},
364370
}
365371

366372
# Remove matplotlib agg warnings from generated doc when using plt.show

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'_static/gallery-dataframe.css',
4545
'_static/no_image.png',
4646
'_static/broken_example.png',
47+
'_static/thebelab_badge.svg',
4748
]},
4849
scripts=['bin/copy_sphinxgallery.sh', 'bin/sphx_glr_python_to_jupyter.py'],
4950
url="https://sphinx-gallery.github.io",

sphinx_gallery/_static/gallery.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ div.sphx-glr-footer {
111111
text-align: center;
112112
}
113113

114+
div.sphx-glr-thebelab-badge {
115+
margin: 1em auto;
116+
vertical-align: middle;
117+
}
118+
114119
div.sphx-glr-download {
115120
margin: 1em auto;
116121
vertical-align: middle;
Lines changed: 1 addition & 0 deletions
Loading

sphinx_gallery/gen_gallery.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import os
2020
import pathlib
2121
from xml.sax.saxutils import quoteattr, escape
22+
import json
2223

2324
from sphinx.errors import ConfigError, ExtensionError
2425
from sphinx.util.console import red
@@ -77,6 +78,7 @@
7778
'inspect_global_variables': True,
7879
'css': _KNOWN_CSS,
7980
'matplotlib_animations': False,
81+
'thebelab': None,
8082
}
8183

8284
logger = sphinx_compatibility.getLogger('sphinx-gallery')
@@ -317,6 +319,17 @@ def call_memory(func):
317319
% (css, _KNOWN_CSS))
318320
if gallery_conf['app'] is not None: # can be None in testing
319321
gallery_conf['app'].add_css_file(css + '.css')
322+
if gallery_conf['thebelab'] is not None:
323+
gallery_conf['thebelab']['selector'] = \
324+
".sphx-glr-code>:not(.sphx-glr-output)"
325+
gallery_conf['thebelab']['outputSelector'] = ".sphx-glr-output"
326+
gallery_conf['thebelab']['predefinedOutput'] = True
327+
gallery_conf['app'].add_js_file(
328+
None,
329+
body=json.dumps(gallery_conf['thebelab']),
330+
type="text/x-thebe-config")
331+
gallery_conf['app'].add_js_file(
332+
"https://unpkg.com/thebelab@latest/lib/index.js")
320333

321334
return gallery_conf
322335

sphinx_gallery/gen_rst.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,23 @@ def __exit__(self, type_, value, tb):
167167
168168
{0}\n <br />\n <br />"""
169169

170+
# Elements for ThebeLab
171+
thebelab_badge = """\n
172+
.. container:: sphx-glr-thebelab-badge
173+
174+
.. image:: {0}
175+
:target: #
176+
:width: 260px
177+
178+
.. raw:: html
179+
180+
<script>
181+
$('.sphx-glr-thebelab-badge a').click(
182+
function(){{thebelab.bootstrap(); return false;}}
183+
);
184+
</script>
185+
"""
186+
170187

171188
def codestr2rst(codestr, lang='python', lineno=None):
172189
"""Return reStructuredText code block from code string"""
@@ -871,15 +888,19 @@ def rst_blocks(script_blocks, output_blocks, file_conf, gallery_conf):
871888

872889
code_rst = codestr2rst(bcontent, lang=gallery_conf['lang'],
873890
lineno=lineno) + '\n'
891+
example_rst += ".. container:: sphx-glr-code\n"
874892
if is_example_notebook_like:
875-
example_rst += code_rst
876-
example_rst += code_output
893+
example_rst += indent(code_rst, ' '*4)
894+
if code_output.strip():
895+
example_rst += " .. container:: sphx-glr-output\n"
896+
example_rst += indent(code_output, ' '*8)
877897
else:
878-
example_rst += code_output
898+
example_rst += "\n .. container:: sphx-glr-output\n"
899+
example_rst += indent(code_output, ' '*8)
879900
if 'sphx-glr-script-out' in code_output:
880901
# Add some vertical space after output
881902
example_rst += "\n\n|\n\n"
882-
example_rst += code_rst
903+
example_rst += indent(code_rst, ' '*4)
883904
else:
884905
block_separator = '\n\n' if not bcontent.endswith('\n') else '\n'
885906
example_rst += bcontent + block_separator
@@ -927,16 +948,21 @@ def save_rst_example(example_rst, example_file, time_elapsed,
927948
example_rst += ("**Estimated memory usage:** {0: .0f} MB\n\n"
928949
.format(memory_used))
929950

930-
# Generate a binder URL if specified
931-
binder_badge_rst = ''
951+
# Generate a binder or ThebeLab URL if specified/required
952+
badge_rst = ''
953+
if gallery_conf['thebelab'] and "sphx-glr-code" in example_rst:
954+
badge_rel_path = os.path.relpath(
955+
os.path.join(glr_path_static(), 'thebelab_badge.svg'),
956+
gallery_conf['src_dir'])
957+
badge_rst += thebelab_badge.format(
958+
"/" + badge_rel_path.replace(os.path.sep, '/'))
932959
if len(binder_conf) > 0:
933-
binder_badge_rst += gen_binder_rst(example_file, binder_conf,
934-
gallery_conf)
960+
badge_rst += gen_binder_rst(example_file, binder_conf, gallery_conf)
935961

936962
fname = os.path.basename(example_file)
937963
example_rst += CODE_DOWNLOAD.format(fname,
938964
replace_py_ipynb(fname),
939-
binder_badge_rst,
965+
badge_rst,
940966
ref_fname)
941967
example_rst += SPHX_GLR_SIG
942968

sphinx_gallery/tests/test_full.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,10 @@ def test_logging_std_nested(sphinx_app):
352352
sphinx_app.srcdir, 'auto_examples', 'plot_log.rst')
353353
with codecs.open(log_rst, 'r', 'utf-8') as fid:
354354
lines = fid.read()
355-
assert '.. code-block:: none\n\n is in the same cell' in lines
356-
assert '.. code-block:: none\n\n is not in the same cell' in lines
355+
assert ' .. code-block:: none\n\n'\
356+
' is in the same cell' in lines
357+
assert ' .. code-block:: none\n\n'\
358+
' is not in the same cell' in lines
357359

358360

359361
def _assert_mtimes(list_orig, list_new, different=(), ignore=()):

sphinx_gallery/tests/test_gen_rst.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,12 @@ def test_rst_empty_code_block(gallery_conf, tmpdir):
195195
196196
Paragraph 1
197197
198+
.. container:: sphx-glr-code
198199
199-
.. code-block:: python
200+
.. code-block:: python
200201
201202
202-
# just a comment"""
203+
# just a comment"""
203204

204205

205206
def test_script_vars_globals(gallery_conf, tmpdir):
@@ -463,11 +464,12 @@ def test_pattern_matching(gallery_conf, log_collector, req_pil):
463464
gallery_conf.update(image_scrapers=(), reset_modules=())
464465
gallery_conf.update(filename_pattern=re.escape(os.sep) + 'plot_0')
465466

466-
code_output = ('\n Out:\n\n .. code-block:: none\n'
467+
code_output = ('\n Out:\n\n'
468+
' .. code-block:: none\n'
467469
'\n'
468-
' Óscar output\n'
469-
' log:Óscar\n'
470-
' $\\langle n_\\uparrow n_\\downarrow \\rangle$'
470+
' Óscar output\n'
471+
' log:Óscar\n'
472+
' $\\langle n_\\uparrow n_\\downarrow \\rangle$'
471473
)
472474
warn_output = 'RuntimeWarning: WarningsAbound'
473475
# create three files in tempdir (only one matches the pattern)

0 commit comments

Comments
 (0)
0