javascript - 如何通过HTML按钮运行Python脚本?

标签 javascript python html

我想通过 HTML 按钮运行 Python 脚本。 我没有使用任何服务器。

我尝试使用本地服务器,但出现此错误:jquery-3.3.1.min.js:2 GET http://127.0.0.1:8080/home/techm/Documents/labelImg-master/labelImg .py 404(未找到)

我尝试在没有服务器的情况下也收到此错误:jquery-3.3.1.min.js:2 无法加载文件:///home/techm/Documents/labelImg-master/labelImg.py :跨源请求仅支持以下协议(protocol)方案:http、data、chrome、chrome-extension、https。

这是我的 HTML 代码:

<!DOCTYPE html>
<html>
  <head>    
  </head>
  <body>
    <input type="button" id='script' name="scriptbutton" value=" Run Script " onclick="goPython()">

    <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>


    <script>
        function goPython(){
            $.ajax({
              url: "/home/techm/Documents/labelImg-master/run.sh",
             context: document.body
            }).done(function() {
             alert('finished python script');;
            });
        }
    </script>
  </body>
</html>

有没有其他方法可以单击 HTML 页面中的按钮并自动运行 python 脚本。

最佳答案

如何使用 Brython? 它是客户端的 python 解释器。

这是网站。

https://brython.info/index.html

你可以像这样使用它,

<div id="editor">
    print(123)
</div>
<button id="btn">Run Python</button>

...

<script type="text/javascript" src="https://cdn.rawgit.com/brython-dev/brython/stable/www/src/brython.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/brython-dev/brython/stable/www/src/brython_stdlib.js"></script>
<script type="text/python">
    from browser import doc, window
    from browser import html

    def exec_python():
        exec(doc['editor'].value)

    doc['btn'].bind('click', exec_python)
</script>

关于javascript - 如何通过HTML按钮运行Python脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52163199/

相关文章:

javascript - react 原生 : Highlight component/area by darkening the rest

python - pylab 3d 散点图与绘制数据的 2d 投影

python - 将 matlab 循环转换为 python 循环

javascript - 创建在单击另一个对象时隐藏的跨度

java - jsoup 中 element.children 的替代方案

javascript - 将鼠标悬停在按钮上会出现 div。如何使按钮在出现的 div 上保持悬停状态?

javascript - jQuery:全局异常处理程序

php - 按国家/地区获取 Twitter 主题标签

python - MLP 中的 Theano 批量学习,偏差形状

javascript - SPA/AngularJs 的版本控制