如何在react js应用程序中使用npm python shell?

如何在react js应用程序中使用npm python shell?,python,reactjs,Python,Reactjs,我正在尝试运行和显示一些简单的python代码,并希望能够使用。是否可以使用react js运行包 我在下面有一个代码沙盒。单击按钮时,是否有方法运行简单的“x=1+1;打印(x)”脚本 代码沙盒: 下面是Code.js文件: import React, { Component } from "react"; import { PythonShell } from "python-shell"; export default class Code extends Component {

我正在尝试运行和显示一些简单的python代码,并希望能够使用。是否可以使用react js运行包

我在下面有一个代码沙盒。单击按钮时,是否有方法运行简单的“x=1+1;打印(x)”脚本

代码沙盒:

下面是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>
      </>
    );
  }
}
import React,{Component}来自“React”;
从“PythonShell”导入{PythonShell};
导出默认类代码扩展组件{
render(){
函数runPython(){
runString(“x=1+1;print(x)”,null,函数(err){
如果(错误)抛出错误;
控制台日志(“完成”);
});
}
返回(
代码在这里
跑
);
}
}

最终创建了一个express.js后端api,并在那里运行它来处理python,然后将输出发送到react客户端。

PythonShell通常用于后端从nodejs应用程序运行python。React是一个前端框架(web浏览器只运行javascript),因此我怀疑这是否容易实现。您是在为web或本机构建React应用程序吗?谢谢,是的,我希望有一种方法可以以某种方式处理它并通过web应用程序发送