Callbacks on custom components don't seem to be working in latest streamlit update

Ver 1.33.0 example:

streamlit-Home-2024-05-05-14-05-32

After update to latest 1.34.0

streamlit-Home-2024-05-05-14-05-20

The code is below:

import streamlit_antd_components as sac 

if "test_text" not in st.session_state:
    st.session_state["test_text"] = None

def onChangeTest():

    st.session_state["test_text"] = st.session_state["testing123"] 

sac.chip(
    items=[
        sac.ChipItem(label='apple'),
        sac.ChipItem(label='google'),
        sac.ChipItem(label='github', icon='github'),
        sac.ChipItem(label='twitter', icon='twitter'),
        sac.ChipItem(label='disabled'),
    ], label='label', index=0, align='center', radius='md', multiple=False, key="testing123", on_change=onChangeTest
)

st.write(st.session_state["test_text"])


UPDATE
And it seems to be affecting other custom components like the popular streamlit-option-menu here.

It seems that on_change has no effect with the new Streamlit version. Your onChangeTest function is not called!

Anyway st.session_state[‘testing123’] contains the right value. Why programming like you did?

May be I’m wrong, but it seems to me that the “Streamlit philosophy” is rather to avoid callbacks.

x = st.button(
)
if x:



Why in your case, you don’t have:
x = sac.chip( 
 ,key = “xxxx”, 
)
if st.session_state.xxxx == ‘google’:


if st.session_state.xxxx == ‘apple’:



Regards

Hey,

The function onChangeTest is called via the on_change=onChangeTest. So when I interact with the widget, the function runs.

Other native widgets have this feature too, with buttons having on_click.

Insert a line “print(‘xxxx’)” into your callback function and you will see that it is not called in this latest version of Streamlit! I took your code and did it!

You should also discuss with the author of the “sac” package and ask for their opinion.

I left a reply on this issue:

the internal package structure has changed due to a refactoring; if you change the import in the streamlit_callback.py to from streamlit.components.v1 import custom_component as _components, it should work again. If you are not the author of sac, this might be something to bring to them.

That being written, this is not an official API we are going to guarantee from Streamlit-side :wink: However, we will look into this more closely and try to come up with a clean API for the future so that this patch is not needed anymore. In the meantime, I hope this^ unblocks you (for now).

2 Likes

Thank you! Appreciate this so much :slight_smile:

Hey, thanks for the fix. But there seems to be some inconsistency in the result. I implemented it in my app and other custom components I built and at times, it does not seem to work. I know it was a hacky workaround but would have been nice to have had it.

Anyway, thanks for trying :slight_smile:

I am sorry to hear that the new workaround does not work as reliably! We are looking into adding this as a native feature, see this PR: Allow passing on_change_callback for CustomComponents by raethlein · Pull Request #8633 · streamlit/streamlit · GitHub
If you want to and have some time, the PR has a wheel file attached to it: https://core-previews.s3-us-west-2.amazonaws.com/pr-8633/streamlit-1.34.0-py2.py3-none-any.whl which you could try out in the meantime :slight_smile:

2 Likes

Thanks for your speedy reply. Downloading it, will get back to you shortly :slight_smile:

So I have downloaded it (loooong ahah), how do I make it work? Not sure from the git PR. Could you give an example?

DW, I got it, works like a charm! :slight_smile:

1 Like

Thank you, I am also looking for a solution to this.

Amazing! Thank you so much for the validation :slight_smile: I will try to wrap it up next week and it should land in release 1.35 or 1.36 (more likely 1.36)

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.