|
| 1 | +=================== |
| 2 | +Extending the theme |
| 3 | +=================== |
| 4 | + |
| 5 | +There are many extensions available for Sphinx that can enhance your site or provide powerful customization abilities. Here we describe a few customizations that are popular with ``pydata-sphinx-theme`` users. |
| 6 | + |
| 7 | +Collapsible admonitions |
| 8 | +======================= |
| 9 | + |
| 10 | +The `sphinx-togglebutton <https://sphinx-togglebutton.readthedocs.io/en/latest/>`__ extension provides optional show/hide behavior for admonitions. Follow their installation instructions, then add it to the ``extentions`` list in your ``conf.py``: |
| 11 | + |
| 12 | +.. code:: python |
| 13 | +
|
| 14 | + extensions = [ |
| 15 | + # [...] |
| 16 | + "sphinx_togglebutton" |
| 17 | + ] |
| 18 | +
|
| 19 | +Then add the ``dropdown`` class to any admonition directive (shown here on a ``note`` admonition): |
| 20 | + |
| 21 | +.. begin-example-dropdown |
| 22 | +.. note:: |
| 23 | + :class: dropdown |
| 24 | + |
| 25 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 26 | +.. end-example-dropdown |
| 27 | +
|
| 28 | +.. tab-set:: |
| 29 | + |
| 30 | + .. tab-item:: rst |
| 31 | + |
| 32 | + .. include:: ./extending.rst |
| 33 | + :start-after: begin-example-dropdown |
| 34 | + :end-before: .. end-example-dropdown |
| 35 | + :code: rst |
| 36 | + :class: highlight-rst |
| 37 | + |
| 38 | + |
| 39 | +Custom admonition styles |
| 40 | +======================== |
| 41 | + |
| 42 | +A `limited set <https://docutils.sourceforge.io/docs/ref/rst/directives.html#admonitions>`__ of admonitions are built-in to docutils (the rST → HTML engine that underlies Sphinx). However, it is possible to create custom admonitions with their own default colors, icons, and titles. |
| 43 | + |
| 44 | + |
| 45 | +Customizing the title |
| 46 | +--------------------- |
| 47 | + |
| 48 | +Although most admonitions have a default title like ``note`` or ``warning``, a generic ``admonition`` directive is built-in to docutils/Sphinx. In this theme, its color defaults to the same color as ``note`` admonitions, and it has a bell icon: |
| 49 | + |
| 50 | +.. begin-example-title |
| 51 | +.. admonition:: Custom title! |
| 52 | + |
| 53 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 54 | +.. end-example-title |
| 55 | +
|
| 56 | +The title is specified on the same line as the ``.. admonition::`` directive: |
| 57 | + |
| 58 | +.. tab-set:: |
| 59 | + |
| 60 | + .. tab-item:: rst |
| 61 | + |
| 62 | + .. include:: ./extending.rst |
| 63 | + :start-after: begin-example-title |
| 64 | + :end-before: .. end-example-title |
| 65 | + :code: rst |
| 66 | + :class: highlight-rst |
| 67 | + |
| 68 | + |
| 69 | +Styling with semantic color names |
| 70 | +--------------------------------- |
| 71 | + |
| 72 | +You can re-style any admonition to match any of the built-in admonition types using any of the semantic color names as a class (this is most useful for custom-titled admonitions): |
| 73 | + |
| 74 | +.. begin-example-semantic |
| 75 | +.. admonition:: Custom title with "warning" style |
| 76 | + :class: warning |
| 77 | + |
| 78 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 79 | +.. end-example-semantic |
| 80 | +
|
| 81 | +Note that it updates both the color and the icon. |
| 82 | + |
| 83 | +.. tab-set:: |
| 84 | + |
| 85 | + .. tab-item:: rst |
| 86 | + |
| 87 | + .. include:: ./extending.rst |
| 88 | + :start-after: begin-example-semantic |
| 89 | + :end-before: .. end-example-semantic |
| 90 | + :code: rst |
| 91 | + :class: highlight-rst |
| 92 | + |
| 93 | +This theme defines classes for `the standard docutils admonition types <https://docutils.sourceforge.io/docs/ref/rst/directives.html#admonitions>`__ (``attention``, ``caution``, etc) and additionally supports ``seealso`` and ``todo`` admonitions (see :doc:`../examples/kitchen-sink/admonitions` for a demo of all built-in admonition styles). |
| 94 | + |
| 95 | + |
| 96 | +Customizing the color |
| 97 | +--------------------- |
| 98 | + |
| 99 | +Besides the pre-defined semantic color classes (see previous section) you can also add a bespoke color to any admonition by defining your own CSS class. Example: |
| 100 | + |
| 101 | +.. begin-example-color |
| 102 | +.. admonition:: Admonition with custom "olive" color |
| 103 | + :class: admonition-olive |
| 104 | + |
| 105 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 106 | +.. end-example-color |
| 107 | +
|
| 108 | +Add the new class to your `custom.css <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_css_files>`__ file. As in the example below, be sure to use the same color for ``border-color``, ``background-color``, and ``color`` (the transparency effect is handled automatically by the theme). |
| 109 | + |
| 110 | +.. tab-set:: |
| 111 | + |
| 112 | + .. tab-item:: rst |
| 113 | + |
| 114 | + .. include:: ./extending.rst |
| 115 | + :start-after: begin-example-color |
| 116 | + :end-before: .. end-example-color |
| 117 | + :code: rst |
| 118 | + :class: highlight-rst |
| 119 | + |
| 120 | + .. tab-item:: css |
| 121 | + |
| 122 | + .. include:: ../_static/custom.css |
| 123 | + :start-after: begin-custom-color |
| 124 | + :end-before: /* end-custom-color |
| 125 | + :code: css |
| 126 | + :class: highlight-css |
| 127 | + |
| 128 | + |
| 129 | +Using a custom icon |
| 130 | +------------------- |
| 131 | + |
| 132 | +Customizing the icon uses a similar process to customizing the color: create a new CSS class in your `custom.css <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_css_files>`__ file. The theme supports `fontawesome v6 icons <https://fontawesome.com/v6/search?o=r&m=free&f=brands>`__ ("free" and "brands" sets). To use an icon, copy its unicode value into your custom class as shown in the CSS tab below: |
| 133 | + |
| 134 | +.. begin-example-icon |
| 135 | +.. admonition:: Check out my custom icon |
| 136 | + :class: admonition-icon |
| 137 | + |
| 138 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 139 | +.. end-example-icon |
| 140 | +
|
| 141 | +.. tab-set:: |
| 142 | + |
| 143 | + .. tab-item:: rst |
| 144 | + |
| 145 | + .. include:: ./extending.rst |
| 146 | + :start-after: begin-example-icon |
| 147 | + :end-before: .. end-example-icon |
| 148 | + :code: rst |
| 149 | + :class: highlight-rst |
| 150 | + |
| 151 | + .. tab-item:: css |
| 152 | + |
| 153 | + .. include:: ../_static/custom.css |
| 154 | + :start-after: begin-custom-icon |
| 155 | + :end-before: /* end-custom-icon |
| 156 | + :code: css |
| 157 | + :class: highlight-css |
| 158 | + |
| 159 | + |
| 160 | +Combining all three customizations |
| 161 | +---------------------------------- |
| 162 | + |
| 163 | +Here we demonstrate an admonition with a custom icon, color, and title (and also make it collapsible). Note that the multiple admonition class names are space-separated: |
| 164 | + |
| 165 | +.. begin-example-youtube |
| 166 | +.. admonition:: YouTube |
| 167 | + :class: dropdown admonition-youtube |
| 168 | + |
| 169 | + .. youtube:: dQw4w9WgXcQ |
| 170 | +.. end-example-youtube |
| 171 | +
|
| 172 | +.. tab-set:: |
| 173 | + |
| 174 | + .. tab-item:: rst |
| 175 | + |
| 176 | + .. include:: ./extending.rst |
| 177 | + :start-after: begin-example-youtube |
| 178 | + :end-before: .. end-example-youtube |
| 179 | + :code: rst |
| 180 | + :class: highlight-rst |
| 181 | + |
| 182 | + .. tab-item:: css |
| 183 | + |
| 184 | + .. include:: ../_static/custom.css |
| 185 | + :start-after: begin-custom-youtube |
| 186 | + :end-before: /* end-custom-youtube |
| 187 | + :code: css |
| 188 | + :class: highlight-css |
0 commit comments