【发布时间】:2018-08-31 03:12:55
【问题描述】:
我需要在 Jupyter 的 Plotly(离线)中获取点击事件。
我想处理这个问题的方法是使用 javascript 和以下命令将值返回给 python:
var kernel = IPython.notebook.kernel;
kernel.execute(command);
...where 命令类似于variable = xxxxx(就像here)
我一开始就陷入了尝试,试图在 python 中用 HTML 绘制图表(观察到我可以通过这种方式成功加载 jQuery):
from IPython.display import HTML
HTML('''
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<h3>Gráfico</h3>
<hr>
<div id="myDiv"></div>
<script>
var trace1 = {
x: [1, 2, 3, 4],
y: [10, 15, 13, 17],
mode: 'markers'
};
var trace2 = {
x: [2, 3, 4, 5],
y: [16, 5, 11, 10],
mode: 'lines'
};
var trace3 = {
x: [1, 2, 3, 4],
y: [12, 9, 15, 12],
mode: 'lines+markers'
};
var data = [ trace1, trace2, trace3 ];
var layout = {};
Plotly.newPlot('myDiv', data, layout);
</script>
</body>
</html>
''')
错误信息是:
ReferenceError: Plotly 未定义 在 eval (在 globalEval 的 eval (jquery.min.js:2), :21:17) 在评估() 在 Function.globalEval (jquery.min.js:2) 在 ua (jquery.min.js:3) 在 n.fn.init.append (jquery.min.js:3) 在 OutputArea._safe_append (outputarea.js:456) 在 OutputArea.append_execute_result (outputarea.js:493) 在 OutputArea.append_output (outputarea.js:326) 在 OutputArea.handle_output (outputarea.js:257) 在输出 (codecell.js:382)
【问题讨论】: