Copy dataframe to clipboard

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
@st.cache
def to_excel(df):
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1',index=False)
    writer.save()
    processed_data = output.getvalue()
    return processed_data

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

    df= to_excel(df)

    st.download_button(label="Download",data=df,file_name='df.xlsx')

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?

Hi!
This works well for me (on windows):

import pyperclip
# Your code ...# 
pyperclip.copy(my_dataframe.to_csv())
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.

Unless I am missing something, this is not really convenient for large dataframes, say > 1000 rows.

You can select all cells (CMD + A) and copy them to the clipboard. But I think for larger data, a download option would be better option for this usecase.

1 Like

That shortcut is what I was missing and it is good enough for me.

2 Likes

The supported keyboard shortcuts are linked in the Bulk edits section.

1 Like

Hi! It works indeed, however, what I find not very convenient is that you can’t copy the column names in this way. Only via loading a .csv file, which we don’t want to do, when we want to copy just a little bit of the values.

We would like it much better with an ability to choose and copy the column names as well.

it can’t work with http,so how to do?