Copy dataframe to clipboard

Well done.
I want to know if there have 2 or more dataframe in web page, how should I use the clip function for each dataframe?
And the gif picture is through which method to insert into answer region?
Thank you.

Thankyou so much Lots of Love @ash2shukla. Glad it worked for everyone was a long outstanding query.

1 Like

Is there any long term plan to adapt the streamlit-bokeh-events into streamlit?

1 Like

Hi @ash2shukla I was trying today in production and got some errors like this

Investigated and it was involved in errors around CORS XSRF.

Can you please help.

I think it will work on localhost else it should be served over https.
Is your streamlit server running over https ?

No its HTTP and Its hosted on Internal VM Server and Other users are given access to it via Port Allowance

When I press Copy It copies on VM’s Clipboard rather than Client Side.

Hi Check this link for clipboard api availability. You cant use clipboard api on non secure origins ( http )

Okay… sounds like a limitation for now. Any other workaround you may suggest?

Hi, @ash2shukla ! It just doesnt work for me, I tried either to replace “button_click” to ButtonClick module as seen in another post or using another browser (tried on chrome and edge).
It’s almost exactly as you exemplified above

copy_button = Button(label="Copy DF")
    copy_button.js_on_event(ButtonClick, CustomJS(args=dict(df=df_export.to_csv(sep='\t')), code="""
    no_event = streamlit_bokeh_events(

The output is nothing, doesnt return any error nor paste the data. Is there something i’m missing?

1 Like

Hi @betinhosaad , can u see in console whether any error pops up when u click the button ? Try adding a console.log prior to navigator.clipboard thing to see if the js code runs at all or not.

Uncaught (in promise) DOMException: The Clipboard API has been blocked because of a permissions policy applied to the current document.

Trying to find out how to fix this

@ash2shukla is it possible to allow copy paste using bokeh? couldnt find anything helpful :pensive:


Any news about this topic ? Did you managed to create something ?

Not yet, the work around was to create an export button that generate a csv instead of actually copying the dataframe.
Hope it helps:

csv = df_export.to_csv(index=False)
b64 = base64.b64encode(csv.encode()).decode()
# st.markdown('### **⬇️ Download output CSV File **')
href = f'<a class="streamlit-button small-button"
href="data:file/csv;base64,{b64}" download="{df_export.columns[0]}.csv">Export</a>'
st.markdown(href, unsafe_allow_html=True)

1 Like

My objective was to export this clipboard to excel, i didn’t find a easy way to use clipboard, instead i’m downloading df as xlsx and use it to copy for my sheets.

I used Heroku to deploy this app, so using href didn’t work, i think something related to VM used… So i used new function st.download_button and it’s works

I managed that with this:

# Function to convert csv to xlsx
def to_excel(df):
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1',index=False)
    processed_data = output.getvalue()
    return processed_data

This function i get from one topic … And to download it…

    df= to_excel(df)


See if could be useful for you…

Best Regards.

1 Like

How do I identify and throw a message if the data has been copied using this. For example: Once a person click on the button, Can I throw some kind of message saying dataframe as been copied?

This works well for me (on windows):

import pyperclip
# Your code ...# 
1 Like

I think pyperclip doesn’t work when the app is run on streamlit. At least, it didn’t work for me! Any solution is appreciated.

I think this will only work if you are running on your local. If you deploy that on a server, pyperclip will try to use the server process’s clipboard instead of the one the user’s browser is running in. If it works at all, it will be uselessly sitting in a clipboard in a headless process on a server, not your local clipboard.

The current dataframe version supports copy to clipboard of all columns which I believe solves the question of the topic. You can find some information here.