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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -82
app.py CHANGED
@@ -1,86 +1,19 @@
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"]
 
 
 
 
1
  import streamlit as st
2
+ import streamlit.components.v1 as components
3
+
4
+ # Define the JavaScript code
5
+ js_code = """
6
+ function myFunction() {
7
+ // Perform your desired operation here
8
+ var result = "This is the result from JavaScript";
9
+ return result;
10
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  """
12
 
13
+ # Create a custom component with the JavaScript code
14
+ mycomponent = components.declare_component("mycomponent", js_code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Add a button to trigger the JavaScript function
17
+ if st.button("Run JavaScript Function"):
18
+ result = mycomponent()
19
+ st.markdown(f"The result is: {result}")