Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|