awacke1 commited on
Commit
52738ee
·
verified ·
1 Parent(s): 7f93673

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -58
app.py CHANGED
@@ -1,67 +1,23 @@
1
  import streamlit as st
2
- from streamlit.components.v1 import html
3
 
4
  def main():
5
  st.title("Copy Text Demo")
6
- st.write("This demo shows how to copy text from an HTML textarea into Streamlit.")
7
 
8
- # HTML component with textarea and copy button
9
- html_code = """
10
- <textarea id="myTextarea" rows="5" style="width: 100%; padding: 10px; margin: 10px 0;">Here is some sample text that you can copy.
11
- Try modifying this text and clicking the Copy button below!</textarea>
 
 
12
 
13
- <button onclick="copyToStreamlit()" style="padding: 8px 16px; background-color: #ff4b4b; color: white; border: none; border-radius: 4px; cursor: pointer;">
14
- Copy to Streamlit
15
- </button>
16
-
17
- <script>
18
- function copyToStreamlit() {
19
- const text = document.getElementById('myTextarea').value;
20
- window.parent.postMessage({
21
- type: 'streamlit:message',
22
- data: {
23
- type: 'copyText',
24
- text: text
25
- }
26
- }, '*');
27
- }
28
-
29
- // Listen for messages from Streamlit
30
- window.addEventListener('message', function(event) {
31
- if (event.data.type === 'streamlit:render') {
32
- // Component has been re-rendered
33
- console.log('Component rendered');
34
- }
35
- });
36
- </script>
37
- """
38
-
39
- # Create a placeholder for the copied text
40
- if 'copied_text' not in st.session_state:
41
- st.session_state.copied_text = None
42
-
43
- # Render HTML component
44
- html(html_code, height=200)
45
-
46
- # JavaScript message handler
47
- st.markdown("""
48
- <script>
49
- window.addEventListener('message', function(event) {
50
- if (event.data.type === 'copyText') {
51
- const text = event.data.text;
52
- window.streamlitMessageListener.handleMessage({
53
- type: 'streamlit:setComponentValue',
54
- value: text
55
- });
56
- }
57
- });
58
- </script>
59
- """, unsafe_allow_html=True)
60
-
61
- # Display copied text in a code block if available
62
- if st.session_state.copied_text:
63
- st.subheader("Copied Text:")
64
- st.code(st.session_state.copied_text, language='text')
65
 
66
  if __name__ == "__main__":
67
  main()
 
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()