A Maggi
A Maggi

Reputation: 31

How to add python code in a html email in python?

To print a variable I have used:

<html>
""" +str(new_oppotunity)+ """
</html>

How can I add a for loop to print items in a list in the html.

Upvotes: 1

Views: 61

Answers (2)

Jerald Macachor
Jerald Macachor

Reputation: 141

Looks like you need to dynamically create HTML with Python rather than insert python code in an HTML document and expect it to just work.

You can create a Python CGI script that imports the jinja library. It takes an HTML template with jinja2 syntax embedded in it and fills in the template with data.

Upvotes: 0

Andrew F
Andrew F

Reputation: 2950

You may want to look at the Jinja templating library. It has powerful tools that help simplify making HTML pages via Python.

A vanilla Python example that may do what you want is

list_of_items = ['banana', 'apple', 'orange']

html_str = '<html>\n<ul>'
for item in list_of_items:
    html_str += '<li>' + item + '</li>\n'
html_str += '</ul>\n</html>\n'

Upvotes: 1

Related Questions