Callback onChange not working for mui.Pagination in streamlit_elements since Streamlit 1.34

Since Streamlit v. 1.34 the callback in mui.Pagination from the streamlit_elements package does not work anymore:

mui.Pagination(count=count_pages, onChange=make_pagination, page=show_page,color="primary", variant="outlined", sx={"margin": "auto",width": "40%"})

This is due to the changes regarding callback functions in custom components.

Is there a solution or workaround despite the adaption by the package author? :slight_smile:

Hey!
I have the same issue
I was able to resolve it by modifying the source code.
my solution is here

1 Like

Hello there!
I have applied your suggested solution to modify as custom components it in callback.py but still it has no effect when i am trying to use onChange event in mui.checkBox().
do i also need to do something after modifying the source code?

Hi!
Unfortunately, the workaround stopped working in the latest versions of streamlit :frowning:
But now I use this solution - it work well in my project

2 Likes

Hi Nina, Thanks for your valuable response :slight_smile:
But the solution you recommended. i do not find that function written in callback.py.
i see def __patch_register_widget(register_widget) function .
Do i need to add this def patch_modules_streamlit_elements as new function in callback.py or create a new file for it?

could you also please tell me in which older version of streamlit ,1.32 or 1.33 does this call back functions were actually working? i am using streamlit 1.39.0 version


Normally, you don’t need to manage the ‘callback. py’ file. After installing the ‘streamlit elements’ package, you just need to run’ streamlit run text. py ‘directly in the newly created’ text. py 'file.
It should be noted that after running, be sure to restart your own streamlit application.

def patch_modules_streamlit_elements(file: str, old_line: str, new_line: str):
    import streamlit_elements
    import os

    relative_file_path = "core/callback.py"
    library_root = list(streamlit_elements.__path__)[0]
    file_path = os.path.join(library_root, relative_file_path)

    with open(file_path, "r") as file:
        lines = file.readlines()

    is_changed = False
    for index, line in enumerate(lines):
        if old_line in line:
            print(f"Replacing line {index + 1} in {file_path}")
            lines[index] = line.replace(old_line, new_line)
            is_changed = True

    if is_changed:
        with open(file_path, "w") as file:
            file.writelines(lines)
        import importlib
        importlib.reload(streamlit_elements)

    return True

def patch_streamlit_elements():
    # fix 1.34.0
    patch_modules_streamlit_elements(
        "core/callback.py",
        "from streamlit.components.v1 import components",
        "from streamlit.components.v1 import custom_component as components\n",
    )


    #fix 1.40.0
    patch_modules_streamlit_elements(
        "core/callback.py",
        '        user_key = kwargs.get("user_key", None)\n',
        """
        try:
            user_key = None
            new_callback_data = kwargs[
                "ctx"
            ].session_state._state._new_session_state.get(
                "streamlit_elements.core.frame.elements_frame", None
            )
            if new_callback_data is not None:
                user_key = new_callback_data._key
        except:
            user_key = None
        """.rstrip()
        + "\n",
    )


if __name__ == "__main__":  
    patch_streamlit_elements()
1 Like

Everything is normal, thank you very much!