好的,基本上,我拥有这个网站:https://quarkedit.ga,它现在有一个使用Ace的HTML/CSS/JS编辑器。现在我需要的是我也有一个python语言的,但我不知道如何使终端或任何显示。我想要一些像https://repl.it的python那样的东西。
output = evaluatePythonCode("print(\"test\")");
我基本上想知道并找出这些事情:
我已经完成了代码的输入和语法突出显示(Ace),但我需要的是:
感谢所有的帮助,如果这个问题不合适或有任何问题,只需评论,我将删除它。
谢谢!
发布于 2018-06-02 06:40:33
您有两个选择:要么在浏览器中评估Python,要么将其发送回某个服务器,该服务器可以产生一个Python进程来评估代码。
对于前者,可以使用there are a few Python-implemented-via-JavaScript solutions,我个人不能保证,但这将是更快的选择,并且不需要服务器来执行代码。PyPy.js在浏览器中有一个可供使用的REPL,因此值得一看。
对于服务器端执行,有大量的方法,这都取决于您的服务器技术、您正在使用的Python解释器、您将如何处理安全性/DOS等。
希望这能帮助你入门。
发布于 2018-06-02 07:17:58
首先,创建一个file.py来编写代码并执行。
您可以使用javascript将XMLHttpRequest
发送到python文件
var xhr = new XMLHttpRequest();
xhr.open("POST", "exec?text=" + code, true);
xhr.onload = function(e) {
var output = JSON.parse(xhr.response);
// do something with the output
}
xhr.send();
并在python文件中使用Flask和subprocess:
import subprocess
from flask import Flask, request
app = Flask(__name__)
@app.route('/exec', methods=['POST']) // route app to /exec
def result():
with open('file.py', 'w') as code: // 'w' means to override existing code in the file
code.write(request['code']) // write the code
return subprocess.check_output(["python", "file.py"]) // execute the code using the terminal
返回代码的输出。如果这个不起作用,请告诉我。谢谢!
https://stackoverflow.com/questions/50651652
复制相似问题