Skip to content
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

Generate inheritance diagrams for models and submodels in the documentation #3074

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ sphinx:

# Set the version of Python and other tools you might need
build:
# Install Graphviz to build SVG files
apt_packages:
- "graphviz"
agriyakhetarpal marked this conversation as resolved.
Show resolved Hide resolved
os: ubuntu-22.04
tools:
python: "3.11"
Expand Down
77 changes: 69 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
import sys
import pybamm

# Path for repository root
sys.path.insert(0, os.path.abspath("../"))

# Path for local Sphinx extensions
sys.path.append(os.path.abspath("./sphinxext/"))


# -- Project information -----------------------------------------------------

Expand All @@ -41,16 +45,21 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
# Sphinx extensions
"sphinx.ext.autodoc",
"sphinx.ext.doctest",
"sphinx.ext.intersphinx",
"sphinx.ext.mathjax",
"sphinx.ext.viewcode",
"sphinx.ext.napoleon",
"sphinx.ext.inheritance_diagram",
# Local and custom extensions
"extend_parent",
"inheritance_diagram",
# Third-party extensions
"sphinx_design",
"sphinx_copybutton",
"myst_parser",
"sphinx_extend_parent",
"sphinx_inline_tabs",
"sphinxcontrib.bibtex",
"sphinx_last_updated_by_git",
Expand All @@ -60,13 +69,6 @@
"hoverxref.extension",
]

# -- sphinxcontrib-bibtex configuration --------------------------------------
bibtex_bibfiles = ["../pybamm/CITATIONS.bib"]
bibtex_style = "unsrt"
bibtex_footbibliography_header = """.. rubric:: References"""
bibtex_reference_style = "author_year"
bibtex_tooltips = True


napoleon_use_rtype = True
napoleon_google_docstring = False
Expand Down Expand Up @@ -273,6 +275,14 @@
"matplotlib": ("https://matplotlib.org/stable/", None),
}

# -- sphinxcontrib-bibtex configuration --------------------------------------

bibtex_bibfiles = ["../pybamm/CITATIONS.bib"]
bibtex_style = "unsrt"
bibtex_footbibliography_header = """.. rubric:: References"""
bibtex_reference_style = "author_year"
bibtex_tooltips = True

# -- nbsphinx configuration options ------------------------------------------

nbsphinx_prolog = r"""
Expand Down Expand Up @@ -336,6 +346,57 @@
hoverxref_tooltip_theme = ["tooltipster-shadow", "tooltipster-shadow-custom"]


# -- sphinxext/inheritance_diagram.py options --------------------------------

graphviz_output_format = "svg"
inheritance_graph_attrs = dict(
rankdir="TB",
size='"10.0, 10.0"',
fontsize=10,
ratio="auto",
center="true",
nodesep=5,
ranksep=0.35,
bgcolor="white",
)
inheritance_node_attrs = dict(
shape="box",
fontsize=14,
fontname="monospace",
height=0.20,
color="black",
style="filled",
)
inheritance_edge_attrs = dict(
arrowsize=0.75,
style='"setlinewidth(0.5)"',
)

# -- Options for sphinx-hoverxref --------------------------------------------

# Hoverxref settings

hoverxref_default_type = "tooltip"
hoverxref_auto_ref = True

hoverxref_roles = ["class", "meth", "func", "ref", "term"]
hoverxref_role_types = dict.fromkeys(hoverxref_roles, "tooltip")

hoverxref_domains = ["py"]

# Currently, only projects that are hosted on readthedocs + CPython, NumPy, and
# SymPy are supported
hoverxref_intersphinx = list(intersphinx_mapping.keys())

# Tooltips settings
hoverxref_tooltip_lazy = False
hoverxref_tooltip_maxwidth = 750
hoverxref_tooltip_animation = "fade"
hoverxref_tooltip_animation_duration = 1
hoverxref_tooltip_content = "Loading information..."
hoverxref_tooltip_theme = ["tooltipster-shadow", "tooltipster-shadow-custom"]


# -- Jinja templating --------------------------------------------------------
# Credit to: https://ericholscher.com/blog/2016/jul/25/integrating-jinja-rst-sphinx/

Expand Down
2 changes: 1 addition & 1 deletion docs/source/_static/pybamm.css
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ html[data-theme="dark"] .sd-shadow-sm {

html[data-theme="dark"] .sd-card .sd-card-header {
background-color: var(--pst-color-background);
color: #150458 !important;
color: #459db9 !important;
}

html[data-theme="dark"] .sd-card .sd-card-footer {
Expand Down
10 changes: 8 additions & 2 deletions docs/source/user_guide/installation/install-from-source.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,28 @@ To install PyBaMM, you will need:
- A BLAS library (for instance `openblas <https://www.openblas.net/>`_).
- A C compiler (ex: ``gcc``).
- A Fortran compiler (ex: ``gfortran``).
- ``graphviz`` (optional), if you wish to build the documentation locally.

You can install the above with

.. tab:: Ubuntu

.. code:: bash

sudo apt install python3.X python3.X-dev libopenblas-dev gcc gfortran
sudo apt install python3.X python3.X-dev libopenblas-dev gcc gfortran graphviz

Where ``X`` is the version sub-number.

.. tab:: MacOS

.. code:: bash

brew install python openblas gcc gfortran libomp
brew install python openblas gcc gfortran graphviz libomp

.. note::

On Windows, you can install ``graphviz`` using the `Chocolatey <https://chocolatey.org/>`_ package manager, or
follow the instructions on the `graphviz website <https://graphviz.org/download/>`_.

Finally, we recommend using `Nox <https://nox.thea.codes/en/stable/>`_.
You can install it with
Expand Down
File renamed without changes.
37 changes: 37 additions & 0 deletions docs/sphinxext/inheritance_diagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Sphinx extension to add an inheritance diagram in the docstring of a class built upon
# the built-in sphinxext.inheritance_diagram extension. The inheritance diagram is
# generated via graphviz and the fully qualified name of the class.

from inspect import getmro, isclass


def add_diagram(app, what, name, obj, options, lines):
# if it is not a class, do nothing
if not isclass(obj):
return
# if it is a model or submodel class, add the inheritance diagram
else:
# get the fully qualified name of the class
cls_name = f"{obj.__module__}.{obj.__qualname__}"
# check if the class is a model or submodel
if "pybamm.models" in cls_name:
# check if the class derives from another class
if not len(getmro(obj)) > 2:
# do nothing if it is not a derived class
return

# Append the inheritance diagram to the docstring
lines.append("\n")
lines.append(".. dropdown:: View inheritance diagram for this model")
lines.append(" :animate: fade-in-slide-down")
lines.append(" :icon: eye\n")
lines.append(" :class-title: sd-align-major-center sd-fs-6 \n")
lines.append(" :class-container: sd-text-info \n")
lines.append("\n")
lines.append(" .. inheritance-diagram:: " + cls_name)
lines.append(" :parts: 2\n")
lines.append("\n")


def setup(app):
app.connect("autodoc-process-docstring", add_diagram)
Loading