Unexpected button on_click behavior, reproducible with 2 lines of code

I create several buttons in a loop, but no matter which one I clicked, it sounds fired the on_click of last button.

I can repro the issue with the following code:

import streamlit as st

# The issue is, no matter if I clicked which of the 3 buttons, I always got "clicked 2" output 
for i in range(3):
    st.button(f"Button {i}", key=f"btn_{i}", on_click=lambda: st.warning(f"clicked {i}"))

To run it
streamlit run https://raw.githubusercontent.com/mattguo/streamlit_todo/main/maybe_bugs.py

Need some idea on how to fix it, Thanks.

Try this:

import streamlit as st

# The issue is, no matter if I clicked which of the 3 buttons, I always got "clicked 2" output 
for i in range(3):
    st.button(f"Button {i}", key=f"btn_{i}", on_click=lambda x: st.warning(f"clicked {x}"), args=[i])