Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97d1ea7

Browse files
Add links to pyodite to examples
1 parent 721fc04 commit 97d1ea7

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

doc/conf.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ def _check_deps():
7575
import sphinxext.gallery_order as gallery_order
7676
# The following import is only necessary to monkey patch the signature later on
7777
from sphinx_gallery import gen_rst
78+
# import hack that replaces the sphinx-gallery links to include pyodide link
79+
import sphinxext.pyodide_links as pyodide_links
7880

7981
if shutil.which('dot') is None:
8082
raise OSError(
@@ -110,6 +112,14 @@ def _check_deps():
110112
'subsection_order': gallery_order.sectionorder,
111113
'within_subsection_order': gallery_order.subsectionorder,
112114
'min_reported_time': 1,
115+
'binder': {'org': 'matplotlib', # This config is wrong for binder, but
116+
'repo': 'github.com/matplotlib/matplotlib.github.com',
117+
'url': 'https://mybinder.org', # needs to be present
118+
'branch': 'master', # for the pyodide links to work.
119+
'notebooks_dir': '_downloads',
120+
'dependencies': 'sphinxext/binder_requirement/requirements.txt',
121+
'use_jupyter_lab': True,
122+
}
113123
}
114124

115125
plot_gallery = 'True'
@@ -124,6 +134,12 @@ def _check_deps():
124134
`Gallery generated by Sphinx-Gallery
125135
<https://sphinx-gallery.readthedocs.io>`_\n"""
126136

137+
138+
# Monkey patching binder from sphinx-gallery to include custom links
139+
#
140+
gen_rst.gen_binder_rst = pyodide_links.gen_links
141+
gen_rst.CODE_DOWNLOAD = pyodide_links.dlcode
142+
127143
# Add any paths that contain templates here, relative to this directory.
128144
templates_path = ['_templates']
129145

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Requirement file for binder links
2+
# This file is currently not used, but needs to be present and found
3+
# by sphinx-gallery to hook into the binder-link generation mechanism.
4+
matplotlib>=2.2

doc/sphinxext/pyodide_links.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
from fnmatch import fnmatch
3+
from sphinx_gallery import binder
4+
5+
# Blacklist contains patterns for which no binder/pyodide links
6+
# should be created
7+
blacklist = ["*_sgskip.py",
8+
"*pipong.py",
9+
"*pgf*.py",
10+
"*svg_filter*.py",
11+
"*font_indexing.py",
12+
"*ftface_props.py",
13+
"*multipage_pdf.py",
14+
"*pick_event_demo2.py"]
15+
16+
17+
# Rewrite "download" section
18+
# {0} is .py link, {1} is ipynb link,
19+
# {2} is additional rst text (used here for additional links)
20+
# {3} is ref_fname
21+
dlcode = """
22+
.. _sphx_glr_download_{3}:
23+
\n.. only :: html
24+
25+
.. container:: sphx-glr-footer
26+
:class: sphx-glr-footer-example
27+
28+
.. container:: sphx-glr-download
29+
30+
:download:`Download Python source code: {0} <{0}>`\n
31+
.. container:: sphx-glr-download
32+
33+
:download:`Download Jupyter notebook: {1} <{1}>`\n
34+
{2}
35+
"""
36+
37+
def gen_pyodide_url(fname):
38+
base = "https://iodide.io/pyodide-demo/matplotlib-sideload.html?sideload="
39+
head, tail = os.path.split(fname)
40+
return base + "https://matplotlib.org/_downloads/" + tail
41+
42+
def gen_links(fname, binder_conf, gallery_conf):
43+
"""Generate the RST + links for the additional links.
44+
This is a replaced version of the original gen_binder_rst.
45+
46+
Parameters
47+
----------
48+
fname: str
49+
The path to the `.py` file for which a Binder badge will be generated.
50+
binder_conf: dict | None
51+
If a dictionary it must have the following keys:
52+
'url': The URL of the BinderHub instance that's running a Binder
53+
service.
54+
'org': The GitHub organization to which the documentation will be
55+
pushed.
56+
'repo': The GitHub repository to which the documentation will be
57+
pushed.
58+
'branch': The Git branch on which the documentation exists (e.g.,
59+
gh-pages).
60+
'dependencies': A list of paths to dependency files that match the
61+
Binderspec.
62+
Returns
63+
-------
64+
rst : str
65+
The reStructuredText for the Binder badge and pyodide link.
66+
"""
67+
binder_conf = binder.check_binder_conf(binder_conf)
68+
binder_url = binder.gen_binder_url(fname, binder_conf, gallery_conf)
69+
pyodide_url = gen_pyodide_url(fname)
70+
71+
for pattern in blacklist:
72+
if fnmatch(fname, pattern):
73+
return "\n"
74+
75+
rst_binder = (
76+
"\n"
77+
" .. container:: sphx-glr-download\n\n"
78+
" `Open with binder (experimental) <{}>`_\n"
79+
).format(binder_url)
80+
rst_pyodide = (
81+
"\n"
82+
" .. container:: sphx-glr-download\n\n"
83+
" `Open with pyodide (experimental) <{}>`_\n"
84+
).format(pyodide_url)
85+
86+
# leave binder out for now , because configuration is unclear
87+
return rst_pyodide #+ rst_binder
88+

0 commit comments

Comments
 (0)