streamlit / streamlit

Streamlit — A faster way to build and share data apps.
https://streamlit.io
Apache License 2.0
35.98k stars 3.11k forks source link

Callbacks for custom components latest streamlit update (1.34.0) #8608

Closed Socvest closed 6 months ago

Socvest commented 7 months ago

Checklist

Summary

In attempt to create a dynamic filter in a dialog box, I stumbled on the fact updating a session_state variable via a custom component is not possible.

Reproducible Code Example

Something as simple as this code does not execute:

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

@st.experimental_dialog(title="Test", width="small")
def test_dialog():

    import streamlit_antd_components as sac

    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'),
        ], label='label', index=0, align='center', radius='md', multiple=False, key="testing123", on_change=onChangeTest
    )
    st.write(st.session_state["test_text"])

st.button("Test", key="test_btn")

if st.session_state["test_btn"]:
    test_dialog()

But it works with native widgets:

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

@st.experimental_dialog(title="Test", width="small")
def test_dialog():

    import streamlit_antd_components as sac

    def onChangeTest():

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

       st.radio("dummy label", ["one", "two", "three", "four"], key="testing123",on_change=onChangeTest )

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

st.button("Test", key="test_btn")

if st.session_state["test_btn"]:
    test_dialog()


### Steps To Reproduce

_No response_

### Expected Behavior

Just like the native code updates in the dialog, I expect custom components to also update in the dialog. But it only works for native widgets and not the custom components. 

### Current Behavior

_No response_

### Is this a regression?

- [ ] Yes, this used to work in a previous version.

### Debug info

- Streamlit version: 1.34.0
- Python version: 3.9
- Operating System: Windows 11
- Browser: Chrome

### Additional Information

_No response_
github-actions[bot] commented 7 months ago

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Visits

kajarenc commented 7 months ago

Hey @Socvest , thank you for opening this issue!

In general, we don't have an official way to support callbacks on custom components (https://github.com/streamlit/streamlit/issues/3977), but it looks like the latest release broke some workarounds that worked previously (https://github.com/victoryhb/streamlit-option-menu/issues/70). (I saw your comment under the last issue.)

@raethlein could have more information about this, but as far as I understood, this particular case doesn't connect with st.experimental_dialog behavior.

Socvest commented 6 months ago

Hey @kajarenc thanks for getting back to me. Yeah, I did further investigations and its more related to the callback work arounds for custom components than st.experimental_dialog.

raethlein commented 6 months ago

Hey @Socvest, 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 😉 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).

Socvest commented 6 months ago

Thank you! Appreciate this very much guys, thanks!