[英]How to use npm python-shell in react js app?
I am trying to run and display some simple python code and was hoping I could use the python-shell package . 我试图运行并显示一些简单的python代码,并希望可以使用python-shell包 。 Is it possible to run the package with react js? 是否可以使用react js运行该包?
I have a code sandbox below. 我在下面有一个代码沙箱。 Is there a way to run the simple "x=1+1;print(x)" script when the button is clicked? 单击按钮时,是否可以运行简单的“ x = 1 + 1; print(x)”脚本?
CodeSandbox: https://codesandbox.io/s/little-meadow-wlni3 CodeSandbox: https ://codesandbox.io/s/little-meadow-wlni3
Here is the Code.js file: 这是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客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.