On click custom component

Is it possible to have an on_click parameter with similar behavior as the normal st.button element, but than with a custom component?

1 Like

Hi @hoop_snake

Could you elaborate more on the use case on how this should be different than the on_click parameter in st.button. Using on_click with a callback function would work irregardless if it’s a component. Perhaps some additional context would help to better understand your use case.

1 Like

It would allow for customized buttons in terms of layout and as such be a valuable addition (e.g. styled as a link or a card)

1 Like

It’s definitely possible to add on_click handlers on custom components. There are a number of “clickable object” components that have been created by the community Streamlit Components - Community Tracker (for example Custom component to display clickable images), so you might look and see if any of the existing ones solve the problem you’re looking for.

If you don’t see what you’re looking for after looking around on the forum, then you could definitely create your own with an on_click parameter.

1 Like

Sorry for the confusion. We’re specifically pointing at the on_click and on_change callback behavior that some of the streamlit components have. They don’t cause full script reruns, but rather point to running a specific function.

for example: in case one wants to make use of st.query_params after clicking on a component button.

Note that on could set a “normal” link with the query parameters included, but that has an even worse result: full rerun AND loss of session.

The normal behavior of streamlit widgets is that any change triggers a full app rerun. Do you mean that you would like to have a click on a custom component and have it run a function without running the rest of the app?

I’m not entirely sure if that’s possible – my intuition is that it won’t work, and that any interaction that ends up calling a custom python function will also cause the app to return.

If you goal specifically is to avoid a rerun, that should come much easier in the future when Streamlit finishes the “partial rerun” feature.

If you don’t mind if the whole app reruns, than having an on_click custom function is easy enough – you can see an example I did here for on_change (which is more complex than an on_click would need to be) here streamlit-keyup/src/st_keyup/__init__.py at main · blackary/streamlit-keyup · GitHub

1 Like

I’m referring to the chapter “Use callbacks to update Session State” from https://docs.streamlit.io/library/api-reference/session-state.

1 Like

found the item on github. With the switch_page actions available, I think it’s more important than ever to get this prioritized. Please vote here: