Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
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()
|