Debian Codegraph @

docs/usage/customization.md

raw
1 # Customization
2
3 It is possible to customize the output of the generated documentation with CSS
4 and/or by overriding templates.
5
6 ## CSS classes
7
8 Our templates add [CSS](https://www.w3schools.com/Css/) classes to many HTML elements
9 to make it possible for users to customize the resulting look and feel.
10
11 To add CSS rules and style mkdocstrings' output,
12 put them in a CSS file in your docs folder, for example in `docs/css/mkdocstrings.css`,
13 and reference this file in [MkDocs' `extra_css` configuration option](https://www.mkdocs.org/user-guide/configuration/#extra_css):
14
15 ```yaml title="mkdocs.yml"
16 extra_css:
17 - css/mkdocstrings.css
18 ```
19
20 Example:
21
22 ```css title="docs/css/mkdocstrings.css"
23 .doc-section-title {
24 font-weight: bold;
25 }
26 ```
27
28 The following CSS classes are used in the generated HTML:
29
30 - `doc`: on all the following elements
31 - `doc-children`: on `div`s containing the children of an object
32 - `doc-object`: on `div`s containing an object
33 - `doc-attribute`: on `div`s containing an attribute
34 - `doc-class`: on `div`s containing a class
35 - `doc-function`: on `div`s containing a function
36 - `doc-module`: on `div`s containing a module
37 - `doc-type_alias`: on `div`s containing a type alias
38 - `doc-heading`: on objects headings
39 - `doc-object-name`: on `span`s wrapping objects names/paths in the heading
40 - `doc-KIND-name`: as above, specific to the kind of object (module, class, function, attribute, type_alias)
41 - `doc-contents`: on `div`s wrapping the docstring then the children (if any)
42 - `first`: same, but only on the root object's contents `div`
43 - `doc-labels`: on `span`s wrapping the object's labels
44 - `doc-label`: on `small` elements containing a label
45 - `doc-label-LABEL`: same, where `LABEL` is replaced by the actual label
46 - `doc-section-title`: on section titles (depend on the [selected style for section rendering][docstring_style])
47 - `doc-section-item`: on section items (depend on the [selected style for section rendering][docstring_style])
48 - `doc-md-description`: on `div`s containing HTML descriptions converted from Markdown docstrings
49 - `doc-symbol`: on `code` tags of symbol types
50 - `doc-symbol-heading`: on symbol types in headings
51 - `doc-symbol-toc`: on symbol types in the ToC
52 - `doc-symbol-KIND`: specific to the kind of object (`module`, `class`, `function`, `method`, `attribute`, `type_alias`)
53
54 /// admonition | Example with colorful labels
55 type: example
56
57 //// tab | CSS
58 ```css
59 .doc-label { border-radius: 15px; padding: 2px 8px; font-weight: bold; }
60 .doc-label-special { background-color: #3330E4; color: white; }
61 .doc-label-private { background-color: #F637EC; color: white; }
62 .doc-label-property { background-color: #FBB454; color: black; }
63 .doc-label-read-only { background-color: #FAEA48; color: black; }
64 ```
65 ////
66
67 //// tab | Result
68 <style>
69 .lbl { border-radius: 15px; padding: 2px 8px; font-weight: bold; }
70 </style>
71 <h3 style="margin: 0;">
72 <p>
73 <small class="lbl" style="background-color: #3330E4; color: white !important;">special</small>
74 <small class="lbl" style="background-color: #F637EC; color: white !important;">private</small>
75 <small class="lbl" style="background-color: #FBB454; color: black !important;">property</small>
76 <small class="lbl" style="background-color: #FAEA48; color: black !important;">read-only</small>
77 </p>
78 </h3>
79 ////
80
81 ///
82
83 ## Symbol types
84
85 ### Colors
86
87 You can customize the colors of the symbol types
88 (see [`show_symbol_type_heading`][show_symbol_type_heading] and [`show_symbol_type_toc`][show_symbol_type_toc])
89 by overriding the values of our CSS variables, for example:
90
91 ```css title="docs/css/mkdocstrings.css"
92 [data-md-color-scheme="default"] {
93 --doc-symbol-parameter-fg-color: #df50af;
94 --doc-symbol-type_parameter-fg-color: #df50af;
95 --doc-symbol-attribute-fg-color: #0079ff;
96 --doc-symbol-function-fg-color: #00dfa2;
97 --doc-symbol-method-fg-color: #00dfa2;
98 --doc-symbol-class-fg-color: #d1b619;
99 --doc-symbol-type_alias-fg-color: #d1b619;
100 --doc-symbol-module-fg-color: #ff0060;
101
102 --doc-symbol-parameter-bg-color: #df50af1a;
103 --doc-symbol-type_parameter-bg-color: #df50af1a;
104 --doc-symbol-attribute-bg-color: #0079ff1a;
105 --doc-symbol-function-bg-color: #00dfa21a;
106 --doc-symbol-method-bg-color: #00dfa21a;
107 --doc-symbol-class-bg-color: #d1b6191a;
108 --doc-symbol-type_alias-bg-color: #d1b6191a;
109 --doc-symbol-module-bg-color: #ff00601a;
110 }
111
112 [data-md-color-scheme="slate"] {
113 --doc-symbol-parameter-fg-color: #ffa8cc;
114 --doc-symbol-type_parameter-fg-color: #ffa8cc;
115 --doc-symbol-attribute-fg-color: #963fb8;
116 --doc-symbol-function-fg-color: #6d67e4;
117 --doc-symbol-method-fg-color: #6d67e4;
118 --doc-symbol-class-fg-color: #46c2cb;
119 --doc-symbol-type_alias-fg-color: #46c2cb;
120 --doc-symbol-module-fg-color: #f2f7a1;
121
122 --doc-symbol-parameter-bg-color: #ffa8cc1a;
123 --doc-symbol-type_parameter-bg-color: #ffa8cc1a;
124 --doc-symbol-attribute-bg-color: #963fb81a;
125 --doc-symbol-function-bg-color: #6d67e41a;
126 --doc-symbol-method-bg-color: #6d67e41a;
127 --doc-symbol-class-bg-color: #46c2cb1a;
128 --doc-symbol-type_alias-bg-color: #46c2cb1a;
129 --doc-symbol-module-bg-color: #f2f7a11a;
130 }
131 ```
132
133 The `[data-md-color-scheme="*"]` selectors work with the [Material for MkDocs] theme.
134 If you are using another theme, adapt the selectors to this theme
135 if it supports light and dark themes,
136 otherwise just override the variables at root level:
137
138 ```css title="docs/css/mkdocstrings.css"
139 :root {
140 --doc-symbol-parameter-fg-color: #df50af;
141 --doc-symbol-type_parameter-fg-color: #df50af;
142 --doc-symbol-attribute-fg-color: #0079ff;
143 --doc-symbol-function-fg-color: #00dfa2;
144 --doc-symbol-method-fg-color: #00dfa2;
145 --doc-symbol-class-fg-color: #d1b619;
146 --doc-symbol-type_alias-fg-color: #d1b619;
147 --doc-symbol-module-fg-color: #ff0060;
148
149 --doc-symbol-parameter-bg-color: #df50af1a;
150 --doc-symbol-type_parameter-bg-color: #df50af1a;
151 --doc-symbol-attribute-bg-color: #0079ff1a;
152 --doc-symbol-function-bg-color: #00dfa21a;
153 --doc-symbol-method-bg-color: #00dfa21a;
154 --doc-symbol-class-bg-color: #d1b6191a;
155 --doc-symbol-type_alias-bg-color: #d1b6191a;
156 --doc-symbol-module-bg-color: #ff00601a;
157 }
158 ```
159
160 /// admonition | Preview
161 type: preview
162
163 <div id="preview-symbol-colors">
164 <style>
165 [data-md-color-scheme="default"] #preview-symbol-colors {
166 --doc-symbol-parameter-fg-color: #df50af;
167 --doc-symbol-type_parameter-fg-color: #df50af;
168 --doc-symbol-attribute-fg-color: #0079ff;
169 --doc-symbol-function-fg-color: #00dfa2;
170 --doc-symbol-method-fg-color: #00dfa2;
171 --doc-symbol-class-fg-color: #d1b619;
172 --doc-symbol-type_alias-fg-color: #d1b619;
173 --doc-symbol-module-fg-color: #ff0060;
174
175 --doc-symbol-parameter-bg-color: #df50af1a;
176 --doc-symbol-type_parameter-bg-color: #df50af1a;
177 --doc-symbol-attribute-bg-color: #0079ff1a;
178 --doc-symbol-function-bg-color: #00dfa21a;
179 --doc-symbol-method-bg-color: #00dfa21a;
180 --doc-symbol-class-bg-color: #d1b6191a;
181 --doc-symbol-type_alias-bg-color: #d1b6191a;
182 --doc-symbol-module-bg-color: #ff00601a;
183 }
184
185 [data-md-color-scheme="slate"] #preview-symbol-colors {
186 --doc-symbol-parameter-fg-color: #ffa8cc;
187 --doc-symbol-type_parameter-fg-color: #ffa8cc;
188 --doc-symbol-attribute-fg-color: #963fb8;
189 --doc-symbol-function-fg-color: #6d67e4;
190 --doc-symbol-method-fg-color: #6d67e4;
191 --doc-symbol-class-fg-color: #46c2cb;
192 --doc-symbol-type_alias-fg-color: #46c2cb;
193 --doc-symbol-module-fg-color: #f2f7a1;
194
195 --doc-symbol-parameter-bg-color: #ffa8cc1a;
196 --doc-symbol-type_parameter-bg-color: #ffa8cc1a;
197 --doc-symbol-attribute-bg-color: #963fb81a;
198 --doc-symbol-function-bg-color: #6d67e41a;
199 --doc-symbol-method-bg-color: #6d67e41a;
200 --doc-symbol-class-bg-color: #46c2cb1a;
201 --doc-symbol-type_alias-bg-color: #46c2cb1a;
202 --doc-symbol-module-bg-color: #f2f7a11a;
203 }
204 </style>
205 <p>
206 Try cycling through the themes to see the colors for each theme:
207 <code class="doc-symbol doc-symbol-parameter"></code>
208 <code class="doc-symbol doc-symbol-attribute"></code>
209 <code class="doc-symbol doc-symbol-function"></code>
210 <code class="doc-symbol doc-symbol-method"></code>
211 <code class="doc-symbol doc-symbol-class"></code>
212 <code class="doc-symbol doc-symbol-module"></code>
213 </p>
214 </div>
215
216 ///
217
218 ### Names
219
220 You can also change the actual symbol names.
221 For example, to use single letters instead of truncated types:
222
223 ```css title="docs/css/mkdocstrings.css"
224 .doc-symbol-parameter::after {
225 content: "P";
226 }
227
228 .doc-symbol-type_parameter::after {
229 content: "P";
230 }
231
232 .doc-symbol-attribute::after {
233 content: "A";
234 }
235
236 .doc-symbol-function::after {
237 content: "F";
238 }
239
240 .doc-symbol-method::after {
241 content: "M";
242 }
243
244 .doc-symbol-class::after {
245 content: "C";
246 }
247
248 .doc-symbol-type_alias::after {
249 content: "T";
250 }
251
252 .doc-symbol-module::after {
253 content: "M";
254 }
255 ```
256
257 /// admonition | Preview
258 type: preview
259
260 <div id="preview-symbol-names">
261 <style>
262 #preview-symbol-names .doc-symbol-parameter::after {
263 content: "P";
264 }
265
266 #preview-symbol-names .doc-symbol-type_parameter::after {
267 content: "P";
268 }
269
270 #preview-symbol-names .doc-symbol-attribute::after {
271 content: "A";
272 }
273
274 #preview-symbol-names .doc-symbol-function::after {
275 content: "F";
276 }
277
278 #preview-symbol-names .doc-symbol-method::after {
279 content: "M";
280 }
281
282 #preview-symbol-names .doc-symbol-class::after {
283 content: "C";
284 }
285
286 #preview-symbol-names .doc-symbol-type_alias::after {
287 content: "T";
288 }
289
290 #preview-symbol-names .doc-symbol-module::after {
291 content: "M";
292 }
293 </style>
294 <ul>
295 <li>Parameter: <code class="doc-symbol doc-symbol-parameter"></code></li>
296 <li>Type Parameter: <code class="doc-symbol doc-symbol-type_parameter"></code></li>
297 <li>Attribute: <code class="doc-symbol doc-symbol-attribute"></code></li>
298 <li>Function: <code class="doc-symbol doc-symbol-function"></code></li>
299 <li>Method: <code class="doc-symbol doc-symbol-method"></code></li>
300 <li>Class: <code class="doc-symbol doc-symbol-class"></code></li>
301 <li>Type Alias: <code class="doc-symbol doc-symbol-type_alias"></code></li>
302 <li>Module: <code class="doc-symbol doc-symbol-module"></code></li>
303 </ul>
304 </div>
305
306 ///
307
308 ## Templates
309
310 Templates are organized into the following tree:
311
312 ```python exec="1" result="tree"
313 from pathlib import Path
314
315 basedir = "src/mkdocstrings_handlers/python/templates/material"
316 print("theme/")
317 for filepath in sorted(path for path in Path(basedir).rglob("*") if "_base" not in str(path) and path.suffix != ".css"):
318 print(
319 " " * (len(filepath.relative_to(basedir).parent.parts) + 1)
320 + filepath.name
321 + ("/" if filepath.is_dir() else "")
322 )
323 ```
324
325 See them [in the repository](https://github.com/mkdocstrings/python/tree/main/src/mkdocstrings_handlers/python/templates/).
326 See the general *mkdocstrings* documentation to learn how to override them: https://mkdocstrings.github.io/theming/#templates.
327
328 Each one of these templates extends a base version in `theme/_base`. Example:
329
330 ```html+jinja title="theme/class.html"
331 {% extends "_base/class.html" %}
332 ```
333
334 Some of these templates define [Jinja blocks](https://jinja.palletsprojects.com/en/3.0.x/templates/#template-inheritance).
335 allowing to customize only *parts* of a template
336 without having to fully copy-paste it into your project:
337
338 ```jinja title="templates/theme/class.html"
339 {% extends "_base/class.html" %}
340 {% block contents %}
341 {{ block.super }}
342 Additional contents
343 {% endblock contents %}
344 ```
345
346 ### Available blocks
347
348 Only the templates for the **Material for MkDocs** provide Jinja blocks.
349 The following tables show the block names, description,
350 and the Jinja context available in their scope.
351
352 #### `module.html`
353
354 - `heading`: The module heading.
355 - `labels`: The module labels.
356 - `contents`: The module contents: docstring and children blocks.
357 - `docstring`: The module docstring.
358 - `summary`: The automatic summaries of members.
359 - `children`: The module children.
360
361 Available context:
362
363 - `config`: The handler configuration (dictionary).
364 - `module`: The [Module][griffe.Module] instance.
365
366 #### `type_alias.html`
367
368 - `heading`: The class heading.
369 - `labels`: The class labels.
370 - `signature`: The class signature.
371 - `contents`: The class contents: bases, docstring, source and children blocks.
372 - `docstring`: The class docstring.
373
374 Available context:
375
376 - `config`: The handler configuration (dictionary).
377 - `type_alias`: The [TypeAlias][griffe.TypeAlias] instance.
378
379 #### `class.html`
380
381 - `heading`: The class heading.
382 - `labels`: The class labels.
383 - `signature`: The class signature.
384 - `contents`: The class contents: bases, docstring, source and children blocks.
385 - `bases`: The class bases.
386 - `docstring`: The class docstring.
387 - `summary`: The automatic summaries of members.
388 - `source`: The class source code.
389 - `children`: The class children.
390
391 Available context:
392
393 - `config`: The handler configuration (dictionary).
394 - `class`: The [Class][griffe.Class] instance.
395
396 #### `function.html`
397
398 - `heading`: The function heading.
399 - `labels`: The function labels.
400 - `signature`: The function signature.
401 - `contents`: The function contents: docstring and source blocks.
402 - `docstring`: The function docstring.
403 - `source`: The function source code.
404
405 Available context:
406
407 - `config`: The handler configuration (dictionary).
408 - `function`: The [Function][griffe.Function] instance.
409
410 #### `attribute.html`
411
412 - `heading`: The attribute heading.
413 - `labels`: The attribute labels.
414 - `signature`: The attribute signature.
415 - `contents`: The attribute contents: docstring block.
416 - `docstring`: The attribute docstring.
417
418 Available context:
419
420 - `config`: The handler configuration (dictionary).
421 - `attribute`: The [Attribute][griffe.Attribute] instance.
422
423 #### Docstring sections
424
425 In `docstring/attributes.html`,
426 `docstring/functions.html`,
427 `docstring/classes.html`,
428 `docstring/modules.html`,
429 `docstring/other_parameters.html`,
430 `docstring/parameters.html`,
431 `docstring/raises.html`,
432 `docstring/receives.html`,
433 `docstring/returns.html`,
434 `docstring/type_aliases.html`,
435 `docstring/type_parameters.html`,
436 `docstring/warns.html`,
437 and `docstring/yields.html`:
438
439 - `table_style`: The section as a table.
440 - `list_style`: The section as a list.
441 - `spacy_style`: The section as a Spacy table.
442
443 Available context:
444
445 - `section`: The [DocstringSection][griffe.DocstringSection] instance (see `DocstringSection*` subclasses).
446
447 ### Syntax highlight in signatures
448
449 You can customize the colors in syntax highlighted signatures.
450 If you are using the [Material for MkDocs] theme,
451 here are some customization examples:
452
453 ```css
454 /* Fancier color for operators such as * and |. */
455 .doc-signature .o {
456 color: var(--md-code-hl-special-color);
457 }
458
459 /* Fancier color for constants such as None, True, and False. */
460 .doc-signature .kc {
461 color: var(--md-code-hl-constant-color);
462 }
463
464 /* Fancier color for built-in types (only useful when cross-references are used). */
465 .doc-signature .n > a[href^="https://docs.python.org/"][href*="/functions.html#"],
466 .doc-signature .n > a[href^="https://docs.python.org/"][href*="/stdtypes.html#"] {
467 color: var(--md-code-hl-constant-color);
468 }
469 ```
470
471 For other themes, use their own CSS variables,
472 or use plain colors such as `violet` or `#2987f2`.
473
474 ## Style recommendations
475
476 [](){#recommended-style-material}
477 ### Material
478
479 Here are some CSS rules for the [Material for MkDocs] theme:
480
481 ```css
482 --8<-- "docs/css/mkdocstrings.css"
483 ```
484
485 [](){#recommended-style-readthedocs}
486 ### ReadTheDocs
487
488 Here are some CSS rules for the built-in ReadTheDocs theme:
489
490 ```css
491 /* Indentation. */
492 div.doc-contents:not(.first) {
493 padding-left: 25px;
494 border-left: .05rem solid rgba(200, 200, 200, 0.2);
495 }
496 ```