Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit.components.v1 import html
|
| 3 |
|
| 4 |
-
# Define JavaScript code
|
| 5 |
javascript_code = """
|
| 6 |
<script>
|
| 7 |
// Function to return a string
|
|
@@ -26,13 +26,14 @@ javascript_code = """
|
|
| 26 |
"""
|
| 27 |
|
| 28 |
# Embed JavaScript into Streamlit app
|
| 29 |
-
|
| 30 |
|
| 31 |
# Placeholder for displaying results
|
| 32 |
result_placeholder = st.empty()
|
| 33 |
|
| 34 |
# Add Streamlit button to trigger JavaScript function
|
| 35 |
if st.button("Call JavaScript Function"):
|
|
|
|
| 36 |
trigger_js = """
|
| 37 |
<script>
|
| 38 |
window.parent.postMessage({ type: 'streamlit:buttonClick' }, '*');
|
|
@@ -44,13 +45,20 @@ if st.button("Call JavaScript Function"):
|
|
| 44 |
st.markdown(
|
| 45 |
"""
|
| 46 |
<script>
|
|
|
|
| 47 |
window.addEventListener('message', (event) => {
|
| 48 |
if (event.data && event.data.type === 'streamlit:js_return') {
|
| 49 |
const result = event.data.data;
|
| 50 |
-
//
|
| 51 |
-
|
| 52 |
}
|
| 53 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
</script>
|
| 55 |
""",
|
| 56 |
unsafe_allow_html=True,
|
|
@@ -60,23 +68,11 @@ st.markdown(
|
|
| 60 |
if "js_result" not in st.session_state:
|
| 61 |
st.session_state["js_result"] = ""
|
| 62 |
|
|
|
|
| 63 |
try:
|
| 64 |
-
received_result =
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
window.addEventListener('message', (event) => {
|
| 68 |
-
if (event.data && event.data.type === 'streamlit:python_side') {
|
| 69 |
-
const data = event.data.data;
|
| 70 |
-
document.dispatchEvent(new CustomEvent('StreamlitResult', { detail: data }));
|
| 71 |
-
}
|
| 72 |
-
});
|
| 73 |
-
</script>
|
| 74 |
-
""",
|
| 75 |
-
height=0,
|
| 76 |
-
)
|
| 77 |
-
# Capture and update the session state
|
| 78 |
-
if received_result != st.session_state["js_result"]:
|
| 79 |
-
st.session_state["js_result"] = received_result
|
| 80 |
result_placeholder.write(f"Result from JavaScript: {received_result}")
|
| 81 |
except Exception as e:
|
| 82 |
st.error(f"An error occurred: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit.components.v1 import html
|
| 3 |
|
| 4 |
+
# Define JavaScript code
|
| 5 |
javascript_code = """
|
| 6 |
<script>
|
| 7 |
// Function to return a string
|
|
|
|
| 26 |
"""
|
| 27 |
|
| 28 |
# Embed JavaScript into Streamlit app
|
| 29 |
+
html(javascript_code, height=300)
|
| 30 |
|
| 31 |
# Placeholder for displaying results
|
| 32 |
result_placeholder = st.empty()
|
| 33 |
|
| 34 |
# Add Streamlit button to trigger JavaScript function
|
| 35 |
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' }, '*');
|
|
|
|
| 45 |
st.markdown(
|
| 46 |
"""
|
| 47 |
<script>
|
| 48 |
+
// Function to listen for messages from JavaScript
|
| 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 result to Streamlit when received
|
| 58 |
+
document.addEventListener('StreamlitResult', (e) => {
|
| 59 |
+
const data = e.detail;
|
| 60 |
+
window.parent.postMessage({ type: 'streamlit:result', data: data }, '*');
|
| 61 |
+
});
|
| 62 |
</script>
|
| 63 |
""",
|
| 64 |
unsafe_allow_html=True,
|
|
|
|
| 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}")
|