|
| 1 | +# This module defines an image scraper for sphinx-gallery |
| 2 | +# https://sphinx-gallery.github.io/ |
| 3 | +# which can be used by projects using plotly in their documentation. |
| 4 | +import inspect, os |
| 5 | + |
| 6 | +import plotly |
| 7 | +from glob import glob |
| 8 | +import shutil |
| 9 | + |
| 10 | +plotly.io.renderers.default = 'sphinx_gallery' |
| 11 | + |
| 12 | + |
| 13 | +def plotly_sg_scraper(block, block_vars, gallery_conf, **kwargs): |
| 14 | + """Scrape Plotly figures for galleries of examples using |
| 15 | + sphinx-gallery. |
| 16 | +
|
| 17 | + Examples should use ``plotly.io.show()`` to display the figure with |
| 18 | + the custom sphinx_gallery renderer. |
| 19 | +
|
| 20 | + Since the sphinx_gallery renderer generates both html and static png |
| 21 | + files, we simply crawl these files and give them the appropriate path. |
| 22 | +
|
| 23 | + Parameters |
| 24 | + ---------- |
| 25 | + block : tuple |
| 26 | + A tuple containing the (label, content, line_number) of the block. |
| 27 | + block_vars : dict |
| 28 | + Dict of block variables. |
| 29 | + gallery_conf : dict |
| 30 | + Contains the configuration of Sphinx-Gallery |
| 31 | + **kwargs : dict |
| 32 | + Additional keyword arguments to pass to |
| 33 | + :meth:`~matplotlib.figure.Figure.savefig`, e.g. ``format='svg'``. |
| 34 | + The ``format`` kwarg in particular is used to set the file extension |
| 35 | + of the output file (currently only 'png' and 'svg' are supported). |
| 36 | +
|
| 37 | + Returns |
| 38 | + ------- |
| 39 | + rst : str |
| 40 | + The ReSTructuredText that will be rendered to HTML containing |
| 41 | + the images. |
| 42 | +
|
| 43 | + Notes |
| 44 | + ----- |
| 45 | + Add this function to the image scrapers |
| 46 | + """ |
| 47 | + examples_dirs = gallery_conf['examples_dirs'] |
| 48 | + if isinstance(examples_dirs, (list, tuple)): |
| 49 | + examples_dirs = examples_dirs[0] |
| 50 | + pngs = sorted(glob(os.path.join(examples_dirs, |
| 51 | + '*.png'))) |
| 52 | + htmls = sorted(glob(os.path.join(examples_dirs, |
| 53 | + '*.html'))) |
| 54 | + image_path_iterator = block_vars['image_path_iterator'] |
| 55 | + image_names = list() |
| 56 | + seen = set() |
| 57 | + for html, png in zip(htmls, pngs): |
| 58 | + if png not in seen: |
| 59 | + seen |= set(png) |
| 60 | + this_image_path_png = next(image_path_iterator) |
| 61 | + this_image_path_html = (os.path.splitext( |
| 62 | + this_image_path_png)[0] + '.html') |
| 63 | + image_names.append(this_image_path_html) |
| 64 | + shutil.move(png, this_image_path_png) |
| 65 | + shutil.move(html, this_image_path_html) |
| 66 | + # Use the `figure_rst` helper function to generate rST for image files |
| 67 | + return figure_rst(image_names, gallery_conf['src_dir']) |
| 68 | + |
| 69 | + |
| 70 | +def figure_rst(figure_list, sources_dir): |
| 71 | + """Generate RST for a list of PNG filenames. |
| 72 | +
|
| 73 | + Depending on whether we have one or more figures, we use a |
| 74 | + single rst call to 'image' or a horizontal list. |
| 75 | +
|
| 76 | + Parameters |
| 77 | + ---------- |
| 78 | + figure_list : list |
| 79 | + List of strings of the figures' absolute paths. |
| 80 | + sources_dir : str |
| 81 | + absolute path of Sphinx documentation sources |
| 82 | +
|
| 83 | + Returns |
| 84 | + ------- |
| 85 | + images_rst : str |
| 86 | + rst code to embed the images in the document |
| 87 | + """ |
| 88 | + |
| 89 | + figure_paths = [os.path.relpath(figure_path, sources_dir) |
| 90 | + .replace(os.sep, '/').lstrip('/') |
| 91 | + for figure_path in figure_list] |
| 92 | + images_rst = "" |
| 93 | + figure_name = figure_paths[0] |
| 94 | + ext = os.path.splitext(figure_name)[1] |
| 95 | + figure_path = os.path.join('images', os.path.basename(figure_name)) |
| 96 | + images_rst = SINGLE_HTML % figure_path |
| 97 | + return images_rst |
| 98 | + |
| 99 | + |
| 100 | +SINGLE_HTML = """ |
| 101 | +.. raw:: html |
| 102 | + :file: %s |
| 103 | +""" |
| 104 | + |
0 commit comments