Streamlit - Using selected text as input on button click

1.1k Views Asked by At

Is there a way in Streamlit to:

  • Select text in a text area

  • Click a button to have the selected text be used an an input to a backend function

So this would take only the text excerpt selected by the user and send that as a function input and not the entire text in the text area.

Researched and haven't found a way to do this. Thanks.

enter image description here

1

There are 1 best solutions below

1
matleg On

Here is a simple example with a callback associated to a button, on which you give a text input by a user as argument:

import streamlit as st

st.session_state["user_input"] = st.text_input(label="enter text")


def my_callback(text_to_display):
    st.text(str(text_to_display))


st.button("display text", on_click=my_callback, args=(st.session_state["user_input"],))

It gives that when running: enter image description here

I used these text input doc and button doc.