Pyodide是一个很新的东西,但是我想知道是否有一种方法可以让用户像检查Js对象一样检查Python对象。例如,现在如果你在pyodide中print一个字典,输出是一个字符串:

但是如果你console.log一个JavaScript对象,它会输出一些浏览器能理解的东西,你可以点击来展开并查看它的属性。

对于调试,我认为这种工具是必要的,几乎所有的IDE都有它。使用pyodide创建一个完整的Python环境,我认为这不会太难。
发布于 2020-01-08 06:37:03
您可以将python对象发送到控制台。在python端,您可以执行以下操作
pyodide.runPython(`
myvar = {"msg": "Hello from python"}
from js import console
console.log(myvar)
`);在javascript端,您可以
console.log(pyodide.globals.myvar);基本的python类型被转换为它们的javascript等效物,并且可以直接检查。其他类型封装在代理对象中。chrome devTools控制台窗口中显示的信息对这些类型帮助不大。
但是,chrome devTools可以使用如下的自定义格式化程序进行扩展
(function() {
var formatter = {
header: function(x, config) {
if (config && config.PyProxyFormatter) {
return ["div", {"width": "100px"}, config.key + ": " + String(x)];
}
if (typeof x === 'function' &&
pyodide._module.PyProxy.isPyProxy(x)) {
return ["div", {}, String(x)];
}
return null;
},
hasBody: function(x) {
return true;
},
body: function(x, config) {
var level = config !== undefined ? config.level : 0;
if (typeof x === 'function' &&
pyodide._module.PyProxy.isPyProxy(x)) {
var keys = pyodide.globals.dir(x);
var elements = keys.map(function(key) {
var childObj = x[key];
var child;
if (typeof childObj === 'object' ||
(typeof childObj === 'function' &&
pyodide._module.PyProxy.isPyProxy(childObj))) {
child = ["object", {
object: childObj,
config: {PyProxyFormatter: true, key: key, level: level + 1}}];
} else {
child = key + ": " + String(childObj);
}
return ["div", {style: "margin-left: " + level*20 + "px"}, child];
});
return ["div", {}].concat(elements);
} else {
return ["div", {}, ["object", { object: x}]];
}
}
};
window.devtoolsFormatters = [formatter];
}
)();https://stackoverflow.com/questions/58685769
复制相似问题