How can I pass parameters into function which defined at "on_click" inside st.button

Summary

I recently work on streamlit and try to plot marker on folium map (for loop by lat lon on each dataframe rows) base on customized dataframe which selected from user.

I have found some issued
1.I cannot pass the input parameter(folium map object) into function when using button. I need to pass parameter because I have to use for loop to generate marker inside the function.

2.Global variable does not work as well - Because I cannot pass the parmeter via the button, so I decided to use button to initiate function and then use Global variable concept inside function instead, but seem streamlit cannot read global variable as well.

3.Marker plot only work when I follow this step

  • Add map m=folium.Map(xxxx)
  • folium.Marker(xxx).add_to(m)
  • folium_static(m, width=1400, height=450)

Which I cannot do this because I need to initially show map without marker, and only plot marker after user press button, However If I put folium_static inside the function (plotting maker function). the screen will add another map instead of ploting marker into the existing map

Steps to reproduce

Code snippet:

def add_marker():
    global m
    folium.Marker([25.042892210970322, 121.54525577135475]).add_to(m)

m = folium.Map(location=[25.042892210970322, 121.54525577135475], zoom_start=8)
folium_static(m, width=1400, height=450)

with st.sidebar:
    add_maker_button = st.button("Plot_marker", on_click=add_marker)

You can pass arguments to on_click or on_change callback functions with the args keyword.

def speak(word):
    st.write(word)

st.button('Say hi', on_click=speak, args=('Hi!',))

Just make sure you pass a tuple to the args keyword, even if you are just sending a single argument.

3 Likes