awacke1 commited on
Commit
9d6ac78
Β·
verified Β·
1 Parent(s): a9751da

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # Define the JavaScript to be embedded
4
+ javascript_code = """
5
+ <script>
6
+ function myJavaScriptFunction() {
7
+ // Function to return a string
8
+ return "Hello from JavaScript!";
9
+ }
10
+
11
+ // Function to be called on button click; uses window.alert to display the return value
12
+ function callAndCapture() {
13
+ const result = myJavaScriptFunction();
14
+ // Use Streamlit's via the streamlit:component event to send data back to Python
15
+ window.parent.postMessage({isStreamlitMessage: true, type: 'streamlit:component', data: result}, '*');
16
+ }
17
+
18
+ // Add an event listener to handle messages from the iframe or parent window.
19
+ window.addEventListener('message', function(event) {
20
+ if (event.data.type === 'streamlit:buttonClick') {
21
+ callAndCapture();
22
+ }
23
+ });
24
+ </script>
25
+ <button onclick="callAndCapture()">Run JavaScript Function</button>
26
+ """
27
+
28
+ # Create a container for HTML/JS script
29
+ html_container = st.empty()
30
+ html_container.html(javascript_code + "<div id='returned_value'></div>", height=300)
31
+
32
+ # Create a placeholder message
33
+ message_placeholder = st.empty()
34
+
35
+ # Add a button in Streamlit to send a message to the iframe to call JavaScript
36
+ if st.button("Call JavaScript Function"):
37
+ js = """
38
+ <script>
39
+ window.parent.postMessage({type: "streamlit:buttonClick"}, "*");
40
+ </script>
41
+ """
42
+ html_container.html(javascript_code + js + "<div id='returned_value'></div>", height=300)
43
+
44
+ # Future-proofing to listen for messages from the JavaScript side
45
+ if "js_result" not in st.session_state:
46
+ st.session_state["js_result"] = ""
47
+
48
+ # Handle messages from JavaScript
49
+ st.markdown(
50
+ """
51
+ <script>
52
+ window.addEventListener('message', (event) => {
53
+ if (event.data && event.data.isStreamlitMessage) {
54
+ const result = event.data.data;
55
+ // Reflect the result back to the Python script
56
+ window.parent.postMessage({type: 'streamlit:component_with_result', data: result}, '*');
57
+ }
58
+ });
59
+ </script>
60
+ """,
61
+ unsafe_allow_html=True,
62
+ )
63
+
64
+ # Python-side listener to write the result to the placeholder
65
+ try:
66
+ import streamlit.components.v1 as components
67
+ messages = components.html(
68
+ """
69
+ <script>
70
+ window.addEventListener('message', (event) => {
71
+ if (event.data.type === 'streamlit:component_with_result') {
72
+ window.parent.postMessage(event.data.data, '*');
73
+ }
74
+ });
75
+ </script>
76
+ """,
77
+ height=0,
78
+ )
79
+ if_messages_received = messages._deserialize()
80
+ if if_messages_received and if_messages_received != st.session_state["js_result"]:
81
+ st.session_state["js_result"] = if_messages_received
82
+ message_placeholder.markdown("_" + if_messages_received + "_")
83
+ except:
84
+ pass
85
+
86
+ st.session_state["js_result"]