Using a pandas data frame inside of an HTML

Viewed 35

I'm trying to implement a pandas dataframe in to this HTML code.

So, instead of User in the Dear User row, I can take a specific value from the last row of my data frame. And instead of Users daily amount after the Reason: row I will get a specific reason from the last row of a data frame.

Sample Code:

        msg.attach(MIMEText(
            '''
            <html>
                <body>
                    <p><img src="cid:1" width="900" height="100"></p>
                    <b><h1 style="text-align: left;">Pay Attention!</h1></b>
                    <h3 style="text-align: left;">Dear User,</h3>
                    <p>There has been an anomaly in the following meaurement: </p>
                    <b><p>Users daily amount </p></b>
                    <h3>Reasons: </h3>
                    <p> - Increase in the amount of Information Request sent</p>
                    <p> - Unknown new rule firing </p>
                    <h3>Last 2 Weeks Activity</h3>
                    <p><img src="cid:0"></p>
                </body>
            </html>
            ''',
            'html', 'utf-8'))

Hope I'm clear, Thanks a lot!

1 Answers

It is not entirely clear what you want to achieve, but as far as I understood, you want to pass a value from pandas dataframe to your html code. In this case, you can simply concatenate the strings.

user = df.iloc[-1][0] //index of value
html =    '''
            <html>
                <body>
                    <p><img src="cid:1" width="900" height="100"></p>
                    <b><h1 style="text-align: left;">Pay Attention!</h1></b>
                    <h3 style="text-align: left;">Dear ''' + user + ''',</h3>
                    <p>There has been an anomaly in the following meaurement: </p>
                    <b><p>Users daily amount </p></b>
                    <h3>Reasons: </h3>
                    <p> - Increase in the amount of Information Request sent</p>
                    <p> - Unknown new rule firing </p>
                    <h3>Last 2 Weeks Activity</h3>
                    <p><img src="cid:0"></p>
                </body>
            </html>
            '''
msg.attach(MIMEText(html, 'html'))
Related