Skip to content

Commit efb715c

Browse files
committed
add diff view options for streamlit
1 parent 275d27e commit efb715c

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# uv add deepdiff --active
2+
# uv run --active streamlit run .\app\streamlit\deepdiff_json.py
3+
import streamlit as st
4+
from deepdiff import DeepDiff
5+
6+
master_index = 0
7+
8+
jsons = [{"name": "Alice", "age": 25}, {"name": "Bob", "age": 26}, {"name": "Alice", "age": 27}]
9+
cols = st.columns(len(jsons))
10+
11+
for i, col in enumerate(cols):
12+
_json = jsons[i]
13+
with col:
14+
st.subheader(_json['name'])
15+
if st.button(f"按钮 {i + 1}", key=f"btn_{i}"):
16+
st.write(f"你点击了按钮 {i + 1}")
17+
if i == master_index:
18+
st.json(_json)
19+
else:
20+
st.json(DeepDiff(jsons[master_index], _json, verbose_level=2))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# uvx streamlit run ./app/streamlit/<this>.py
2+
import streamlit as st
3+
import difflib
4+
5+
text1 = st.text_area("原始文本")
6+
text2 = st.text_area("修改后文本")
7+
8+
diff_lines = difflib.unified_diff(
9+
text1.splitlines(), text2.splitlines(),
10+
fromfile='原始', tofile='修改后', lineterm=''
11+
)
12+
# git bash style
13+
st.markdown("```diff\n" + "\n".join(diff_lines) + "\n```")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import streamlit as st
2+
import difflib
3+
4+
text1 = st.text_area("原始文本", "This is the original text.\nIt has two lines.")
5+
text2 = st.text_area("修改后文本", "This is the modified text.\nIt has three lines.")
6+
7+
diff = difflib.HtmlDiff().make_table(
8+
text1.splitlines(), text2.splitlines(),
9+
fromdesc='原始', todesc='修改后',
10+
context=True, numlines=2
11+
)
12+
13+
styled_html = f"""
14+
<style>
15+
table.diff {{ background-color: white; color: black; font-family: monospace; }}
16+
td.diff_header {{ background-color: #e0e0e0; }}
17+
td.diff_next {{ background-color: #f0f0f0; }}
18+
td.diff_add {{ background-color: #aaffaa; }}
19+
td.diff_chg {{ background-color: #ffffaa; }}
20+
td.diff_sub {{ background-color: #ffaaaa; }}
21+
</style>
22+
{diff}
23+
"""
24+
# No comparison, just list out
25+
st.components.v1.html(styled_html, scrolling=True)

0 commit comments

Comments
 (0)