3

I have a Python script that constructs a pandas DataFrame from API data, which I then convert to a pretty_html_table that will be the body of an email. In one of the rows, I have data containing an address. It is formatted as follows

Street Address: 123 Street Ave<br>City: City<br>State: ST<br>Postal: 12345<br>

The HTML line breaks are a part of the string and thus aren't encoded, so in the email, it is all in that one line as shown above. I would like to have each element be on its own line within the same row (essentially have those tags actually do what they're supposed to do)

from pretty_html_table import build_table
import pandas

table = pandas.DataFrame({
   'Label': ["Name", "Address", "Item", "Contact Email", "Date"],
   'Info': ["", "", "", "", ""]
})

table.loc[0, 'Info'] = foo
table.loc[1, 'Info'] = foo
table.loc[2, 'Info'] = foo
table.loc[3, 'Info'] = foo
table.loc[4, 'Info'] = foo

pretty_table = build_table(table, "green_dark")

I can just replace the tags with whitespaces and make the string a little neater, but I would prefer for it to be on separate lines without making 4 separate rows.

3
  • 1
    {Comment from furas copied from Staging Ground}: it prints <br> because pandas (using df.to_html() in this place in source code) converts to &lt;br&gt; and browser/email displays it as string <br>, and it doesn't treat it as HTML tag. You may test print( table.to_html() ) and you should see &lt;br&gt; instead of <br> in HTML Commented Oct 13 at 4:40
  • 2
    {Comment from furas copied from Staging Ground}: based on source code you can use build_table(..., escape=False) and it will not convert <br> to &lt;br&gt; Commented Oct 13 at 4:40
  • @Abra thanks for the tedious work of migrating these comments, there should be a feature for that. Commented Oct 13 at 8:11

2 Answers 2

2

pretty_html_table escapes HTML by default. After building the table, just unescape the <br> tags:

html_table = build_table(table, "green_dark")
html_table = html_table.replace("&lt;br&gt;", "<br>")

Now the <br> tags render as real line breaks in your email.

Sign up to request clarification or add additional context in comments.

Comments

1

Use build_table(..., escape=False).

escape controls whether html-sequences in data will be escaped or not. Escaping is obviously the default because of security. Read about Cross-Site-Scripting (XSS) before you think about enabling this in a web-context.

According to the source code of build_table this parameter gets passed to the escape-parameter of the underlying pandas .to_html call, which does the same thing, with the same default.

So use df.to_html(..., escape=True) if you are using pandas directly.

The gist of this answer was originally written by @furas as a staging-ground-comment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.