[英]How would I go about adding a python code runner to my website
Alright, so basically, I own this website: https://quarkedit.ga which right now has a HTML/CSS/JS editor using Ace. 好吧,基本上,我拥有这个网站: https : //quarkedit.ga ,该网站现在具有使用Ace的HTML / CSS / JS编辑器。 Now what I need is I also have a python language one, but I don't know how to make the terminal or whatever show up. 现在我需要的是我也有一种python语言,但是我不知道如何制作终端或显示任何内容。 I'm going for something like https://repl.it 's python thing. 我正在寻找类似https://repl.it的python东西。 Just wondering if there is any API which i could use, something like 只是想知道我是否可以使用任何API,例如
output = evaluatePythonCode("print(\"test\")");
I basically want to know and find out these things: 我基本上想知道并找出这些东西:
I have the input for the code done, and the syntax highlighting (Ace) But what I need is the: 我已经完成了代码的输入,并且语法高亮显示了(Ace),但是我需要的是:
Output 输出量
All help will be appreciated, and if this question isn't appropriate or anything just comment and I'll remove it. 所有帮助将不胜感激,如果这个问题不合适,或者只是评论,我将其删除。
Thanks! 谢谢!
You've got two options: either evaluate the Python in the browser or post it back to some server that can spawn a Python process to evaluate the code. 您有两种选择:要么在浏览器中评估Python,要么将其发布回可以生成Python进程来评估代码的服务器。
For the former, there are a few Python-implemented-via-JavaScript solutions out there, which I can't personally vouch for but would be the faster option and wouldn't require you to have servers to execute the code. 对于前者, 有一些通过JavaScript通过Python实现的解决方案 ,我个人不能保证,但是这是更快的选择,并且不需要您有服务器来执行代码。 PyPy.js has a REPL in a browser available to play with, so that's worth taking a look at. PyPy.js在浏览器中有一个REPL可供使用,因此值得一看。
For server-side execution, there's a ton of approaches, all depending on your server technologies, which Python interpreter you're using, how you're going to handle security/DOS, etc. 对于服务器端执行,有很多方法,所有方法都取决于服务器技术,所使用的Python解释器,如何处理安全性/ DOS等。
Hopefully that helps you get started. 希望这可以帮助您入门。
First create a file.py to write code to and execute. 首先创建一个file.py来编写代码并执行。
You can use javascript to send a XMLHttpRequest
to a python file 您可以使用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();
And using Flask and subprocess in the python file: 并在python文件中使用Flask和子进程:
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
Return the output of the code. 返回代码的输出。 Please tell me if this does not work. 请告诉我这是否无效。 Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.