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!

1 Like

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

4 Likes

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

2 Likes

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

1 Like

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

4 Likes

Hi,

This works well!

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

Thank you

1 Like

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)

In case it helps anyone coming back to this, I stumbled across the following library, st-copy-to-clipboard. It works both locally and in the cloud as far as I can tell. I have tested this with Streamlit version 1.30.0.

Hope this helps!

2 Likes

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