Output of callback function inserts at top of page instead of bottom

Hi everyone,

I have a similar question to this unanswered one, Button callback function placing the plots at the top of webapp.

I am using an external library st-annotated-text to annotate text.
Issue: The command annotated_text(*streamlit_args) inserts the output at the top of the webpage, but I need it to be inserted at the bottom after the text_area itself. How can I achieve this?

Here is a screenshot of the issue.

Below is a minimal replicable version of the app.py.

def main():

    import streamlit as st
    from annotated_text import annotated_text

    # Define function to pass to st.text_input's on_change argument
    def run_model():
        input_text = st.session_state.input_text
        streamlit_args = [(input_text, "category")]
        annotated_text(*streamlit_args)

    st.text_area(label="Insert text here.", on_change=run_model, key="input_text")


if __name__ == "__main__":
    main()

Do not call annotated_text in the callback.

I’ll need to generate the streamlit_args within the callback because in my actual code, that is the model output. Could you show me an example of how to fetch that value from the callback to use annotate_text outside of it?

In the callback, store the model output in session_state.

st.session_state.streamlit_args = [(input_text, "category")]

Here is the new code following your suggestion. Perhaps I’m implementing the annotated_text part incorrectly, but I’ll need to reference the st.session_state.streamlit in annotated_text, but it won’t be defined at the point of running annotated_text. I also tried adding if "streamlit_args in st.session_state before running annotated_text, but it will just end up not running since it detects that streamlit_args is not in session_state.

Could you let me know how I’m implementing it wrongly?

def main():

    import streamlit as st
    from annotated_text import annotated_text

    # Define function to pass to st.text_input's on_change argument
    def run_model():
        input_text = st.session_state.input_text
        streamlit_args = [(input_text, "category")]

    st.text_area(label="Insert text here.", on_change=run_model, key="input_text")
    annotated_text(*st.session_state.streamlit_args)


if __name__ == "__main__":
    main()

I’ll need to reference the st.session_state.streamlit in annotated_text , but it won’t be defined at the point of running annotated_text .

Then check if it is defined before calling annotated_text.

st.text_area(label="Insert text here.", on_change=run_model, key="input_text")
if  "streamlit_args" in st.session_state:
    annotated_text(*st.session_state.streamlit_args)
1 Like

Much appreciated Goyo!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.