import streamlit as st # Define the JavaScript to be embedded javascript_code = """ """ # Create a container for HTML/JS script html_container = st.empty() html_container.html(javascript_code + "
", height=300) # Create a placeholder message message_placeholder = st.empty() # Add a button in Streamlit to send a message to the iframe to call JavaScript if st.button("Call JavaScript Function"): js = """ """ html_container.html(javascript_code + js + "", height=300) # Future-proofing to listen for messages from the JavaScript side if "js_result" not in st.session_state: st.session_state["js_result"] = "" # Handle messages from JavaScript st.markdown( """ """, unsafe_allow_html=True, ) # Python-side listener to write the result to the placeholder try: import streamlit.components.v1 as components messages = components.html( """ """, height=0, ) if_messages_received = messages._deserialize() if if_messages_received and if_messages_received != st.session_state["js_result"]: st.session_state["js_result"] = if_messages_received message_placeholder.markdown("_" + if_messages_received + "_") except: pass st.session_state["js_result"]