如何在单击按钮时从html调用python函数并显示函数返回的结果

2024-04-18 16:09:36 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的简单html代码index.html

    <html>
           <head>
               <script type="text/javascript" src="index.js"></script>
           </head>
           <body>
               <input type="button" onclick="fun()" value="alert">
           </body>
      </html>

这是index.js文件

       fun(){}
           const spawn=require('child_process').spawn;
           const process=spawn('python',['./hello.py']);
           process.stdout.on('data',(data)=>{
               console.log(data.toString());
           })
       }

这是hello.py文件

print("Hello world")

我已经试过了,但在点击按钮时,这给了我关于require的错误。。 有什么建议吗?我该怎么做


Tags: 文件hellodataindexhtmltypejsscript