| 1 |
Announcement banners |
| 2 |
==================== |
| 3 |
|
| 4 |
You can add an announcement banner that draws extra attention from your reader. |
| 5 |
It will be displayed at the top of the screen but will disappear once they start scrolling. |
| 6 |
|
| 7 |
To add an announcement banner, use the ``html_theme_options["announcement"]`` configuration. |
| 8 |
There are two ways to use this. |
| 9 |
|
| 10 |
Provide local HTML in your theme |
| 11 |
-------------------------------- |
| 12 |
|
| 13 |
By default, the value of your ``html_theme_options["announcement"]`` will be inserted directly into your announcement banner as raw HTML. |
| 14 |
|
| 15 |
For example, the following configuration adds a simple announcement. |
| 16 |
|
| 17 |
.. code-block:: python |
| 18 |
:caption: conf.py |
| 19 |
|
| 20 |
html_theme_options = { |
| 21 |
... |
| 22 |
"announcement": "Here's a <a href='https://pydata.org'>PyData Announcement!</a>", |
| 23 |
} |
| 24 |
|
| 25 |
Insert remote HTML with JavaScript |
| 26 |
---------------------------------- |
| 27 |
|
| 28 |
You can specify an arbitrary URL that will be used as the HTML source for your announcement. |
| 29 |
When the page is loaded, JavaScript will attempt to fetch this HTML and insert it as-is into the announcement banner. |
| 30 |
This allows you to define a single HTML announcement that you can pull into multiple documentation sites or versions. |
| 31 |
|
| 32 |
If the value of ``html_theme_options["announcement"]`` begins with ``http`` it will be treated as a URL to remote HTML. |
| 33 |
|
| 34 |
For example, the following configuration tells the theme to load the ``custom-template.html`` example from this documentation's GitHub repository: |
| 35 |
|
| 36 |
.. code-block:: python |
| 37 |
:caption: conf.py |
| 38 |
|
| 39 |
html_theme_options = { |
| 40 |
... |
| 41 |
"announcement": "https://raw.githubusercontent.com/pydata/pydata-sphinx-theme/main/docs/_templates/custom-template.html", |
| 42 |
} |
| 43 |
|
| 44 |
Update or remove announcement banner |
| 45 |
------------------------------------ |
| 46 |
|
| 47 |
If you set ``html_theme_options["announcement"]`` to plain text or HTML, then to |
| 48 |
update the announcement banner you need to modify this string and rebuild your |
| 49 |
documentation pages. To remove the announcement banner, set this value to an |
| 50 |
empty string and rebuild your documentation pages. |
| 51 |
|
| 52 |
If you set ``html_theme_options["announcement"]`` to a URL string (starts with |
| 53 |
``http``), then you can edit the file at that URL to update the announcement |
| 54 |
banner. Saving an empty file at that URL will remove the announcement banner. |
| 55 |
That's the main advantage of using a URL--you can change the announcement banner |
| 56 |
without rebuilding and redeploying all of your documentation pages. For example, |
| 57 |
if you point the announcement to the URL of a file in your repo, as we do on |
| 58 |
this documentation site (see previous section), then you can edit, save and push |
| 59 |
your changes to just that file (empty file = remove announcement) without |
| 60 |
rebuilding and redeploying all your docs. |
| 61 |
|
| 62 |
.. _version-warning-banners: |
| 63 |
|
| 64 |
Version warning banners |
| 65 |
----------------------- |
| 66 |
|
| 67 |
In addition to the general-purpose announcement banner, the theme includes a built-in banner to warn users when they are viewing versions of your docs other than the latest stable version. To use this feature, add the following to your ``conf.py``: |
| 68 |
|
| 69 |
.. code-block:: python |
| 70 |
:caption: conf.py |
| 71 |
|
| 72 |
html_theme_options = { |
| 73 |
... |
| 74 |
"show_version_warning_banner": True, |
| 75 |
} |
| 76 |
|
| 77 |
.. important:: |
| 78 |
|
| 79 |
This functionality relies on the :ref:`version switcher <version-dropdowns>` to determine the version number of the latest stable release. |
| 80 |
*It will only work* if your version switcher ``.json`` has exactly one entry with property ``"preferred": true`` |
| 81 |
and your entries have ``version`` properties that are parsable by the `compare-versions node module <https://www.npmjs.com/package/compare-versions>`__, for example: |
| 82 |
|
| 83 |
.. code-block:: json |
| 84 |
|
| 85 |
{ |
| 86 |
"name": "stable", |
| 87 |
"version": "9.9.9", |
| 88 |
"url": "https://anything", |
| 89 |
"preferred": true |
| 90 |
} |
| 91 |
|
| 92 |
If the active version compares less than the preferred version, the announcement will inform the user that |
| 93 |
they are viewing an older version of the documentation and provide a link to the preferred version. If the |
| 94 |
version compares greater than the preferred version (or if the version match contains the strings `"dev"`, |
| 95 |
`"rc"` or `"pre"`), the announcement will say they are viewing an unstable development version instead. |
| 96 |
|
| 97 |
If you want similar functionality for *older* versions of your docs (i.e. those built before the ``show_version_warning_banner`` configuration option was available), you can manually add a banner by prepending the following HTML to all pages (be sure to replace ``URL_OF_STABLE_VERSION_OF_PROJECT`` with a valid URL, and adjust styling as desired): |
| 98 |
|
| 99 |
.. code-block:: html |
| 100 |
|
| 101 |
<div style="background-color: rgb(248, 215, 218); color: rgb(114, 28, 36); text-align: center;"> |
| 102 |
<div> |
| 103 |
<div>This is documentation for <strong>an old version</strong>. |
| 104 |
<a href="{{ URL_OF_STABLE_VERSION_OF_PROJECT }}" style="background-color: rgb(220, 53, 69); color: rgb(255, 255, 255); margin: 1rem; padding: 0.375rem 0.75rem; border-radius: 4px; display: inline-block; text-align: center;">Switch to stable version</a> |
| 105 |
</div> |
| 106 |
</div> |
| 107 |
</div> |