简体   繁体   中英

send pygal line chart in mail using mandrill api

from past week I am trying to create a simple line chart using pygal python library & send the resulting mail using mandrill api.

I placed my render result in "figure" tag as specified in pygal documentation.

I am able to send the email but it is completely blank.

following is my code.

    line_chart = pygal.Line()
    line_chart.title = 'Browser usage evolution (in %)'
    line_chart.x_labels = map(str, range(2002, 2013))
    line_chart.add('Firefox', [None, None, 0, 16.6,   25,   31, 36.4, 45.5, 46.3, 42.8, 37.1])
    line_chart.add('Chrome',  [None, None, None, None, None, None,    0,  3.9, 10.8, 23.8, 35.3])
    line_chart.add('IE',      [85.8, 84.6, 84.7, 74.5,   66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1])
    line_chart.add('Others',  [14.2, 15.4, 15.3,  8.9,    9, 10.4,  8.9,  5.8,  6.7,  6.8,  7.5])
    chart = line_chart.render()

    html = """<html>
            <head>
            <script type="text/javascript" src="http://kozea.github.com/pygal.js/javascripts/svg.jquery.js"></script>
            <script type="text/javascript" src="http://kozea.github.com/pygal.js/javascripts/pygal-tooltips.js"></script>
            </head>
           <body>
            <figure>"""
    html += chart
    html += "</figure></body></html>"

    try:
        mandrill_client = mandrill.Mandrill(MANDRILL_API_KEY)
        message = {}
        message['from_email'] = constants.REPORTS
        message['subject'] = 'Sending report of non reporting gps devices.'

        for email_id in send_to:
            to_dict = {}
            to_dict['email'] = str(email_id)
            to.append(to_dict)

        message['to'] = to
        message['preserve_recipients'] = True                                   
        message['html'] = str(html)

        result = mandrill_client.messages.send(message=message, async=False, ip_pool=constants.MANDRILL_MAIN_POOL, send_at=constants.SEND_AT) 
    except mandrill.Error, e:
        print 'A mandrill error occurred: %s - %s' % (e.__class__, e)

can anybody tell me what I am doing wrong here?

Email clients cannot render Javascript. The chart that you are attaching needs to be rendered by pygal.js, which fails since email cannot render Javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM