Akshayram1 commited on
Commit
3959fd2
·
verified ·
1 Parent(s): 3277f8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -19
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- from diff_match_patch import diff_match_patch # Import from local file
3
  import re
4
 
5
  def main():
@@ -11,8 +11,6 @@ def main():
11
  st.session_state.old_text = ""
12
  if 'new_text' not in st.session_state:
13
  st.session_state.new_text = ""
14
- if 'diff_html' not in st.session_state:
15
- st.session_state.diff_html = ""
16
 
17
  # Create two columns for text areas
18
  col1, col2 = st.columns(2)
@@ -26,12 +24,23 @@ def main():
26
  )
27
 
28
  with col2:
29
- st.session_state.new_text = st.text_area(
30
- "New Text:",
31
- height=300,
32
- value=st.session_state.new_text,
33
- placeholder="Enter modified text..."
34
- )
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Button controls
37
  col_compare, col_clear, col_copy = st.columns([1, 1, 1])
@@ -44,33 +53,29 @@ def main():
44
 
45
  html = []
46
  for op, text in diffs:
47
- text = text.replace("\n", "<br>") # Preserve newlines in HTML
48
  if op == 1: # Insertion
49
  html.append(f'<span style="background-color: #c8e6c9; border-radius: 4px; padding: 2px 4px;">{text}</span>')
50
  elif op == -1: # Deletion
51
  html.append(f'<span style="background-color: #ffcdd2; text-decoration: line-through; border-radius: 4px; padding: 2px 4px;">{text}</span>')
52
- else: # No change
53
  html.append(text)
54
 
55
  st.session_state.diff_html = "".join(html)
 
 
56
 
57
  with col_clear:
58
  if st.button("Clear"):
59
  st.session_state.old_text = ""
60
  st.session_state.new_text = ""
61
- st.session_state.diff_html = ""
62
  st.rerun()
63
 
64
- # Display comparison results
65
- if st.session_state.diff_html:
66
- st.markdown("### Comparison Result:")
67
- st.markdown(st.session_state.diff_html, unsafe_allow_html=True)
68
-
69
  # Copy functionality
70
  with col_copy:
71
  if st.button("Copy Result"):
72
- if st.session_state.diff_html:
73
- # Remove HTML tags and convert <br> to newlines
74
  clean_text = re.sub(r'<br>', '\n', st.session_state.diff_html)
75
  clean_text = re.sub(r'<[^>]+>', '', clean_text)
76
 
 
1
  import streamlit as st
2
+ from diff_match_patch import diff_match_patch
3
  import re
4
 
5
  def main():
 
11
  st.session_state.old_text = ""
12
  if 'new_text' not in st.session_state:
13
  st.session_state.new_text = ""
 
 
14
 
15
  # Create two columns for text areas
16
  col1, col2 = st.columns(2)
 
24
  )
25
 
26
  with col2:
27
+ # Display diffs directly in the New Text area
28
+ new_text_display = st.empty()
29
+
30
+ # Show either editable text area or diff result
31
+ if 'show_diff' not in st.session_state or not st.session_state.show_diff:
32
+ st.session_state.new_text = new_text_display.text_area(
33
+ "New Text:",
34
+ height=300,
35
+ value=st.session_state.new_text,
36
+ placeholder="Enter modified text...",
37
+ key="new_text_editable"
38
+ )
39
+ else:
40
+ new_text_display.markdown(
41
+ st.session_state.diff_html,
42
+ unsafe_allow_html=True
43
+ )
44
 
45
  # Button controls
46
  col_compare, col_clear, col_copy = st.columns([1, 1, 1])
 
53
 
54
  html = []
55
  for op, text in diffs:
56
+ text = text.replace("\n", "<br>")
57
  if op == 1: # Insertion
58
  html.append(f'<span style="background-color: #c8e6c9; border-radius: 4px; padding: 2px 4px;">{text}</span>')
59
  elif op == -1: # Deletion
60
  html.append(f'<span style="background-color: #ffcdd2; text-decoration: line-through; border-radius: 4px; padding: 2px 4px;">{text}</span>')
61
+ else:
62
  html.append(text)
63
 
64
  st.session_state.diff_html = "".join(html)
65
+ st.session_state.show_diff = True
66
+ st.rerun()
67
 
68
  with col_clear:
69
  if st.button("Clear"):
70
  st.session_state.old_text = ""
71
  st.session_state.new_text = ""
72
+ st.session_state.show_diff = False
73
  st.rerun()
74
 
 
 
 
 
 
75
  # Copy functionality
76
  with col_copy:
77
  if st.button("Copy Result"):
78
+ if 'diff_html' in st.session_state:
 
79
  clean_text = re.sub(r'<br>', '\n', st.session_state.diff_html)
80
  clean_text = re.sub(r'<[^>]+>', '', clean_text)
81