Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit.components.v1 import html
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Define JavaScript code
|
| 5 |
javascript_code = """
|
| 6 |
<script>
|
|
@@ -57,4 +61,46 @@ st.markdown(
|
|
| 57 |
});
|
| 58 |
|
| 59 |
// Dispatch data from CustomEvent to Streamlit
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit.components.v1 import html
|
| 3 |
|
| 4 |
+
# Debugging Headers and Cookies
|
| 5 |
+
st.write("Headers:", st.context.headers)
|
| 6 |
+
st.write("Cookies:", st.context.cookies)
|
| 7 |
+
|
| 8 |
# Define JavaScript code
|
| 9 |
javascript_code = """
|
| 10 |
<script>
|
|
|
|
| 61 |
});
|
| 62 |
|
| 63 |
// Dispatch data from CustomEvent to Streamlit
|
| 64 |
+
document.addEventListener('StreamlitResult', (e) => {
|
| 65 |
+
const data = e.detail;
|
| 66 |
+
window.parent.postMessage({ type: 'streamlit:result', data: data }, '*');
|
| 67 |
+
});
|
| 68 |
+
</script>
|
| 69 |
+
""",
|
| 70 |
+
unsafe_allow_html=True,
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Use an iframe for communication
|
| 74 |
+
if "js_result" not in st.session_state:
|
| 75 |
+
st.session_state["js_result"] = ""
|
| 76 |
+
|
| 77 |
+
try:
|
| 78 |
+
# Listen for result data from JavaScript
|
| 79 |
+
received_result = html(
|
| 80 |
+
"""
|
| 81 |
+
<script>
|
| 82 |
+
window.addEventListener('message', (event) => {
|
| 83 |
+
if (event.data && event.data.type === 'streamlit:result') {
|
| 84 |
+
const data = event.data.data;
|
| 85 |
+
// Send the result back to Streamlit
|
| 86 |
+
document.dispatchEvent(new CustomEvent('PythonReceivedResult', { detail: data }));
|
| 87 |
+
}
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
document.addEventListener('PythonReceivedResult', (e) => {
|
| 91 |
+
const received = e.detail;
|
| 92 |
+
document.getElementById('result-placeholder').innerText = received;
|
| 93 |
+
});
|
| 94 |
+
</script>
|
| 95 |
+
<div id="result-placeholder"></div>
|
| 96 |
+
""",
|
| 97 |
+
height=0,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# If there's a new result, update it in session state
|
| 101 |
+
if received_result and received_result != st.session_state["js_result"]:
|
| 102 |
+
st.session_state["js_result"] = received_result
|
| 103 |
+
result_placeholder.write(f"Result from JavaScript: {received_result}")
|
| 104 |
+
|
| 105 |
+
except Exception as e:
|
| 106 |
+
st.error(f"An error occurred: {e}")
|