python发送绘图图表(div)作为电子邮件内容 [英] python send plotly chart (div) as email content

查看:90
本文介绍了python发送绘图图表(div)作为电子邮件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 plotly 创建图表并返回 div.我想在 html 中输入 div 并作为电子邮件发送.

Im using plotly to create a chart and return div. I want to input div into the html and send as an email.

div = plotly.offline.plot(data, include_plotlyjs=False, output_type='div')

代码如下:

 mail_msg = f'''
            <p style="font-weight:bold;">\n\t <font="Times New Roman"><font size="2">This Month:</font></p>
            <p>{table_predict_html}</p>
            <p style="font-weight:bold;">\n\t <font="Times New Roman"><font size="2">Contrast:</font></p>
            <p>{tot_html}</p>
            <p style="font-weight:bold;">\n\t <font="Times New Roman"><font size="2">Revenue:</font></p>
            <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
            <p>{div}</p>
            '''
 msg.attach(MIMEText(mail_msg, 'html', 'utf-8'))

table_predict_html 和 tot_html 是 html 中的表格.电子邮件中显示了表格,但图表中全是空白.那么将 div 插入 html 的正确方法是什么?

table_predict_html and tot_html are tables in html. Tables displayed in the email but it's all blank for the chart. So what is the proper way of inserting the div to html?

谢谢

推荐答案

如果是静态图表,可以使用fig.write_image("images/fig1.png") 并将其作为图片发送.

If it is a static chart, you can save it as a picture file using fig.write_image("images/fig1.png") and send it as a picture.

如果是动态图表,我想如果你这样做可能会更容易<代码>div1 = div\.to_html()\.replace('<table border="1" class="dataframe">','<table class="table table-striped">')

If it is a dynamic chart, I think it probably will be easier if you do div1 = div\ .to_html()\ .replace('<table border="1" class="dataframe">','<table class="table table-striped">')

然后将其作为 HTML 发送.

and then send it as an HTML.

这篇关于python发送绘图图表(div)作为电子邮件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆