我正在尝试运行并显示一些简单的 python 代码,并希望我可以使用 python-shell package 。是否可以使用react js运行该包?
我下面有一个代码沙箱。有没有办法在单击按钮时运行简单的“x=1+1;print(x)”脚本?
代码沙盒: https://codesandbox.io/s/little-meadow-wlni3
这是 Code.js 文件:
import React, { Component } from "react";
import { PythonShell } from "python-shell";
export default class Code extends Component {
render() {
function runPython() {
PythonShell.runString("x=1+1;print(x)", null, function(err) {
if (err) throw err;
console.log("finished");
});
}
return (
<>
<div className="output">code goes here</div>
<button type="button" onClick={runPython}>
Run
</button>
</>
);
}
}
最佳答案
最终创建了一个express.js后端API并在那里运行它来处理Python,然后将输出发送到React客户端。
关于python - 如何在 React js 应用程序中使用 npm python-shell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56798164/