Add annotation to chart directly in Streamlit

Hi @Fabio :wave:

It is certainly possible! Here’s an example that uses st.empty() and st.text_input() to achieve that behavior without st.session_state:

import streamlit as st
import plotly.graph_objects as go
import numpy as np

annotation_1 = st.text_input("Annotation text:", key="annotation_1")

animals = ["giraffes", "orangutans", "monkeys"]
fig_1 = go.Figure([go.Bar(x=animals, y=[20, 14, 23])])

chart_1 = st.empty()
chart_1.plotly_chart(fig_1)

if annotation_1:
    fig_1.add_annotation(
        text=annotation_1,
        xref="paper",
        yref="paper",
        x=0,
        y=1.1,
        showarrow=False,
        align="left",
        xanchor="left",
        font=dict(size=20, color="#242526"),
    )
    chart_1.plotly_chart(fig_1)


annotation_2 = st.text_input("Annotation text:", key="annotation_2")

x = np.arange(10)
fig_2 = go.Figure(data=go.Scatter(x=x, y=x ** 2))

chart_2 = st.empty()
chart_2.plotly_chart(fig_2)

if annotation_2:
    fig_2.add_annotation(
        text=annotation_2,
        xref="paper",
        yref="paper",
        x=0,
        y=1.1,
        showarrow=False,
        align="left",
        xanchor="left",
        font=dict(size=20, color="#242526"),
    )
    chart_2.plotly_chart(fig_2)

plotly-annotation

Happy Streamlit’ing! :balloon:
Snehan