Debian Codegraph @

docs/user_guide/extending.rst

raw
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
37
38 .. tab-item:: markdown
39
40 .. code-block:: md
41
42 ```{note}
43 :class: dropdown
44
45 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
46 ```
47
48
49 Custom admonition styles
50 ========================
51
52 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.
53
54
55 Customizing the title
56 ---------------------
57
58 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:
59
60 .. begin-example-title
61 .. admonition:: Custom title!
62
63 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
64 .. end-example-title
65
66 The title is specified on the same line as the ``.. admonition::`` directive:
67
68 .. tab-set::
69
70 .. tab-item:: rst
71
72 .. include:: ./extending.rst
73 :start-after: begin-example-title
74 :end-before: .. end-example-title
75 :code: rst
76 :class: highlight
77
78 .. tab-item:: markdown
79
80 .. code-block:: md
81
82 ```{admonition} Custom title!
83
84 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
85 ```
86
87 Styling with semantic color names
88 ---------------------------------
89
90 You can re-style any admonition to match any of the built-in admonition types
91 using any of the :ref:`theme's semantic color names <color-variables>` as a
92 class (this is most useful for custom-titled admonitions):
93
94 .. begin-example-semantic
95 .. admonition:: Custom title with "warning" style
96 :class: warning
97
98 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
99 .. end-example-semantic
100
101 Note that it updates both the color and the icon. See :doc:`./styling` for a list of all semantic color names.
102
103 .. tab-set::
104
105 .. tab-item:: rst
106
107 .. include:: ./extending.rst
108 :start-after: begin-example-semantic
109 :end-before: .. end-example-semantic
110 :code: rst
111 :class: highlight
112
113 .. tab-item:: markdown
114
115 .. code-block:: md
116
117 ```{admonition} Custom title with "warning" style
118 :class: warning
119
120 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
121 ```
122
123 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).
124
125 Customizing the color
126 ---------------------
127
128 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:
129
130 .. begin-example-color
131 .. admonition:: Admonition with custom "olive" color
132 :class: admonition-olive
133
134 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
135 .. end-example-color
136
137 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.
138 Be sure to use the same color for ``border-color`` and ``color`` and a different shade for ``background-color``:
139
140 .. tab-set::
141
142 .. tab-item:: rst
143
144 .. include:: ./extending.rst
145 :start-after: begin-example-color
146 :end-before: .. end-example-color
147 :code: rst
148 :class: highlight
149
150 .. tab-item:: markdown
151
152 .. code-block:: md
153
154 ```{admonition} Admonition with custom "olive" color
155 :class: admonition-olive
156
157 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
158 ```
159
160 And add the following to your ``custom.css`` file:
161
162 .. include:: ../_static/custom.css
163 :start-after: begin-custom-color
164 :end-before: /* end-custom-color
165 :code: css
166 :class: highlight
167
168
169 Using a custom icon
170 -------------------
171
172 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:
173
174 .. begin-example-icon
175 .. admonition:: Check out my custom icon
176 :class: admonition-icon
177
178 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
179 .. end-example-icon
180
181 .. tab-set::
182
183 .. tab-item:: rst
184
185 .. include:: ./extending.rst
186 :start-after: begin-example-icon
187 :end-before: .. end-example-icon
188 :code: rst
189 :class: highlight
190
191 .. tab-item:: markdown
192
193 .. code-block:: md
194
195 ```{admonition} Check out my custom icon
196 :class: admonition-icon
197
198 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
199 ```
200
201 And add the following css to your ``custom.css`` file:
202
203 .. include:: ../_static/custom.css
204 :start-after: begin-custom-icon
205 :end-before: /* end-custom-icon
206 :code: css
207 :class: highlight
208
209
210 Combining all three customizations
211 ----------------------------------
212
213 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:
214
215 .. begin-example-youtube
216 .. admonition:: YouTube
217 :class: dropdown admonition-youtube
218
219 .. youtube:: dQw4w9WgXcQ
220 .. end-example-youtube
221
222 .. tab-set::
223
224 .. tab-item:: rst
225
226 .. include:: ./extending.rst
227 :start-after: begin-example-youtube
228 :end-before: .. end-example-youtube
229 :code: rst
230 :class: highlight
231
232 .. tab-item:: markdown
233
234 .. code-block:: md
235
236 ````{admonition} YouTube
237 :class: dropdown admonition-youtube
238
239 ```{youtube} dQw4w9WgXcQ
240 ```
241
242 ````
243
244 And add the following css to your custom.css file:
245
246 .. include:: ../_static/custom.css
247 :start-after: begin-custom-youtube
248 :end-before: /* end-custom-youtube
249 :code: css
250 :class: highlight