awacke1 commited on
Commit
3f75756
·
verified ·
1 Parent(s): 52738ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -16
app.py CHANGED
@@ -1,23 +1,61 @@
1
  import streamlit as st
 
 
2
 
3
  def main():
4
- st.title("Copy Text Demo")
5
 
6
- # Text input area
7
- text_input = st.text_area(
8
- "Enter your text here:",
9
- value="Here is some sample text that you can copy.",
10
- height=200
11
- )
12
-
13
- # Button to trigger copy
14
- if st.button("Copy to Code Block"):
15
- st.subheader("Copied Text in Code Block:")
16
- st.code(text_input, language="text")
17
-
18
- # Optional: Also show raw text
19
- st.subheader("Raw Text:")
20
- st.write(text_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  if __name__ == "__main__":
23
  main()
 
1
  import streamlit as st
2
+ from streamlit.components.v1 import html
3
+ import time
4
 
5
  def main():
6
+ st.title("HTML to Streamlit Sync Demo")
7
 
8
+ # Create a session state variable for the text
9
+ if 'text_value' not in st.session_state:
10
+ st.session_state.text_value = "Initial text..."
11
+
12
+ # HTML component with a textarea
13
+ html_code = """
14
+ <textarea id="html-textarea" style="width: 100%; height: 150px; padding: 10px; margin: 10px 0;">
15
+ This is text in the HTML textarea.
16
+ Try editing this text!
17
+ </textarea>
18
+
19
+ <script>
20
+ // Function to get textarea content
21
+ function getTextareaContent() {
22
+ let content = document.getElementById('html-textarea').value;
23
+ // Send content to Streamlit
24
+ window.parent.postMessage({
25
+ type: 'streamlit:setComponentValue',
26
+ value: content
27
+ }, '*');
28
+ }
29
+
30
+ // Set up timer to check for changes
31
+ setInterval(getTextareaContent, 1000); // Check every second
32
+ </script>
33
+ """
34
+
35
+ # Column layout for side-by-side comparison
36
+ col1, col2 = st.columns(2)
37
+
38
+ with col1:
39
+ st.subheader("HTML Textarea")
40
+ # Render the HTML component
41
+ html(html_code, height=200)
42
+
43
+ with col2:
44
+ st.subheader("Streamlit Textarea")
45
+ # Streamlit textarea that shows the synced content
46
+ synced_text = st.text_area(
47
+ "Synced content (updates every second):",
48
+ value=st.session_state.text_value,
49
+ height=150,
50
+ key="synced_textarea"
51
+ )
52
+
53
+ # Show the current value for debugging
54
+ st.subheader("Current Value in Session State:")
55
+ st.code(st.session_state.text_value)
56
+
57
+ # Add a note about the sync behavior
58
+ st.info("The content from the HTML textarea will sync to the Streamlit textarea every second. Try typing in the HTML textarea and watch the Streamlit textarea update!")
59
 
60
  if __name__ == "__main__":
61
  main()