javascript - 使用 python 电子邮件或其他模块在电子邮件中发送 Bokeh 图

我有一个包含 Bokeh 图的完整 html 文件。现在我正在尝试使用 python 电子邮件模块发送该 html。我只是使用此处给出的第三个示例:https://docs.python.org/2.7/library/email-examples.html (如何创建 HTML 消息......)

但这似乎只是向我发送了一封空白电子邮件。

我还尝试了其他一些包含嵌入式 javascript 的 html。这似乎很好(禁用 JS,我不关心)。

关于可能发生的事情以及我可以改变什么的任何线索?

这是 html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Bokeh Scatter Plots</title>

        <link rel="stylesheet" href="http://cdn.bokeh.org/bokeh/release/bokeh-0.9.0.min.css" type="text/css" />
        <script type="text/javascript" src="http://cdn.bokeh.org/bokeh/release/bokeh-0.9.0.min.js"></script>

<script type="text/javascript">
    Bokeh.$(function() {
        var all_models = [{"attributes": {"line_color": {"value": "white"}, "fill_color": {"value": "#5ab738"}, "tags": [], "doc": null, "fill_alpha": {"value": 0.7}, "height": {"units": "data", "field": "z"}, "width": {"units": "data", "field": "width_cat"}, "y": {"field": "midz"}, "x": {"field": "catz"}, "id": "20b3f29b-629c-4d0d-a8ef-9bc05e5b178a"}, "type": "Rect", "id": "20b3f29b-629c-4d0d-a8ef-9bc05e5b178a"}, {"attributes": {"nonselection_glyph": null, "data_source": {"type": "ColumnDataSource", "id": "23913a89-b55a-4191-a5c3-3195acc7e1f4"}, "tags": [], "doc": null, "selection_glyph": null, "id": "5eda121f-edca-48cf-aed6-1e109b54b2ff", "glyph": {"type": "Rect", "id": "20b3f29b-629c-4d0d-a8ef-9bc05e5b178a"}}, "type": "GlyphRenderer", "id": "5eda121f-edca-48cf-aed6-1e109b54b2ff"}, {"attributes": {"geometries": [], "tags": [], "doc": null, "id": "c43afbcc-a846-4f33-a482-039a0fd206e8"}, "type": "ToolEvents", "id": "c43afbcc-a846-4f33-a482-039a0fd206e8"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "tags": [], "doc": null, "major_label_orientation": 0.7853981633974483, "axis_label": "categories", "formatter": {"type": "CategoricalTickFormatter", "id": "f27df891-c727-4511-af15-049332957dda"}, "ticker": {"type": "CategoricalTicker", "id": "9d14f049-833c-4895-bb18-a13fd30163a3"}, "id": "50005101-7498-4430-8b3e-fece52250277"}, "type": "CategoricalAxis", "id": "50005101-7498-4430-8b3e-fece52250277"}, {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b", "attributes": {"x_range": {"type": "FactorRange", "id": "46a7ffcd-19f4-4d8a-ae50-e01aa946b82e"}, "right": [], "above": [], "tags": [], "tools": [{"type": "PanTool", "id": "d6948ffa-b6f5-4379-a967-82dca90488a0"}, {"type": "WheelZoomTool", "id": "03df0ff2-3de4-4341-aecc-1bc593ebb8f2"}, {"type": "BoxZoomTool", "id": "3b6ab084-b09a-4131-809e-282113f78357"}, {"type": "PreviewSaveTool", "id": "8264e0e1-e9b3-44cd-aed7-02d01251240a"}, {"type": "ResizeTool", "id": "d3c48641-01cb-467c-91ee-3a0c6085d201"}, {"type": "ResetTool", "id": "3f0c4f54-5e2b-4253-b7e4-9bb9a7deabab"}, {"type": "HelpTool", "id": "3a1fb46c-8124-4374-83a4-2dd5bf4f6860"}], "title": "Bar example p", "renderers": [{"type": "CategoricalAxis", "id": "50005101-7498-4430-8b3e-fece52250277"}, {"type": "LinearAxis", "id": "42be7469-495b-4e8a-8021-a9a3191cecf0"}, {"type": "Grid", "id": "42bb2980-8afe-426d-9894-767dd3e4caad"}, {"type": "GlyphRenderer", "id": "8eb8810b-0e44-4851-89df-79debf7dbe2a"}, {"type": "GlyphRenderer", "id": "5eda121f-edca-48cf-aed6-1e109b54b2ff"}], "plot_width": 400, "extra_y_ranges": {}, "extra_x_ranges": {}, "tool_events": {"type": "ToolEvents", "id": "c43afbcc-a846-4f33-a482-039a0fd206e8"}, "plot_height": 400, "doc": null, "id": "36f2771e-837b-4b64-b541-523ef126784b", "y_range": {"type": "Range1d", "id": "90174572-530c-4de3-8906-e80952108b06"}, "below": [{"type": "CategoricalAxis", "id": "50005101-7498-4430-8b3e-fece52250277"}], "left": [{"type": "LinearAxis", "id": "42be7469-495b-4e8a-8021-a9a3191cecf0"}]}}, {"attributes": {"doc": null, "id": "9d14f049-833c-4895-bb18-a13fd30163a3", "tags": []}, "type": "CategoricalTicker", "id": "9d14f049-833c-4895-bb18-a13fd30163a3"}, {"attributes": {"doc": null, "id": "f27df891-c727-4511-af15-049332957dda", "tags": []}, "type": "CategoricalTickFormatter", "id": "f27df891-c727-4511-af15-049332957dda"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "tags": [], "doc": null, "axis_label": "values", "formatter": {"type": "BasicTickFormatter", "id": "a5efb989-af95-4129-983a-b867fe96eddf"}, "ticker": {"type": "BasicTicker", "id": "57a0ff59-725c-4d01-920a-2a3f8c65e6c4"}, "id": "42be7469-495b-4e8a-8021-a9a3191cecf0"}, "type": "LinearAxis", "id": "42be7469-495b-4e8a-8021-a9a3191cecf0"}, {"attributes": {"tags": [], "doc": null, "mantissas": [2, 5, 10], "id": "57a0ff59-725c-4d01-920a-2a3f8c65e6c4"}, "type": "BasicTicker", "id": "57a0ff59-725c-4d01-920a-2a3f8c65e6c4"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "tags": [], "doc": null, "dimension": 1, "ticker": {"type": "BasicTicker", "id": "57a0ff59-725c-4d01-920a-2a3f8c65e6c4"}, "id": "42bb2980-8afe-426d-9894-767dd3e4caad"}, "type": "Grid", "id": "42bb2980-8afe-426d-9894-767dd3e4caad"}, {"attributes": {"doc": null, "id": "a5efb989-af95-4129-983a-b867fe96eddf", "tags": []}, "type": "BasicTickFormatter", "id": "a5efb989-af95-4129-983a-b867fe96eddf"}, {"attributes": {"column_names": ["width_cat", "stackedz", "stackedy", "cat", "width", "zero", "midz", "midy", "y", "caty", "catz", "z"], "tags": [], "doc": null, "selected": {"2d": {"indices": []}, "1d": {"indices": []}, "0d": {"indices": [], "flag": false}}, "callback": null, "data": {"width_cat": [0.2, 0.2, 0.2, 0.2, 0.2], "stackedz": [6.5, 9.5, 8.0, 6.0, 6.0], "stackedy": [3.0, 3.5, 1.0, 2.0, 2.5], "cat": ["C1", "C2", "C3", "D1", "D2"], "width": [0.8, 0.8, 0.8, 0.8, 0.8], "zero": [7.0, 12.0, 14.0, 8.0, 7.0], "midz": [0.5, 2.5, 6.0, 2.0, 1.0], "midy": [3.0, 3.5, 1.0, 2.0, 2.5], "y": [6, 7, 2, 4, 5], "caty": ["C1:0.333333333333", "C2:0.333333333333", "C3:0.333333333333", "D1:0.333333333333", "D2:0.333333333333"], "catz": ["C1:0.666666666667", "C2:0.666666666667", "C3:0.666666666667", "D1:0.666666666667", "D2:0.666666666667"], "z": [1, 5, 12, 4, 2]}, "id": "23913a89-b55a-4191-a5c3-3195acc7e1f4"}, "type": "ColumnDataSource", "id": "23913a89-b55a-4191-a5c3-3195acc7e1f4"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "dimensions": ["width", "height"], "tags": [], "doc": null, "id": "d6948ffa-b6f5-4379-a967-82dca90488a0"}, "type": "PanTool", "id": "d6948ffa-b6f5-4379-a967-82dca90488a0"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "dimensions": ["width", "height"], "tags": [], "doc": null, "id": "03df0ff2-3de4-4341-aecc-1bc593ebb8f2"}, "type": "WheelZoomTool", "id": "03df0ff2-3de4-4341-aecc-1bc593ebb8f2"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "dimensions": ["width", "height"], "tags": [], "doc": null, "id": "3b6ab084-b09a-4131-809e-282113f78357"}, "type": "BoxZoomTool", "id": "3b6ab084-b09a-4131-809e-282113f78357"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "tags": [], "doc": null, "id": "8264e0e1-e9b3-44cd-aed7-02d01251240a"}, "type": "PreviewSaveTool", "id": "8264e0e1-e9b3-44cd-aed7-02d01251240a"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "tags": [], "doc": null, "id": "d3c48641-01cb-467c-91ee-3a0c6085d201"}, "type": "ResizeTool", "id": "d3c48641-01cb-467c-91ee-3a0c6085d201"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "tags": [], "doc": null, "id": "3f0c4f54-5e2b-4253-b7e4-9bb9a7deabab"}, "type": "ResetTool", "id": "3f0c4f54-5e2b-4253-b7e4-9bb9a7deabab"}, {"attributes": {"plot": {"subtype": "Chart", "type": "Plot", "id": "36f2771e-837b-4b64-b541-523ef126784b"}, "tags": [], "doc": null, "id": "3a1fb46c-8124-4374-83a4-2dd5bf4f6860"}, "type": "HelpTool", "id": "3a1fb46c-8124-4374-83a4-2dd5bf4f6860"}, {"attributes": {"callback": null, "factors": ["C1", "C2", "C3", "D1", "D2"], "doc": null, "tags": [], "id": "46a7ffcd-19f4-4d8a-ae50-e01aa946b82e"}, "type": "FactorRange", "id": "46a7ffcd-19f4-4d8a-ae50-e01aa946b82e"}, {"attributes": {"end": 13.200000000000001, "callback": null, "doc": null, "tags": [], "start": 0, "id": "90174572-530c-4de3-8906-e80952108b06"}, "type": "Range1d", "id": "90174572-530c-4de3-8906-e80952108b06"}, {"attributes": {"line_color": {"value": "white"}, "fill_color": {"value": "#f22c40"}, "tags": [], "doc": null, "fill_alpha": {"value": 0.7}, "height": {"units": "data", "field": "y"}, "width": {"units": "data", "field": "width_cat"}, "y": {"field": "midy"}, "x": {"field": "caty"}, "id": "43271b33-7e9c-4120-a177-4d0f1c233de1"}, "type": "Rect", "id": "43271b33-7e9c-4120-a177-4d0f1c233de1"}, {"attributes": {"nonselection_glyph": null, "data_source": {"type": "ColumnDataSource", "id": "23913a89-b55a-4191-a5c3-3195acc7e1f4"}, "tags": [], "doc": null, "selection_glyph": null, "id": "8eb8810b-0e44-4851-89df-79debf7dbe2a", "glyph": {"type": "Rect", "id": "43271b33-7e9c-4120-a177-4d0f1c233de1"}}, "type": "GlyphRenderer", "id": "8eb8810b-0e44-4851-89df-79debf7dbe2a"}];
        Bokeh.load_models(all_models);
        var plots = [{'modeltype': 'Plot', 'elementid': '#806c01c5-0a0d-4d12-9df9-5de64399e878', 'modelid': '36f2771e-837b-4b64-b541-523ef126784b'}];
        for (idx in plots) {
            var plot = plots[idx];
            var model = Bokeh.Collections(plot.modeltype).get(plot.modelid);
            Bokeh.logger.info('Realizing plot:')
            Bokeh.logger.info(' - modeltype: ' + plot.modeltype);
            Bokeh.logger.info(' - modelid: ' + plot.modelid);
            Bokeh.logger.info(' - elementid: ' + plot.elementid);
            var view = new model.default_view({
                model: model,
                el: plot.elementid
            });
            Bokeh.index[plot.modelid] = view;
        }
    });
</script>
    </head>
    <body>
<div class="plotdiv" id="806c01c5-0a0d-4d12-9df9-5de64399e878"></div>    </body>
</html>

这是 python 脚本:

#!/usr/bin/env python

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

me = 'abc@mac.com'
you = 'abc@abc.com'

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link JavaScript big"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
#text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttps://www.python.org"

# example below
'''
html = """\ 
<!DOCTYPE html>
<html>
<body>

<h1>My First JavaScript</h1>

<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>

<p id="demo"></p>

</body>
</html>
"""
'''

# html goes here
html = """\

"""


# Record the MIME types of both parts - text/plain and text/html.
#part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
#msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

bokeh html 包含在注释“# html goes here”下方的三重引号中(就像上面的示例一样)

最佳答案

如评论中所述,电子邮件不呈现 HTML。

我想到了三个选项:将绘图转换为图像并发送,将绘图放到服务器并将指向绘图的链接放入电子邮件或将 HTML 作为附件发送。我认为您可能无法单独在 HTML 主体上按原样显示情节。

将情节作为附件的一种简单方法是通过 Red Mail 进行:

from redmail import EmailSender

email = EmailSender(
    host="smtp.myserver.com", port=587,
    username="me@example.com", password="<PASSWORD>"
)

plot_content = "<!DOCTYPE html><html lang="en">....</html>"

msg = email.send(
    sender=["me@example.com"],
    receivers=["you@example.com"],
    subject="Link JavaScript big",
    text="The plot is attached.",
    attachments={'plot.html': plot_content}, 
)

如果您喜欢这种方式,它还支持将图像嵌入到 HTML 正文中。它还是高度可定制的并且功能非常丰富,因此很可能足以满足您的用例。

Red Mail 的文档:https://red-mail.readthedocs.io

免责声明:我是作者,请按您认为合适的方式使用它。

https://stackoverflow.com/questions/31555116/

相关文章:

hibernate - Spring-data/Hibernate 不能正确处理空值

java - 如何通过键前缀查询 Samza KeyValueStore?

sql - 如何在 SQL MS Access 2013 中根据文本特征标准和 n+1、n+2、n+

php - 使用 Jsonb 绑定(bind)参数?|使用 Postgres 的 Doctrine2

r - 如何计算 vowpal wabbit 中 LDA 模型的对数似然

facebook - Graph Facebook 发生未知错误

php - 带有 PHP SDK 的 AWS EC2 - 等到实例具有公共(public) DNS

c# - 在基于 Web 的架构上使用 POCO、DTO 和 ViewModel

python - 通过 Mininet 网络发送 "random"流量

c# - Visual Studio : how to use scripts to "build