Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/src/_static/theme_override.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ ul.squarelist {
text-indent: 1em;
padding-left: 5em;
}

.center {
text-align: center;
}
113 changes: 111 additions & 2 deletions docs/src/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from subprocess import run
import sys
from tempfile import gettempdir
import textwrap
from urllib.parse import quote
import warnings

Expand Down Expand Up @@ -412,12 +413,14 @@ def reset_modules(gallery_conf, fname):
)
sys.path.insert(0, str(reset_modules_dir))

GALLERY_CODE: str = "../gallery_code"
GALLERY_DIRS: str = "generated/gallery"

sphinx_gallery_conf = {
# path to your example scripts
"examples_dirs": ["../gallery_code"],
"examples_dirs": GALLERY_CODE,
# path to where to save gallery generated output
"gallery_dirs": ["generated/gallery"],
"gallery_dirs": GALLERY_DIRS,
# filename pattern for the files in the gallery
"filename_pattern": "/plot_",
# filename pattern to ignore in the gallery
Expand All @@ -441,3 +444,109 @@ def reset_modules(gallery_conf, fname):
"section": "Section %s",
"table": "Table %s",
}


def generate_carousel(
app: Sphinx,
fname: Path,
ncards: int | None = None,
margin: int | None = None,
width: int | None = None,
) -> None:
"""Generate and write the gallery carousel RST file."""
if ncards is None:
ncards = 3

if margin is None:
margin = 4

if width is None:
width = "25%"

base = Path(app.srcdir, *GALLERY_DIRS.split("/"))
cards_by_link = {}

card = r""".. card::
:img-background: {image}
:link: {link}
:link-type: ref
:width: {width}
:margin: {margin}
:class-card: align-self-center
"""

# TODO @bjlittle: use Path.walk when python >=3.12
for root, _, files in os.walk(str(base)):
root = Path(root) # noqa: PLW2901
if root.name == "images":
root_relative = root.relative_to(app.srcdir)
link_relative = root.parent.relative_to(app.srcdir)

for file in files:
path = Path(file)
if path.suffix == ".png":
# generate the card "img-background" filename
image = root_relative / path

# generate the card "link" reference
# remove numeric gallery image index e.g., "001"
parts = path.stem.split("_")[:-1]
link = parts[:2] + list(link_relative.parts) + parts[2:]
link = f"{'_'.join(link)}.py"

# needed in case a gallery filename has mixed case
link = link.lower()

kwargs = {
"image": image,
"link": link,
"width": width,
"margin": margin,
}

cards_by_link[link] = card.format(**kwargs)

# sort the cards by their link
cards = [cards_by_link[link] for link in sorted(cards_by_link.keys())]
cards = textwrap.indent("\n".join(cards), prefix=" " * 4)

# now, create the card carousel
carousel = f""".. card-carousel:: {ncards}

{cards}

.. rst-class:: center

:fa:`images` Gallery Carousel

"""

# finally, write the rst for the gallery carousel
Path(app.srcdir, fname).write_text(carousel)


def gallery_carousel(
app: Sphinx,
env: BuildEnvironment, # noqa: ARG001
docnames: list[str], # noqa: ARG001
) -> None:
"""Create the gallery carousel."""
# create empty or truncate existing file
fname = Path(app.srcdir, "gallery_carousel.txt")

with fname.open("w"):
pass

# if _bool_eval(arg=app.builder.config.plot_gallery):
# # only generate the carousel if we have a gallery
# generate_carousel(app, fname)
generate_carousel(app, fname)


def setup(app: Sphinx) -> None:
"""Configure sphinx application."""
# we require the output of this extension
app.setup_extension("sphinx_gallery.gen_gallery")

# register callback to generate gallery carousel
app.connect("env-before-read-docs", gallery_carousel)
4 changes: 4 additions & 0 deletions docs/src/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ representations become unwieldy and inefficient.

For more information see :ref:`why_iris`.


.. grid:: 3

.. grid-item-card::
Expand Down Expand Up @@ -128,6 +129,9 @@ For more information see :ref:`why_iris`.
Icons made by FreePik from `Flaticon <https://www.flaticon.com/>`_


.. include:: gallery_carousel.txt


.. _iris_support:

Support
Expand Down
2 changes: 1 addition & 1 deletion docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ This document explains the changes made to Iris for this release
📚 Documentation
================

#. N/A
#. `@tkknight`_ added a gallery carousel to the documentation homepage. (:pull:`6884`)


💼 Internal
Expand Down
3 changes: 2 additions & 1 deletion requirements/py311.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ dependencies:
- sphinx-copybutton
- sphinx-gallery >=0.11.0
- sphinx-design
- pydata-sphinx-theme >=0.13.0
# Pinned reason: https://github.com/SciTools/iris/issues/6885
- pydata-sphinx-theme !=0.16.0,!=0.16.1,<0.16.2

# Temporary minimum pins.
# See https://github.com/SciTools/iris/pull/5051
Expand Down
3 changes: 2 additions & 1 deletion requirements/py312.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ dependencies:
- sphinx-copybutton
- sphinx-gallery >=0.11.0
- sphinx-design
- pydata-sphinx-theme >=0.13.0
# Pinned reason: https://github.com/SciTools/iris/issues/6885
- pydata-sphinx-theme !=0.16.0,!=0.16.1,<0.16.2

# Temporary minimum pins.
# See https://github.com/SciTools/iris/pull/5051
Expand Down
3 changes: 2 additions & 1 deletion requirements/py313.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ dependencies:
- sphinx-copybutton
- sphinx-gallery >=0.11.0
- sphinx-design
- pydata-sphinx-theme >=0.13.0
# Pinned reason: https://github.com/SciTools/iris/issues/6885
- pydata-sphinx-theme !=0.16.0,!=0.16.1,<0.16.2

# Temporary minimum pins.
# See https://github.com/SciTools/iris/pull/5051
Expand Down
Loading