Debian Codegraph @

docs/community/topics/config.md

raw
1 # Update Sphinx configuration during the build
2
3 Sometimes you want to update configuration values _during a build_.
4 For example, if you want to set a default if the user hasn't provided a value, or if you want to move the value from one keyword to another for a deprecation.
5
6 Here are some tips to do this the "right" way in Sphinx.
7
8 ## Update config: use `app.config`
9
10 For example, `app.config.foo = "bar"`.
11 For some reason, when Sphinx sets things it directly uses `__dict__`, but this doesn't seem to be different from the pattern described here.
12
13 ## Update theme options: use `app.builder.theme_options`
14
15 For example, `app.builder.theme_options["logo"] = {"text": "Foo"}`.
16
17 ## Check if a user has provided a default: `app.config._raw_config`
18
19 The `app.config._raw_config` attribute contains all of the **user-provided values**.
20 Use this if you want to check whether somebody has manually specified something.
21 For example, `"somekey" in app.config._raw_config` will be `False` if a user has _not_ provided that option.
22
23 You can also check `app.config.overrides` for any CLI-provided overrides.
24
25 We bundle both checks in a helper function called `_config_provided_by_user`.
26
27 ## Avoid the `config-inited` event
28
29 This theme is activated **after** `config-inited` is triggered, so if you write an event that depends on it in this theme, then it will never occur.
30 The earliest event you can use is `builder-inited`.