Find and Replace functionality in `st.text_area`

Is there a way to find and replace text within a st.text_area component? Is there another streamlit component that has a find and replace functionality?

If you assign a key to the widget, you can manipulate its contents from Session State.

import streamlit as st

if "my_widget" not in st.session_state:
    st.session_state.my_widget = "meow meow meow meow meow meow meow meow meow"

def replace(old, new):
    st.session_state.my_widget = st.session_state.my_widget.replace(old, new)

st.text_area("The cat says:", key="my_widget")

st.button("meow to woof", on_click=replace, args=["meow","woof"])
st.button("woof to meow", on_click=replace, args=["woof","meow"])

Thank you @mathcatsand that is what I was looking for. Is it possible to pass the args to st.button from within the app. So that a user could use the replace function from the app?

Edit:
never mind, I figured out how to do it.

Yes, you can make all kinds of variations of this:

import streamlit as st

if "my_widget" not in st.session_state:
    st.session_state.my_widget = "meow meow meow meow meow meow meow meow meow"

def replace():
    st.session_state.my_widget = st.session_state.my_widget.replace(st.session_state.old, st.session_state.new)

st.text_area("The cat says:", key="my_widget")

st.text_input("Replace this", value="meow", key="old")
st.text_input("With this:", value="woof", key="new")

st.button("Replace", on_click=replace)

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