|
| 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, "*.png"))) |
| 51 | + htmls = sorted(glob(os.path.join(examples_dirs, "*.html"))) |
| 52 | + image_path_iterator = block_vars["image_path_iterator"] |
| 53 | + image_names = list() |
| 54 | + seen = set() |
| 55 | + for html, png in zip(htmls, pngs): |
| 56 | + if png not in seen: |
| 57 | + seen |= set(png) |
| 58 | + this_image_path_png = next(image_path_iterator) |
| 59 | + this_image_path_html = os.path.splitext(this_image_path_png)[0] + ".html" |
| 60 | + image_names.append(this_image_path_html) |
| 61 | + shutil.move(png, this_image_path_png) |
| 62 | + shutil.move(html, this_image_path_html) |
| 63 | + # Use the `figure_rst` helper function to generate rST for image files |
| 64 | + return figure_rst(image_names, gallery_conf["src_dir"]) |
| 65 | + |
| 66 | + |
| 67 | +def figure_rst(figure_list, sources_dir): |
| 68 | + """Generate RST for a list of PNG filenames. |
| 69 | +
|
| 70 | + Depending on whether we have one or more figures, we use a |
| 71 | + single rst call to 'image' or a horizontal list. |
| 72 | +
|
| 73 | + Parameters |
| 74 | + ---------- |
| 75 | + figure_list : list |
| 76 | + List of strings of the figures' absolute paths. |
| 77 | + sources_dir : str |
| 78 | + absolute path of Sphinx documentation sources |
| 79 | +
|
| 80 | + Returns |
| 81 | + ------- |
| 82 | + images_rst : str |
| 83 | + rst code to embed the images in the document |
| 84 | + """ |
| 85 | + |
| 86 | + figure_paths = [ |
| 87 | + os.path.relpath(figure_path, sources_dir).replace(os.sep, "/").lstrip("/") |
| 88 | + for figure_path in figure_list |
| 89 | + ] |
| 90 | + images_rst = "" |
| 91 | + figure_name = figure_paths[0] |
| 92 | + ext = os.path.splitext(figure_name)[1] |
| 93 | + figure_path = os.path.join("images", os.path.basename(figure_name)) |
| 94 | + images_rst = SINGLE_HTML % figure_path |
| 95 | + return images_rst |
| 96 | + |
| 97 | + |
| 98 | +SINGLE_HTML = """ |
| 99 | +.. raw:: html |
| 100 | + :file: %s |
| 101 | +""" |
0 commit comments