# Is there a generic API for setting plot annotations?

**URL:** <https://discourse.bokeh.org/t/is-there-a-generic-api-for-setting-plot-annotations/10900>\
**Category:** Community Support\
**Created:** [September 29, 2023, 4:35pm UTC](https://discourse.bokeh.org/t/is-there-a-generic-api-for-setting-plot-annotations/10900 "2023-09-29T16:35:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hrbigelow](https://discourse.bokeh.org/user_avatar/discourse.bokeh.org/hrbigelow/32/5817_2.png) [@hrbigelow](https://discourse.bokeh.org/u/hrbigelow)\
**Post date:** [September 29, 2023, 4:35pm UTC](https://discourse.bokeh.org/t/is-there-a-generic-api-for-setting-plot-annotations/10900/1 "2023-09-29T16:35:47Z")

</div>

Hi,

If I have an app that allows the user to specify plot annotatiaons in a YAML format, which becomes a python nested dict, for example, here might be some user-provided annotations:

```python
annot = {
    'title': {
        'text': 'perplexity',
        'font_size': '24px',
    },
    'xaxis': {
        'axis_label': 'sgd steps',
        'font_size': '16px',
    }
    ....
}

```

I wondered, what is the most ergonomic way of assigning these to a given figure?  
In the [annotations](https://docs.bokeh.org/en/latest/docs/user_guide/basic/annotations.html) docs, I see some examples of setting various annotations directly as python properties or sub-properties. For example:

```python
p.title.text = 'perplexity'
p.title.font_size = '24px'
p.xaxis.axis_label = 'sgd steps'
p.xaxis.font_size = '16px'
...

```

I would like to avoid writing the code like:

`p. __dict__ [key]. __dict__ [key2] = val`

Wondering if there was a better way to do this generically. Thanks in advance for any help!

Best,

Henry

---

<div class="post-metadata">

**Author:** ![Bryan](https://discourse.bokeh.org/user_avatar/discourse.bokeh.org/bryan/32/3625_2.png) [@Bryan](https://discourse.bokeh.org/u/Bryan)\
**Post date:** [September 29, 2023, 4:45pm UTC](https://discourse.bokeh.org/t/is-there-a-generic-api-for-setting-plot-annotations/10900/2 "2023-09-29T16:45:45Z")

</div>

> [@hrbigelow](#):
>
> ```auto
> p. __dict__ [key]. __dict__ [key2] = val
> 
> ```

FYI this almost certainly won’t work. In order to support cross-runtime validation, serialization, and documentation, Bokeh properties are actually object descriptors on metaclasses. They need to be actually set as actual attributes in order to invoke the critical descriptor protocol codepaths. So you’d need to go through `setattr` for this.

You could potentially leverage themes in this case:

[https://docs.bokeh.org/en/latest/docs/user\_guide/styling/themes.html](https://docs.bokeh.org/en/latest/docs/user_guide/styling/themes.html)

Themes actually allow you to specify values for any Bokeh property. But note that they are type-based, per Document. e.g. you can set a new default font size for _all_ title objects, or tick label orientations on _all_ axes. You could technically specify a default title text, but it will apply to all plots (so probably not useful unless you just have one plot).

---

<div class="post-metadata">

**Author:** ![Bryan](https://discourse.bokeh.org/user_avatar/discourse.bokeh.org/bryan/32/3625_2.png) [@Bryan](https://discourse.bokeh.org/u/Bryan)\
**Post date:** [September 29, 2023, 4:48pm UTC](https://discourse.bokeh.org/t/is-there-a-generic-api-for-setting-plot-annotations/10900/3 "2023-09-29T16:48:57Z")

</div>

Oh, there is also `update` on Models that you can apply to individual Bokeh objects (e.g. to set individual title for individual distinct plots):

[https://docs.bokeh.org/en/latest/docs/reference/core/has\_props.html#bokeh.core.has\_props.HasProps.update](https://docs.bokeh.org/en/latest/docs/reference/core/has_props.html#bokeh.core.has_props.HasProps.update)

It’s a very rarely used function, but it’s probably more along the lines of what you want, since you could pass in a dict of values as `kwargs`. It won’t process nested values, though. You’ll still need to get ahold of individual objects to update a batch of its properties.

---

<div class="post-metadata">

**Author:** ![hrbigelow](https://discourse.bokeh.org/user_avatar/discourse.bokeh.org/hrbigelow/32/5817_2.png) [@hrbigelow](https://discourse.bokeh.org/u/hrbigelow)\
**Post date:** [September 29, 2023, 5:05pm UTC](https://discourse.bokeh.org/t/is-there-a-generic-api-for-setting-plot-annotations/10900/4 "2023-09-29T17:05:07Z")

</div>

Hi Bryan,

Thanks again! The `update` function looks nice - That will be helpful and I can make it work. Themes might work too, although I am using multiplle plots per page.

Best,

Henry
