File: extending.rst

package info (click to toggle)
pydata-sphinx-theme 0.16.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,088 kB
  • sloc: python: 2,796; javascript: 701; makefile: 42; sh: 12
file content (250 lines) | stat: -rw-r--r-- 7,681 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
===================
Extending the theme
===================

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.

Collapsible admonitions
=======================

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``:

.. code:: python

    extensions = [
        # [...]
        "sphinx_togglebutton"
    ]

Then add the ``dropdown`` class to any admonition directive (shown here on a ``note`` admonition):

.. begin-example-dropdown
.. note::
    :class: dropdown

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
.. end-example-dropdown

.. tab-set::

    .. tab-item:: rst

        .. include:: ./extending.rst
            :start-after: begin-example-dropdown
            :end-before: .. end-example-dropdown
            :code: rst
            :class: highlight-rst

    .. tab-item:: markdown

        .. code-block:: md

            ```{note}
            :class: dropdown

            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            ```


Custom admonition styles
========================

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.


Customizing the title
---------------------

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:

.. begin-example-title
.. admonition:: Custom title!

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
.. end-example-title

The title is specified on the same line as the ``.. admonition::`` directive:

.. tab-set::

    .. tab-item:: rst

        .. include:: ./extending.rst
            :start-after: begin-example-title
            :end-before: .. end-example-title
            :code: rst
            :class: highlight-rst

    .. tab-item:: markdown

        .. code-block:: md

            ```{admonition} Custom title!

            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            ```

Styling with semantic color names
---------------------------------

You can re-style any admonition to match any of the built-in admonition types
using any of the :ref:`theme's semantic color names <color-variables>` as a
class (this is most useful for custom-titled admonitions):

.. begin-example-semantic
.. admonition:: Custom title with "warning" style
    :class: warning

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
.. end-example-semantic

Note that it updates both the color and the icon. See :doc:`./styling` for a list of all semantic color names.

.. tab-set::

    .. tab-item:: rst

        .. include:: ./extending.rst
            :start-after: begin-example-semantic
            :end-before: .. end-example-semantic
            :code: rst
            :class: highlight-rst

    .. tab-item:: markdown

        .. code-block:: md

            ```{admonition} Custom title with "warning" style
            :class: warning

            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            ```

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).

Customizing the color
---------------------

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:

.. begin-example-color
.. admonition:: Admonition with custom "olive" color
    :class: admonition-olive

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
.. end-example-color

To do this, you will need to add a 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`` and ``color`` and a different shade for ``background-color``:

.. tab-set::

    .. tab-item:: rst

        .. include:: ./extending.rst
            :start-after: begin-example-color
            :end-before: .. end-example-color
            :code: rst
            :class: highlight-rst

    .. tab-item:: markdown

        .. code-block:: md

            ```{admonition} Admonition with custom "olive" color
            :class: admonition-olive

            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            ```

And add the following to your ``custom.css`` file:

.. include:: ../_static/custom.css
    :start-after: begin-custom-color
    :end-before: /* end-custom-color
    :code: css
    :class: highlight-css


Using a custom icon
-------------------

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:

.. begin-example-icon
.. admonition:: Check out my custom icon
    :class: admonition-icon

    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
.. end-example-icon

.. tab-set::

    .. tab-item:: rst

        .. include:: ./extending.rst
            :start-after: begin-example-icon
            :end-before: .. end-example-icon
            :code: rst
            :class: highlight-rst

    .. tab-item:: markdown

        .. code-block:: md

            ```{admonition} Check out my custom icon
            :class: admonition-icon

            Lorem ipsum dolor sit amet, consectetur adipiscing elit.
            ```

And add the following css to your ``custom.css`` file:

.. include:: ../_static/custom.css
    :start-after: begin-custom-icon
    :end-before: /* end-custom-icon
    :code: css
    :class: highlight-css


Combining all three customizations
----------------------------------

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:

.. begin-example-youtube
.. admonition:: YouTube
    :class: dropdown admonition-youtube

    ..  youtube:: dQw4w9WgXcQ
.. end-example-youtube

.. tab-set::

    .. tab-item:: rst

        .. include:: ./extending.rst
            :start-after: begin-example-youtube
            :end-before: .. end-example-youtube
            :code: rst
            :class: highlight-rst

    .. tab-item:: markdown

        .. code-block:: md

            ````{admonition} YouTube
            :class: dropdown admonition-youtube

            ```{youtube} dQw4w9WgXcQ
            ```

            ````

And add the following css to your custom.css file:

.. include:: ../_static/custom.css
    :start-after: begin-custom-youtube
    :end-before: /* end-custom-youtube
    :code: css
    :class: highlight-css