Skip to content

Commit

Permalink
Inheritance Diagram Dark Mode Support (#1834)
Browse files Browse the repository at this point in the history
Apply CSS filtering to have the inheritance diagram conform to dark mode, and add a example demonstrating inheritance diagram in kitchensink.. 

We also install graphviz on CI to test this. 

The css will only apply to inheritance diagram and not to all graphviz output.

Addresses #820
  • Loading branch information
j9ac9k committed May 29, 2024
1 parent e248a5a commit d7bbb79
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 6 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
pandoc: true
- name: "Install Graphviz on Windows 🗔"
if: runner.os == 'Windows'
run: choco install graphviz
- name: "Install Graphviz on macOS "
if: runner.os == 'macOS'
run: |
brew update
brew install graphviz
- name: "Install Graphviz on Linux 🐧"
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends graphviz
- name: "Build docs and check for warnings 📖"
shell: bash
run: |
Expand Down
14 changes: 14 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"sphinx.ext.autosummary",
"sphinx.ext.todo",
"sphinx.ext.viewcode",
"sphinx.ext.intersphinx",
"sphinx.ext.graphviz",
"sphinxext.rediraffe",
"sphinx_design",
"sphinx_copybutton",
Expand Down Expand Up @@ -60,6 +62,8 @@
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "**.ipynb_checkpoints"]

intersphinx_mapping = {"sphinx": ("https://www.sphinx-doc.org/en/master", None)}

# -- Sitemap -----------------------------------------------------------------

# ReadTheDocs has its own way of generating sitemaps, etc.
Expand Down Expand Up @@ -90,6 +94,16 @@
"jupyter": ("Jupyter", "https://jupyter.org"),
}


# -- sphinx_ext_graphviz options ---------------------------------------------

graphviz_output_format = "svg"
inheritance_graph_attrs = dict(
rankdir="LR",
fontsize=14,
ratio="compress",
)

# -- sphinx_togglebutton options ---------------------------------------------
togglebutton_hint = str(_("Click to expand"))
togglebutton_hint_hide = str(_("Click to collapse"))
Expand Down
29 changes: 29 additions & 0 deletions docs/examples/graphviz.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
========
Graphviz
========

Inheritance Diagram
-------------------

If you use :mod:`sphinx.ext.inheritance_diagram` to generate inheritance diagrams with
:mod:`sphinx.ext.graphviz`, and you output the inheritance diagrams in SVG format,
they will automatically adapt to this theme's light or dark mode.

To have the inheritance-diagram render to SVG, inside ``conf.py``, you need
the following option.

.. code-block:: python
# conf.py
...
graphviz_output_format = 'svg'
...
Below is an example of the inheritance diagram for ``matplotlib.figure.Figure``.
Try toggling light/dark mode to see it adapt!

.. inheritance-diagram:: matplotlib.figure.Figure

See the sphinx inheritance-diagram `documentation`_ for more information.

.. _documentation: https://www.sphinx-doc.org/en/master/usage/extensions/inheritance.html
1 change: 1 addition & 0 deletions docs/examples/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ See the sections in the primary sidebar and below to explore.
kitchen-sink/index
pydata
execution
graphviz


.. Note: the caption below is intentionally long in order to test out what long captions look like.
Expand Down
22 changes: 16 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[build-system]
requires = ["sphinx-theme-builder @ https://github.com/pradyunsg/sphinx-theme-builder/archive/87214d0671c943992c05e3db01dca997e156e8d6.zip"]
requires = [
"sphinx-theme-builder @ https://github.com/pradyunsg/sphinx-theme-builder/archive/87214d0671c943992c05e3db01dca997e156e8d6.zip",
]
build-backend = "sphinx_theme_builder"

[tool.sphinx-theme-builder]
Expand All @@ -10,7 +12,7 @@ additional-compiled-static-assets = [
"vendor/",
"styles/bootstrap.css",
"scripts/bootstrap.js",
"locale/"
"locale/",
]

[project]
Expand All @@ -27,7 +29,7 @@ dependencies = [
"Babel",
"pygments>=2.7",
"accessible-pygments",
"typing-extensions"
"typing-extensions",
]
license = { file = "LICENSE" }
maintainers = [
Expand All @@ -52,7 +54,7 @@ classifiers = [
[project.optional-dependencies]
doc = [
"numpydoc",
"linkify-it-py", # for link shortening
"linkify-it-py", # for link shortening
"rich",
"sphinxext-rediraffe",
"sphinx-sitemap",
Expand All @@ -76,10 +78,18 @@ doc = [
"nbsphinx",
"ipyleaflet",
"colorama",
"ipywidgets"
"ipywidgets",
"graphviz",
]
test = ["pytest", "pytest-cov", "pytest-regressions", "sphinx[test]"]
dev = ["pyyaml", "pre-commit", "pydata-sphinx-theme[doc,test]", "tox", "pandoc", "sphinx-theme-builder[cli]"]
dev = [
"pyyaml",
"pre-commit",
"pydata-sphinx-theme[doc,test]",
"tox",
"pandoc",
"sphinx-theme-builder[cli]",
]
a11y = ["pytest-playwright"]
i18n = ["Babel", "jinja2"]

Expand Down
2 changes: 2 additions & 0 deletions readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ build:
os: ubuntu-20.04
tools:
python: "3.10"
apt_packages:
- graphviz
jobs:
# build the gallery of themes before building the doc
post_install:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Styles for graphviz generated output from Sphinx */

/* Style the inheritance diagram such that it has a dark mode */
html[data-theme="dark"] div.graphviz > object.inheritance {
filter: brightness(0.8) invert(0.82) contrast(1.2);
color-scheme: normal;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
@import "./extensions/copybutton";
@import "./extensions/ethical-ads";
@import "./extensions/execution";
@import "./extensions/graphviz";
@import "./extensions/pydata";
@import "./extensions/sphinx_design";
@import "./extensions/togglebutton";
Expand Down

0 comments on commit d7bbb79

Please sign in to comment.