-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Scraper function for sphinx-gallery, in plotly.io #1577
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f6a5af8
scraper function for sphinx-gallery, in plotly.io
emmanuelle 343f2b2
added psutil dep to tox.ini
emmanuelle 0e7e209
python 2 compatibility
emmanuelle dd4cc35
added orca dep to package.json for tests
emmanuelle 2ea2fea
removed typo
emmanuelle 6909d76
corrected bug
emmanuelle 795061a
python 2 compatibility
emmanuelle 6654b46
added electron
emmanuelle b976752
try to configure orca
emmanuelle 65ea73f
Moved test file so that it's tested by the orca build only.
emmanuelle 6433175
use custom renderer for sphinx-gallery
emmanuelle c76736e
Merge branch 'master' into sphinxgallery
emmanuelle c353bc4
removed sphinx files which are now in plotly-sphinx-gallery
emmanuelle b210bc5
Modified test with new renderer name
emmanuelle e6f183e
try to fix py2 compatibility
emmanuelle 8b87a18
write_html instead of offline.plot
emmanuelle 3d881ab
change import order
emmanuelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# This module defines an image scraper for sphinx-gallery | ||
# https://sphinx-gallery.github.io/ | ||
# which can be used by projects using plotly in their documentation. | ||
import inspect, os | ||
|
||
import plotly | ||
from glob import glob | ||
import shutil | ||
|
||
plotly.io.renderers.default = 'sphinx_gallery' | ||
|
||
|
||
def plotly_sg_scraper(block, block_vars, gallery_conf, **kwargs): | ||
"""Scrape Plotly figures for galleries of examples using | ||
sphinx-gallery. | ||
|
||
Examples should use ``plotly.io.show()`` to display the figure with | ||
the custom sphinx_gallery renderer. | ||
|
||
Since the sphinx_gallery renderer generates both html and static png | ||
files, we simply crawl these files and give them the appropriate path. | ||
|
||
Parameters | ||
---------- | ||
block : tuple | ||
A tuple containing the (label, content, line_number) of the block. | ||
block_vars : dict | ||
Dict of block variables. | ||
gallery_conf : dict | ||
Contains the configuration of Sphinx-Gallery | ||
**kwargs : dict | ||
Additional keyword arguments to pass to | ||
:meth:`~matplotlib.figure.Figure.savefig`, e.g. ``format='svg'``. | ||
The ``format`` kwarg in particular is used to set the file extension | ||
of the output file (currently only 'png' and 'svg' are supported). | ||
|
||
Returns | ||
------- | ||
rst : str | ||
The ReSTructuredText that will be rendered to HTML containing | ||
the images. | ||
|
||
Notes | ||
----- | ||
Add this function to the image scrapers | ||
""" | ||
examples_dirs = gallery_conf['examples_dirs'] | ||
if isinstance(examples_dirs, (list, tuple)): | ||
examples_dirs = examples_dirs[0] | ||
pngs = sorted(glob(os.path.join(examples_dirs, | ||
'*.png'))) | ||
htmls = sorted(glob(os.path.join(examples_dirs, | ||
'*.html'))) | ||
image_path_iterator = block_vars['image_path_iterator'] | ||
image_names = list() | ||
seen = set() | ||
for html, png in zip(htmls, pngs): | ||
if png not in seen: | ||
seen |= set(png) | ||
this_image_path_png = next(image_path_iterator) | ||
this_image_path_html = (os.path.splitext( | ||
this_image_path_png)[0] + '.html') | ||
image_names.append(this_image_path_html) | ||
shutil.move(png, this_image_path_png) | ||
shutil.move(html, this_image_path_html) | ||
# Use the `figure_rst` helper function to generate rST for image files | ||
return figure_rst(image_names, gallery_conf['src_dir']) | ||
|
||
|
||
def figure_rst(figure_list, sources_dir): | ||
"""Generate RST for a list of PNG filenames. | ||
|
||
Depending on whether we have one or more figures, we use a | ||
single rst call to 'image' or a horizontal list. | ||
|
||
Parameters | ||
---------- | ||
figure_list : list | ||
List of strings of the figures' absolute paths. | ||
sources_dir : str | ||
absolute path of Sphinx documentation sources | ||
|
||
Returns | ||
------- | ||
images_rst : str | ||
rst code to embed the images in the document | ||
""" | ||
|
||
figure_paths = [os.path.relpath(figure_path, sources_dir) | ||
.replace(os.sep, '/').lstrip('/') | ||
for figure_path in figure_list] | ||
images_rst = "" | ||
figure_name = figure_paths[0] | ||
ext = os.path.splitext(figure_name)[1] | ||
figure_path = os.path.join('images', os.path.basename(figure_name)) | ||
images_rst = SINGLE_HTML % figure_path | ||
return images_rst | ||
|
||
|
||
SINGLE_HTML = """ | ||
.. raw:: html | ||
:file: %s | ||
""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import plotly | ||
import os | ||
import shutil | ||
import pytest | ||
|
||
|
||
# Fixtures | ||
# -------- | ||
@pytest.fixture() | ||
def setup(): | ||
# Reset orca state | ||
plotly.io.orca.config.restore_defaults(reset_server=False) | ||
|
||
|
||
# Run setup before every test function in this file | ||
pytestmark = pytest.mark.usefixtures("setup") | ||
|
||
|
||
def execute_plotly_example(): | ||
""" | ||
Some typical code which would go inside a gallery example. | ||
""" | ||
import plotly.graph_objs as go | ||
|
||
# Create random data with numpy | ||
import numpy as np | ||
|
||
N = 200 | ||
random_x = np.random.randn(N) | ||
random_y_0 = np.random.randn(N) | ||
random_y_1 = np.random.randn(N) - 1 | ||
|
||
# Create traces | ||
trace_0 = go.Scatter( | ||
x=random_x, | ||
y=random_y_0, | ||
mode='markers', | ||
name='Above', | ||
) | ||
|
||
fig = go.Figure(data=[trace_0]) | ||
plotly.io.show(fig) | ||
|
||
|
||
def test_scraper(): | ||
from plotly.io._sg_scraper import plotly_sg_scraper | ||
# test that monkey-patching worked ok | ||
assert plotly.io.renderers.default == 'sphinx_gallery' | ||
# Use dummy values for arguments of plotly_sg_scraper | ||
block = '' # we don't need actually code | ||
import tempfile | ||
tempdir = tempfile.mkdtemp() | ||
gallery_conf = {'src_dir':tempdir, | ||
'examples_dirs':'plotly/tests/test_orca'} | ||
names = iter(['0', '1', '2']) | ||
block_vars = {'image_path_iterator':names} | ||
execute_plotly_example() | ||
res = plotly_sg_scraper(block, block_vars, gallery_conf) | ||
shutil.rmtree(tempdir) | ||
assert ".. raw:: html" in res |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.