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

标签 python reactjs

我正在尝试运行并显示一些简单的 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/

相关文章:

python - 日期的 Pandas 分位数函数?

javascript - 函数表达式在渲染函数中的表现

Python:类型注释,如何定义元组的元素?

python - 优化 itertools 与分组 DataFrame 和后置过滤器的组合

javascript - 操作 'FooQuery' 包装 'Foo' 需要一个变量,但没有找到 react-apollo

javascript - React Native 卡片图像封面

reactjs - 在react-router-dom中重定向?

javascript - Redux + React-Router 不渲染

python - 如何计算列表中的数字

python - 修改 Pandas 数据框中的许多列