Two data tables with two to_clipboard() buttons. Bug?

In some project I’m working on I noticed a weird behavior.
The page has two tables - each has a “copy data” button beneath it (copying the dataframe to clipboard).
Trying to copy the first table with the corresponding button click results in copying the second table.

Here is a simple code to reproduce:

import pandas as pd
import streamlit as st


data1 = pd.DataFrame({'a': [1,2,3,4],
                      'b': [10,20,30,40]})
data2 = pd.DataFrame({'A': [0.1,0.2,0.3,0.4],
                      'B': [0.11,0.22,0.33,0.44]})

st.button('copy data1', on_click= data1.to_clipboard(), key='data1')

st.button('copy data2', on_click= data2.to_clipboard(), key='data2')

Just press the first copy button and paste to excel. What I’m getting is the second table.

  • Streamlit version: 1.11.1
  • Python version: 3.9.12
  • Using Conda virtual environment
  • OS version: Windows 10
  • Browser version: Firefox 105.0.3 (64-bit)

st.button('copy data2', on_click= data2.to_clipboard(), key='data2') will call data2.to_clipboard() each time that line of code is executed, regardless of whether you click the button or not, and will pass the return value (None) as the on_click parameter.

Use just the method as callback, not the return value of a method call: on_click=data2.to_clipboard

1 Like

Thank you!