Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,10 +12,11 @@ javascript_code = """
|
|
| 12 |
// Function to call and send data back to Python
|
| 13 |
function callAndCapture() {
|
| 14 |
const result = myJavaScriptFunction();
|
|
|
|
| 15 |
window.parent.postMessage({ isStreamlitMessage: true, type: 'streamlit:js_return', data: result }, '*');
|
| 16 |
}
|
| 17 |
|
| 18 |
-
// Add event listener to
|
| 19 |
window.addEventListener('message', function(event) {
|
| 20 |
if (event.data && event.data.type === 'streamlit:buttonClick') {
|
| 21 |
callAndCapture();
|
|
@@ -28,7 +29,7 @@ javascript_code = """
|
|
| 28 |
# Embed JavaScript into Streamlit app
|
| 29 |
html(javascript_code, height=300)
|
| 30 |
|
| 31 |
-
# Placeholder
|
| 32 |
result_placeholder = st.empty()
|
| 33 |
|
| 34 |
# Add Streamlit button to trigger JavaScript function
|
|
@@ -36,43 +37,24 @@ if st.button("Call JavaScript Function"):
|
|
| 36 |
st.write("Waiting for JavaScript response...")
|
| 37 |
trigger_js = """
|
| 38 |
<script>
|
|
|
|
| 39 |
window.parent.postMessage({ type: 'streamlit:buttonClick' }, '*');
|
| 40 |
</script>
|
| 41 |
"""
|
| 42 |
html(trigger_js, height=0)
|
| 43 |
|
| 44 |
-
#
|
| 45 |
st.markdown(
|
| 46 |
"""
|
| 47 |
<script>
|
| 48 |
-
//
|
| 49 |
window.addEventListener('message', (event) => {
|
| 50 |
if (event.data && event.data.type === 'streamlit:js_return') {
|
|
|
|
| 51 |
const result = event.data.data;
|
| 52 |
-
// Send the result to Streamlit via a custom event
|
| 53 |
document.dispatchEvent(new CustomEvent('StreamlitResult', { detail: result }));
|
| 54 |
}
|
| 55 |
});
|
| 56 |
|
| 57 |
-
// Dispatch
|
| 58 |
-
|
| 59 |
-
const data = e.detail;
|
| 60 |
-
window.parent.postMessage({ type: 'streamlit:result', data: data }, '*');
|
| 61 |
-
});
|
| 62 |
-
</script>
|
| 63 |
-
""",
|
| 64 |
-
unsafe_allow_html=True,
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
-
# Handle the result sent from JavaScript
|
| 68 |
-
if "js_result" not in st.session_state:
|
| 69 |
-
st.session_state["js_result"] = ""
|
| 70 |
-
|
| 71 |
-
# Update session state and display the result
|
| 72 |
-
try:
|
| 73 |
-
received_result = st.session_state["js_result"]
|
| 74 |
-
# Only display if we have new data
|
| 75 |
-
if received_result:
|
| 76 |
-
result_placeholder.write(f"Result from JavaScript: {received_result}")
|
| 77 |
-
except Exception as e:
|
| 78 |
-
st.error(f"An error occurred: {e}")
|
|
|
|
| 12 |
// Function to call and send data back to Python
|
| 13 |
function callAndCapture() {
|
| 14 |
const result = myJavaScriptFunction();
|
| 15 |
+
// Send the result back to Streamlit
|
| 16 |
window.parent.postMessage({ isStreamlitMessage: true, type: 'streamlit:js_return', data: result }, '*');
|
| 17 |
}
|
| 18 |
|
| 19 |
+
// Add an event listener to listen for messages from Streamlit
|
| 20 |
window.addEventListener('message', function(event) {
|
| 21 |
if (event.data && event.data.type === 'streamlit:buttonClick') {
|
| 22 |
callAndCapture();
|
|
|
|
| 29 |
# Embed JavaScript into Streamlit app
|
| 30 |
html(javascript_code, height=300)
|
| 31 |
|
| 32 |
+
# Placeholder to display results
|
| 33 |
result_placeholder = st.empty()
|
| 34 |
|
| 35 |
# Add Streamlit button to trigger JavaScript function
|
|
|
|
| 37 |
st.write("Waiting for JavaScript response...")
|
| 38 |
trigger_js = """
|
| 39 |
<script>
|
| 40 |
+
// Trigger the JavaScript function via a postMessage event
|
| 41 |
window.parent.postMessage({ type: 'streamlit:buttonClick' }, '*');
|
| 42 |
</script>
|
| 43 |
"""
|
| 44 |
html(trigger_js, height=0)
|
| 45 |
|
| 46 |
+
# Add a listener for JavaScript responses
|
| 47 |
st.markdown(
|
| 48 |
"""
|
| 49 |
<script>
|
| 50 |
+
// Listen for messages from JavaScript
|
| 51 |
window.addEventListener('message', (event) => {
|
| 52 |
if (event.data && event.data.type === 'streamlit:js_return') {
|
| 53 |
+
// Pass the data back to Streamlit as a new message
|
| 54 |
const result = event.data.data;
|
|
|
|
| 55 |
document.dispatchEvent(new CustomEvent('StreamlitResult', { detail: result }));
|
| 56 |
}
|
| 57 |
});
|
| 58 |
|
| 59 |
+
// Dispatch data from CustomEvent to Streamlit
|
| 60 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|