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 Nina, Thanks for your valuable response
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()