我想通过 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/