Alright, so basically, I own this website: https://quarkedit.ga which right now has a HTML/CSS/JS editor using Ace. 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. I'm going for something like https://repl.it 's python thing. Just wondering if there is any API which i could use, something like
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:
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.
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. PyPy.js has a REPL in a browser available to play with, so that's worth taking a look at.
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.
Hopefully that helps you get started.
First create a file.py to write code to and execute.
You can use javascript to send a XMLHttpRequest
to a python file
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:
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.