我最近遇到了Pyodide项目。
我已经使用 Pyodide 构建了一个小演示,但是尽管我花了很多时间查看源代码,但对我来说如何重定向
print
输出(除了修改 CPython 源代码)并不明显),以及如何将输出从 matplotlib.pyplot 重定向到浏览器。
从源代码来看, FigureCanvasWasm 确实有一个
show()
方法,具有适当的后端用于绘制到浏览器画布 - 但是,我不清楚如何实例化此类并调用它的 show()
方法,或者确实,是否有另一种更明显的方法将绘图重定向到画布。
因此我的问题是:
print()
消息这是我的测试页面:
<!doctype html>
<meta charset="utf-8">
<html lang="en">
<html>
<head>
<title>Demo</title>
<script src="../../pyodide/build/pyodide.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
languagePluginLoader.then(() => {
pyodide.loadPackage(['matplotlib']).then(() => {
pyodide.runPython(`
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
#fig = plt.gcf()
#fig.savefig(imgdata, format='png')
print('Done from python!')`
);
//var image = pyodide.pyimport('imgdata');
//console.log(image);
});});
</script>
<html>
首先让我们看看是否可以在浏览器中显示任何内容;例如一个普通的字符串。 Python 变量存储在
pyodide.globals
属性中。因此,我们可以从那里获取 python 对象并将其放入页面上的 <div>
元素中。
<!doctype html>
<meta charset="utf-8">
<html>
<head>
<title>Demo</title>
<script src="../pyodide/pyodide.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
languagePluginLoader.then(() => {
pyodide.runPython(`my_string = "This is a python string." `);
document.getElementById("textfield").innerText = pyodide.globals.my_string;
});
</script>
<div id="textfield"></div>
<html>
现在我想我们可以用 matplotlib 图形做同样的事情。下面将显示文档中保存的 png 图像。
<!doctype html>
<meta charset="utf-8">
<html lang="en">
<html>
<head>
<title>Demo</title>
<script src="../pyodide/pyodide.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
languagePluginLoader.then(() => {
pyodide.loadPackage(['matplotlib']).then(() => {
pyodide.runPython(`
import matplotlib.pyplot as plt
import io, base64
fig, ax = plt.subplots()
ax.plot([1,3,2])
buf = io.BytesIO()
fig.savefig(buf, format='png')
buf.seek(0)
img_str = 'data:image/png;base64,' + base64.b64encode(buf.read()).decode('UTF-8')`
);
document.getElementById("pyplotfigure").src=pyodide.globals.img_str
});});
</script>
<div id="textfield">A matplotlib figure:</div>
<div id="pyplotdiv"><img id="pyplotfigure"/></div>
<html>
我还没有研究过
backends.wasm_backend
,所以这可能允许以更自动化的方式实现上述操作。
使用 wasm 后端时,图形的 canvas 属性是 FigureCanvasWasm 的实例。调用画布的
show()
方法应该足以在浏览器中显示图形。不幸的是,画布的 create_root_element()
方法中的一个小错误导致图形无法显示。此方法创建一个包含图形的 div
元素。它首先尝试创建一个碘化物输出 div 元素。如果失败,则会创建一个纯 HTML div 元素。然而,该元素永远不会附加到文档中,因此保持不可见。
下面是来自 FigureCanvasWasm 如果发生这种情况的代码行
def create_root_element(self):
# Designed to be overridden by subclasses for use in contexts other
# than iodide.
try:
from js import iodide
return iodide.output.element('div')
except ImportError:
return document.createElement('div')
注释表明非碘化物代码是一个需要通过重写方法来扩展的存根。这需要子类化 FigureCanvasWasm,将其安装为 pyodide 模块并配置 matplotlib 以使用该后端。
但是有一个快捷方式,因为 python 允许重写实例的方法,而无需修改类,如问题 394770 所示。将以下代码放入 HTML 文档中会在浏览器中显示一个图形
import numpy as np
from matplotlib import pyplot as plt
from js import document
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
f = plt.figure()
plt.plot(x,y)
# ordinary function to create a div
def create_root_element1(self):
div = document.createElement('div')
document.body.appendChild(div)
return div
#ordinary function to find an existing div
#you'll need to put a div with appropriate id somewhere in the document
def create_root_element2(self):
return document.getElementById('figure1')
#override create_root_element method of canvas by one of the functions above
f.canvas.create_root_element = create_root_element1.__get__(
create_root_element1, f.canvas.__class__)
f.canvas.show()
最初工具栏不显示图标。我必须下载、解压并安装 fontawesome 以及 pyodide 并在标题中包含以下行才能获取这些
<link rel="stylesheet" href="font-awesome-4.7.0/css/font-awesome.min.css">
编辑: 关于问题的第一部分,将输出流重定向到浏览器,您可以看看它是如何在 pyodide 的 console.html.
中完成的它用 StringIO 对象替换 sys.stdout
pyodide.runPython(`
import sys
import io
sys.stdout = io.StringIO()
`);
然后运行 python 代码(可以完全忽略它是在 wasm 上下文中运行的事实)
pyodide.runPython(`
print("Hello, world!")
`);
最后,将 stdout 缓冲区的内容发送到输出元素
var stdout = pyodide.runPython("sys.stdout.getvalue()")
var div = document.createElement('div');
div.innerText = stdout;
document.body.appendChild(div);
要显示 print() 调用 pyodide 形式,您可以使用 loadPyodide 上的参数来重定向 stdout:
var paragraph = document.getElementById("p");
pyodide = await loadPyodide({
indexURL : "https://cdn.jsdelivr.net/pyodide/v0.18.1/full/",
stdin: window.prompt,
stdout: (text) => {paragraph.textContent += text;},
stderr: (text) => {paragraph.textContent += text;}
});
https://github.com/pyodide/pyodide/blob/main/src/js/pyodide.js
我为 Python 创建了一个简单的交互式 shell。如果您需要更多详细信息,请阅读此教程。
(async () => { // create anonymous async function to enable await
var output = document.getElementById("output")
var code = document.getElementById("code")
output.value = 'Initializing...\n'
window.pyodide = await loadPyodide({stdout: addToOutput, stderr: addToOutput}) // redirect stdout and stderr to addToOutput
output.value += 'Ready!\n'
})()
function addToOutput(s) {
output.value += `${s}\n`
output.scrollTop = output.scrollHeight
}
async function evaluatePython() {
addToOutput(`>>>${code.value}`)
await pyodide.loadPackagesFromImports(code.value, addToOutput, addToOutput)
try {
let result = await pyodide.runPythonAsync(code.value)
addToOutput(`${result}`)
}
catch (e) {
addToOutput(`${e}`)
}
code.value = ''
}
<script src="https://cdn.jsdelivr.net/pyodide/v0.21.3/full/pyodide.js"></script>
Output:
<textarea id="output" style="width: 100%;" rows="10" disabled=""></textarea>
<textarea id="code" rows="3">import numpy as np
np.ones((10,))
</textarea>
<button id="run" onclick="evaluatePython()">Run</button>
这是
matplotlib
的示例。请注意,这将加载一堆依赖项,这将需要几分钟的时间。
const python_code = `
from js import document
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import io, base64
def generate_plot_img():
# get values from inputs
mu = int(document.getElementById('mu').value)
sigma = int(document.getElementById('sigma').value)
# generate an interval
x = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
# calculate PDF for each value in the x given mu and sigma and plot a line
plt.plot(x, stats.norm.pdf(x, mu, sigma))
# create buffer for an image
buf = io.BytesIO()
# copy the plot into the buffer
plt.savefig(buf, format='png')
buf.seek(0)
# encode the image as Base64 string
img_str = 'data:image/png;base64,' + base64.b64encode(buf.read()).decode('UTF-8')
# show the image
img_tag = document.getElementById('fig')
img_tag.src = img_str
buf.close()
`;
(async () => { // create anonymous async function to enable await
window.pyodide = await loadPyodide()
await pyodide.loadPackagesFromImports(python_code)
await pyodide.runPythonAsync(python_code)
})()
<script src="https://cdn.jsdelivr.net/pyodide/v0.21.3/full/pyodide.js"></script>
mu:
<input id='mu' value='1' type="number">
<br><br>
sigma:
<input id='sigma' value='1' type="number">
<br><br>
<button onclick='pyodide.globals.get("generate_plot_img")()'>Plot</button>
<br>
<img id="fig" />
我注意到,如果代码中包含“plt.show()”,则会自动绘制图像。
pyodide.runPython("import matplotlib.pyplot as plt; fig = plt.figure(figsize=(8,3)); plt.plot([1, 2, 3, 4]); plt.ylabel('some numbers'); plt.show(); print('Done from python!')");