Plotly allows you to add annotations to a chart, for instance under the chart title like so.
But the annotation is hard coded.
user_input="This is my hard-coded annotathon that helps understand the chart and that I would like to type in with a widget."
fig.add_annotation(text = user_input,
xref = "paper",
yref = "paper",
x = 0,
y = 1.060,
showarrow = False,
align = "left",
xanchor = "left",
font = dict(size = 11, color = "#242526"))
I would like the user to be able write the annotation into a text input widget and (after re-submitting ) magically show up in the chart. The text input widget should be on top or just below each chart (there is more than one chart).
I have tried adding a widget, but when, after entering my comment, I rerun the chart, my widget naturally goes to blank and no annotation shows up in the chart.
I am sure this is a job for st.session_state() but I have no idea how to implement it.
It should be possible to annotate each chart independently with a different widget .
At the end, I used session state because I have a submit button to plot the charts.
I have one last problem. In the text input widget, when I enter my text, I get a “Press enter to apply” suggestion that in my case is misleading since I need to submit. Any way to make it go away?
The only time the “Press Enter to apply” prompt doesn’t appear is when the text input widget is within a form. When outside of a form, there is no way to disable the behavior. Here’s an example where you will not see the prompt:
import streamlit as st
with st.form("my_form"):
annotation_1 = st.text_input("Annotation text:", key="annotation_1")
submit = st.form_submit_button("Submit")
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.