Skip to content

Commit 9ea89d1

Browse files
authored
fix double linebreak issue in jinja rendering #694 (#719)
* fix double linebreak issue in jinja rendering * make style * more simply replacing all `\n`
1 parent b15e610 commit 9ea89d1

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

promptsource/app.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,21 @@ def reset_template_state():
8282
"<style>" + HtmlFormatter(style="friendly").get_style_defs(".highlight") + "</style>", unsafe_allow_html=True
8383
)
8484

85-
WIDTH = 80
85+
WIDTH = 140
8686

8787

8888
def show_jinja(t, width=WIDTH):
89+
def replace_linebreaks(t):
90+
"""
91+
st.write does not handle double breaklines very well. When it encounters `\n\n`, it exit the curent <div> block.
92+
Explicitely replacing all `\n` with their html equivalent to bypass this issue.
93+
Also stripping the trailing `\n` first.
94+
"""
95+
return t.strip("\n").replace("\n", "<br/>")
96+
8997
wrap = textwrap.fill(t, width=width, replace_whitespace=False)
9098
out = highlight(wrap, DjangoLexer(), HtmlFormatter())
99+
out = replace_linebreaks(out)
91100
st.write(out, unsafe_allow_html=True)
92101

93102

0 commit comments

Comments
 (0)