Skip to content

Commit

Permalink
Support native colored text in streamlit, closes #30
Browse files Browse the repository at this point in the history
Signed-off-by: houfu <[email protected]>
  • Loading branch information
houfu committed Oct 31, 2024
1 parent a3c3bcd commit 4dc53a3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions redlines/redlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def output_markdown(self) -> str:
|red | "The quick brown fox <span style='color:red;font-weight:700;text-decoration:line-through;'>jumps over </span><span style='color:red;font-weight:700;'>walks past </span>the lazy dog."|
|ghfm (GitHub Flavored Markdown)| 'The quick brown fox ~~jumps over ~~**walks past **the lazy dog.' |
|bbcode (BBCode) | 'The quick brown fox [s][color=red]jumps over [/color][/s][b][color=green]walks past [/color][/b]the lazy dog.' |
|streamlit | 'The quick brown fox ~~:red[jumps over ]~~ **:green[walks past ]** the lazy dog.' |
### Custom styling
Expand Down Expand Up @@ -166,9 +167,10 @@ def output_markdown(self) -> str:
Try this:
* If streamlit version is >= 1.16.0, consider the markdown style "streamlit"
* If streamlit version is < 1.16.0, consider the markdown style `ghfm`
* Enable parsing of HTML. In Streamlit, you need to set the `unsafe_allow_html` argument in `st.write` or
`st.markdown` to `True`.
* Use the markdown style `ghfm`
### Colab
Expand Down Expand Up @@ -238,7 +240,12 @@ def output_markdown(self) -> str:
elif style == "ghfm":
md_styles = {"ins": ("**", "**"), "del": ("~~", "~~")}
elif style == "bbcode":
md_styles = {"ins": ("[b][color=green]", "[/color][/b]"), "del": ("[s][color=red]", "[/color][/s]")}
md_styles = {
"ins": ("[b][color=green]", "[/color][/b]"),
"del": ("[s][color=red]", "[/color][/s]"),
}
elif style == "streamlit":
md_styles = {"ins": ("**:green[", "]** "), "del": ("~~:red[", "]~~ ")}

for tag, i1, i2, j1, j2 in self.opcodes:
if tag == "equal":
Expand Down
5 changes: 5 additions & 0 deletions tests/test_redline.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ def test_markdown_style():
test = Redlines(test_string_1, markdown_style="bbcode")
assert test.compare(test_string_2) == expected_md

# Test streamlit style
expected_md = "The quick brown fox ~~:red[jumps over ]~~ **:green[walks past ]** the lazy dog."
test = Redlines(test_string_1, markdown_style="streamlit")
assert test.compare(test_string_2) == expected_md


def test_paragraphs_handling():
test_string_1 = """
Expand Down

0 comments on commit 4dc53a3

Please sign in to comment.