Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,23 +3,42 @@ from markdown import markdown
|
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import streamlit.components.v1 as components
|
| 5 |
|
| 6 |
-
# Set page config
|
| 7 |
-
st.set_page_config(page_title="Markdown Converter", layout="wide", initial_sidebar_state="expanded", page_icon="📝"
|
| 8 |
|
| 9 |
def md_to_text(md_text):
|
| 10 |
html = markdown(md_text)
|
| 11 |
soup = BeautifulSoup(html, features='html.parser')
|
| 12 |
return soup.get_text()
|
| 13 |
|
| 14 |
-
def copy_button(text_to_copy):
|
|
|
|
| 15 |
components.html(
|
| 16 |
f"""
|
| 17 |
-
<
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
Copy
|
| 20 |
</button>
|
| 21 |
""",
|
| 22 |
-
height=
|
| 23 |
)
|
| 24 |
|
| 25 |
st.title("Markdown to Plain Text Converter")
|
|
@@ -48,15 +67,14 @@ with col2:
|
|
| 48 |
|
| 49 |
# Add copy button
|
| 50 |
if st.session_state.converted_text:
|
| 51 |
-
copy_button(st.session_state.converted_text)
|
| 52 |
|
| 53 |
# Optional: Add preview toggle with copy button
|
| 54 |
with st.expander("Preview Original Markdown"):
|
| 55 |
if md_input:
|
| 56 |
st.markdown(md_input, unsafe_allow_html=True)
|
| 57 |
# Add copy button for the preview
|
| 58 |
-
copy_button(md_input)
|
| 59 |
-
st.caption("Copy Preview Content")
|
| 60 |
else:
|
| 61 |
st.write("No Markdown to preview")
|
| 62 |
|
|
@@ -67,4 +85,28 @@ st.sidebar.markdown("""
|
|
| 67 |
2. Click 'Convert'
|
| 68 |
3. Use the 'Copy' button in the right panel
|
| 69 |
4. Toggle the preview to see original formatting and copy if needed
|
| 70 |
-
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
import streamlit.components.v1 as components
|
| 5 |
|
| 6 |
+
# Set page config (without theme parameter)
|
| 7 |
+
st.set_page_config(page_title="Markdown Converter", layout="wide", initial_sidebar_state="expanded", page_icon="📝")
|
| 8 |
|
| 9 |
def md_to_text(md_text):
|
| 10 |
html = markdown(md_text)
|
| 11 |
soup = BeautifulSoup(html, features='html.parser')
|
| 12 |
return soup.get_text()
|
| 13 |
|
| 14 |
+
def copy_button(text_to_copy, button_id):
|
| 15 |
+
# More reliable HTML/JS implementation of copy button
|
| 16 |
components.html(
|
| 17 |
f"""
|
| 18 |
+
<script>
|
| 19 |
+
function copyText{button_id}() {{
|
| 20 |
+
const text = `{text_to_copy.replace('`', '\\`').replace("'", "\\'")}`;
|
| 21 |
+
navigator.clipboard.writeText(text).then(function() {{
|
| 22 |
+
console.log('Text copied to clipboard');
|
| 23 |
+
document.getElementById('copy-button-{button_id}').innerHTML = 'Copied!';
|
| 24 |
+
setTimeout(function() {{
|
| 25 |
+
document.getElementById('copy-button-{button_id}').innerHTML = 'Copy';
|
| 26 |
+
}}, 2000);
|
| 27 |
+
}})
|
| 28 |
+
.catch(function(err) {{
|
| 29 |
+
console.error('Failed to copy text: ', err);
|
| 30 |
+
}});
|
| 31 |
+
}}
|
| 32 |
+
</script>
|
| 33 |
+
<button id="copy-button-{button_id}" onclick="copyText{button_id}()"
|
| 34 |
+
style="background-color: #4CAF50; color: white; border: none;
|
| 35 |
+
padding: 5px 10px; text-align: center; text-decoration: none;
|
| 36 |
+
display: inline-block; font-size: 14px; margin: 4px 2px;
|
| 37 |
+
cursor: pointer; border-radius: 4px;">
|
| 38 |
Copy
|
| 39 |
</button>
|
| 40 |
""",
|
| 41 |
+
height=50,
|
| 42 |
)
|
| 43 |
|
| 44 |
st.title("Markdown to Plain Text Converter")
|
|
|
|
| 67 |
|
| 68 |
# Add copy button
|
| 69 |
if st.session_state.converted_text:
|
| 70 |
+
copy_button(st.session_state.converted_text, "output")
|
| 71 |
|
| 72 |
# Optional: Add preview toggle with copy button
|
| 73 |
with st.expander("Preview Original Markdown"):
|
| 74 |
if md_input:
|
| 75 |
st.markdown(md_input, unsafe_allow_html=True)
|
| 76 |
# Add copy button for the preview
|
| 77 |
+
copy_button(md_input, "preview")
|
|
|
|
| 78 |
else:
|
| 79 |
st.write("No Markdown to preview")
|
| 80 |
|
|
|
|
| 85 |
2. Click 'Convert'
|
| 86 |
3. Use the 'Copy' button in the right panel
|
| 87 |
4. Toggle the preview to see original formatting and copy if needed
|
| 88 |
+
""")
|
| 89 |
+
|
| 90 |
+
# Apply light theme using CSS
|
| 91 |
+
st.markdown("""
|
| 92 |
+
<style>
|
| 93 |
+
.stApp {
|
| 94 |
+
background-color: white;
|
| 95 |
+
color: #333333;
|
| 96 |
+
}
|
| 97 |
+
.stTextArea textarea {
|
| 98 |
+
background-color: #f9f9f9;
|
| 99 |
+
color: #333333;
|
| 100 |
+
}
|
| 101 |
+
.stButton button {
|
| 102 |
+
background-color: #4CAF50;
|
| 103 |
+
color: white;
|
| 104 |
+
}
|
| 105 |
+
.stExpander {
|
| 106 |
+
background-color: #f9f9f9;
|
| 107 |
+
}
|
| 108 |
+
.sidebar .sidebar-content {
|
| 109 |
+
background-color: #f0f0f0;
|
| 110 |
+
}
|
| 111 |
+
</style>
|
| 112 |
+
""", unsafe_allow_html=True)
|