Akshayram1 commited on
Commit
a56bdae
·
verified ·
1 Parent(s): 810b0ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -27
app.py CHANGED
@@ -3,8 +3,13 @@ from markdown import markdown
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)
@@ -13,26 +18,25 @@ def md_to_text(md_text):
13
 
14
  def copy_button(text_to_copy, button_id):
15
  # Creating a safer version of the text for JavaScript
16
- # This avoids the backslash issue in f-strings
17
  escaped_text = text_to_copy.replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r')
18
 
19
- html_code = """
20
  <script>
21
- function copyText""" + button_id + """() {
22
- const text = \"""" + escaped_text + """\";
23
- navigator.clipboard.writeText(text).then(function() {
24
  console.log('Text copied to clipboard');
25
- document.getElementById('copy-button-""" + button_id + """').innerHTML = 'Copied!';
26
- setTimeout(function() {
27
- document.getElementById('copy-button-""" + button_id + """').innerHTML = 'Copy';
28
- }, 2000);
29
- })
30
- .catch(function(err) {
31
  console.error('Failed to copy text: ', err);
32
- });
33
- }
34
  </script>
35
- <button id="copy-button-""" + button_id + """" onclick="copyText""" + button_id + """()"
36
  style="background-color: #4CAF50; color: white; border: none;
37
  padding: 5px 10px; text-align: center; text-decoration: none;
38
  display: inline-block; font-size: 14px; margin: 4px 2px;
@@ -50,8 +54,11 @@ st.write("Paste your Markdown content below and convert it to plain text!")
50
  col1, col2 = st.columns(2)
51
 
52
  with col1:
53
- md_input = st.text_area("Markdown Input", height=300,
54
- placeholder="Paste your Markdown here...")
 
 
 
55
 
56
  # Convert button
57
  if st.button("Convert"):
@@ -61,22 +68,24 @@ if st.button("Convert"):
61
  # Display results
62
  with col2:
63
  if 'converted_text' in st.session_state:
64
- # Removed the "Plain Text Output" label
65
- st.text_area("",
66
- value=st.session_state.converted_text,
67
- height=300,
68
- key="output_area")
 
69
 
70
  # Add copy button
71
  if st.session_state.converted_text:
72
  copy_button(st.session_state.converted_text, "output")
73
 
74
- # Optional: Add preview toggle with copy button
75
  with st.expander("Preview Original Markdown"):
76
  if md_input:
77
  st.markdown(md_input, unsafe_allow_html=True)
78
- # Add copy button for the preview
79
- copy_button(md_input, "preview")
 
80
  else:
81
  st.write("No Markdown to preview")
82
 
@@ -86,7 +95,8 @@ st.sidebar.markdown("""
86
  1. Paste your Markdown in the left panel
87
  2. Click 'Convert'
88
  3. Use the 'Copy' button in the right panel
89
- 4. Toggle the preview to see original formatting and copy if needed
 
90
  """)
91
 
92
  # Apply light theme using CSS
 
3
  from bs4 import BeautifulSoup
4
  import streamlit.components.v1 as components
5
 
6
+ # Set page config
7
+ st.set_page_config(
8
+ page_title="Markdown Converter",
9
+ layout="wide",
10
+ initial_sidebar_state="expanded",
11
+ page_icon="📝"
12
+ )
13
 
14
  def md_to_text(md_text):
15
  html = markdown(md_text)
 
18
 
19
  def copy_button(text_to_copy, button_id):
20
  # Creating a safer version of the text for JavaScript
 
21
  escaped_text = text_to_copy.replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r')
22
 
23
+ html_code = f"""
24
  <script>
25
+ function copyText{button_id}() {{
26
+ const text = "{escaped_text}";
27
+ navigator.clipboard.writeText(text).then(function() {{
28
  console.log('Text copied to clipboard');
29
+ document.getElementById('copy-button-{button_id}').innerHTML = 'Copied!';
30
+ setTimeout(function() {{
31
+ document.getElementById('copy-button-{button_id}').innerHTML = 'Copy';
32
+ }}, 2000);
33
+ }})
34
+ .catch(function(err) {{
35
  console.error('Failed to copy text: ', err);
36
+ }});
37
+ }}
38
  </script>
39
+ <button id="copy-button-{button_id}" onclick="copyText{button_id}()"
40
  style="background-color: #4CAF50; color: white; border: none;
41
  padding: 5px 10px; text-align: center; text-decoration: none;
42
  display: inline-block; font-size: 14px; margin: 4px 2px;
 
54
  col1, col2 = st.columns(2)
55
 
56
  with col1:
57
+ md_input = st.text_area(
58
+ "Markdown Input",
59
+ height=300,
60
+ placeholder="Paste your Markdown here..."
61
+ )
62
 
63
  # Convert button
64
  if st.button("Convert"):
 
68
  # Display results
69
  with col2:
70
  if 'converted_text' in st.session_state:
71
+ st.text_area(
72
+ "",
73
+ value=st.session_state.converted_text,
74
+ height=300,
75
+ key="output_area"
76
+ )
77
 
78
  # Add copy button
79
  if st.session_state.converted_text:
80
  copy_button(st.session_state.converted_text, "output")
81
 
82
+ # Preview section with proper copy functionality
83
  with st.expander("Preview Original Markdown"):
84
  if md_input:
85
  st.markdown(md_input, unsafe_allow_html=True)
86
+ # Convert to plain text for the copy button
87
+ preview_plain_text = md_to_text(md_input)
88
+ copy_button(preview_plain_text, "preview")
89
  else:
90
  st.write("No Markdown to preview")
91
 
 
95
  1. Paste your Markdown in the left panel
96
  2. Click 'Convert'
97
  3. Use the 'Copy' button in the right panel
98
+ 4. Toggle the preview to see original formatting
99
+ 5. The preview's copy button copies the plain text version
100
  """)
101
 
102
  # Apply light theme using CSS