sgerbhctim
sgerbhctim

Reputation: 3640

How to output auto-generating HTML in a text variable and return it?

I have built a function that basically prints the html I need in order to send out a formatted email with report and data information.

def create_html_security_ldw(df1, df2):

    date_time = time.strftime('%b %d %Y')

    print('<html><body>')
    print('<img src=\"xyz.png\" style=\"display: block; margin: 40px auto; width: 200px; height: auto;\">')
    print('<h1 style=\"text-align: center;\">Security Risk LDW - ' + date_time + '</h1>')
    print('<h2 style=\"text-align: center;\">Top Unsettled by Security</h2>')
    print('<div style=\"position: relative; margin: auto; width: 60%;\"><table id=\"t01\"><tr><th>Security</th><th>Ticker</th><th>Yesterday</th><th>Currency</th></tr>')

    for index, row in df1.iterrows():
        print('<tr><td><b>' + row['Security'] + '</b></td>')
        print('<td>' + row['Ticker'] + '</td>')
        print('<td>' + str(row['*Yesterday*']) + '</td>')
        print('<td>' + row['Currency'] + '</td></tr>')

    print('</table><br><br>')

    print('<h2 style=\"text-align: center;\">Top Unsettled by Currency</h2>')
    print('<div style=\"position: relative; margin: auto; width: 60%;\"><table id=\"t01\"><tr><th>Currency</th><th>Yesterday</th><th>Percentage</th></tr>')


    for index, row in df2.iterrows():
        print('<tr><td><b>' + row['Currency'] + '</b></td>')
        print('<td>' + str(row['*Yesterday*']) + '</td>')
        print('<td>' + str(row['Percentage']) + '</td></tr>')

    print('</table>')
    print('</div><br><br><br></body></html>')

Is there any possible way that I can take this and store it in a variable, that in turn returns to a different function that I call by saying this:

send_outlook_email(create_html_security_ldw(big_security, sums_sort))

The outputted data is totally correct if I copy and paste it in a .html file. But I want it to self-generate. I tried running it this way, but obviously it is not going to work because I am not returning anything. Any suggestions?

Upvotes: 0

Views: 39

Answers (2)

Mark
Mark

Reputation: 92461

There are all manner of string processing tools in Python, so yes, you can definitely do this.

The obvious, but possible tedious way, it to just replace all your print statements with concatenations:

s = '<html><body>'
s += '<img src=\"xyz.png\" style=\"display: block; margin: 40px auto; width: 200px; height: auto;\">\n'

for index in ["Hello", "there", "buddy"]:
    s += '<tr><td><b>' + index + '</b></td>\n'

return s

Beyond that Python has all sorts of template and format possibilities that will let you pass variables into the strings to be formated. One example:

from string import Template

t = Template("<tr><td><b>$word</b></td>\n")
for index in ["Hello", "there", "buddy"]:
    s += t.substitute(word=index)

print(s)

More here: https://docs.python.org/2.4/lib/node109.html

Beyond that, there are full-fledged packages made for templating HTML such as Jinja (http://jinja.pocoo.org)

and a lot more here: https://wiki.python.org/moin/Templating

Upvotes: 1

Salmaan P
Salmaan P

Reputation: 857

Yes, you can create a variable like:

html = """<html>
<body> {{var1}}
</body>
</html>"""

this would be your template and you can pass variable to this template using:

new_html = html.format( var1="hello")

So you can pass any kind of variable you want in the template.

Upvotes: 0

Related Questions