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

Skip to content

Commit d5d9ab8

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

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
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 pyodite link
79+
import sphinxext.pyodite_links as pyodite_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': 'matplotlib.github.io', # we need some 'binder' config set
117+
'url': 'https://mybinder.org', # for the pyodite links to work.
118+
'branch': 'master',
119+
'notebooks_dir': 'notebooks',
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 = pyodite_links.gen_links
141+
gen_rst.CODE_DOWNLOAD = pyodite_links.dlcode
142+
127143
# Add any paths that contain templates here, relative to this directory.
128144
templates_path = ['_templates']
129145

doc/make.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if "%SPHINXBUILD%" == "" (
1010
set SOURCEDIR=.
1111
set BUILDDIR=build
1212
set SPHINXPROJ=matplotlib
13-
set SPHINXOPTS=-W
13+
set SPHINXOPTS=
1414
set O=
1515

1616
%SPHINXBUILD% >NUL 2>NUL
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Requirement file for binder links
2+
matplotlib>=2.2

doc/sphinxext/pyodite_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/pyodite 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_pyodite_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 pyodite link.
66+
"""
67+
binder_conf = binder.check_binder_conf(binder_conf)
68+
binder_url = binder.gen_binder_url(fname, binder_conf, gallery_conf)
69+
pyodite_url = gen_pyodite_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_pyodite = (
81+
"\n"
82+
" .. container:: sphx-glr-download\n\n"
83+
" `Open with pyodite (experimental) <{}>`_\n"
84+
).format(pyodite_url)
85+
86+
# leave binder out for now , because configuration is unclear
87+
return rst_pyodite # + rst_binder
88+

0 commit comments

Comments
 (0)