1

Im trying to add an up and down arrow to a pandas data frame with to_html for an email report.

I'm using a lambda function to input an up and down arrow onto column values in my data frame, I know the image html works ok becasue I can put it in the body of the email and it works fine but when I use this function and the pandas formatter it outputs like in the below image (ps. i know the CID is different to whats in my function, I was just tesing something)

Any one have any idea why? Or anyone have a better way to do it?

pandas formatter error

call:

worst_20_accounts.to_html(index=False,formatters={'Last Run Rank Difference': lambda x: check_html_val_for_arrow(x)}))

function:

def check_html_val_for_arrow(x):
    try:
        if x > 0:
            return str(x) + ' <img src="cid:image7">'
        elif x < 0:
            return str(x) + ' <img src="cid:image8">'
        else:
            return str(x)
    except:
        return str(x)

1 Answer 1

1

escape=False

By default, the pandas.DataFrame.to_html method escapes any html in the dataframe's values.

my_img_snippet = (
    "<img src='https://www.pnglot.com/pngfile/detail/"
    "208-2086079_right-green-arrow-right-green-png-arrow.png'>"
)
df =  pd.DataFrame([[my_img_snippet]])

Then

from IPython.display import HTML
HTML(df.to_html(escape=False))

enter image description here


style

You can let the styler object handle the rendering

df.style

enter image description here

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

1 Comment

Thank you very much

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.