How to add "Copy to Clipboard" Button to text area

Hello, I run Streamlit in the cloud. I have a text field (st.text_area) where the response of an API is returned.
How can I add a copy-to-clipboard button for the content of the text field?
Thanks!

There is no direct way to copy something as far as i know
But if you use markdown as code then you will get option to copy there

import streamlit as st
def main():
    st.title("Copy Paste in streamlit")
    pathinput = st.text_input("Enter your Path:")
    #you can place your path instead
    Path = f'''{pathinput}'''
    st.code(Path, language="python")
    st.markdown("Now you get option to copy")
if __name__ == "__main__":main()

here in this example you can get copy option

1 Like

Hi,
Alternatively, you can also use the pyperclip library to copy text to your clipboard:

import streamlit as st
import pyperclip

a=st.text_area('Type in the text_area and click copy')

if st.button('Copy'):
    pyperclip.copy(a)
    st.success('Text copied successfully!')
   

Cheers,
Moiz

1 Like

Thanks, but as far as I know pyperclip isnโ€™t running with the cloud version.

I tried but it didnt worked in past ig
i mean in cloud

3 Likes

Hi,

This works well!

Is there a way to know in the app which text got copied?

Thank you

Umm not sure
May be u can do a call every time copy button is clicked ( copy streamlit button)
And then update that value in a function and auto refresh

Soft fix kind of solution

st.code(โ€˜print(โ€œhiโ€)โ€™, language=โ€œpythonโ€)

Streamlit shows a copy icon next to this block of code, however if i click on that i donโ€™t get a message saying copied. How to achieve that?

here you are using markdown language features to copy it so it cant be achieved in this way
as it has nothing to do with streamlit

pyperclip works but its not working in community cloud

1 Like

st.code(your text here)