简体   繁体   English

我将如何向我的网站添加python代码运行器

[英]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: 我基本上想知道并找出这些东西:

  • What API's can I use to do this? 我可以使用哪些API来执行此操作?
  • What would the syntax be? 语法是什么?
  • Can I do this with pure HTML/CSS/JS or would I have to use a JS Library? 我可以使用纯HTML / CSS / JS来执行此操作,还是必须使用JS库?

I have the input for the code done, and the syntax highlighting (Ace) But what I need is the: 我已经完成了代码的输入,并且语法高亮显示了(Ace),但是我需要的是:

  • Execution 执行
  • 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.

相关问题 Unity:我将如何为无尽的亚军游戏创建一个加电系统? [等候接听] - Unity: How would I go about creating a power up system into my endless runner game? [on hold] 我将如何在此网站上为导航创建悬停动画? - How would I go about creating the hover animation for the nav in this website? 我 go 如何将 arguments 添加到我的 discord.js 命令? - How would i go about adding arguments to my discord.js commands? 如何在指令中添加自定义过滤器? - How would I go about adding a custom filter in a directive? 我要如何在旅途中添加一些段落并使用段落标签将结果显示到文档中 - how would i go about adding some paragraphs on the go and show the result to the document with paragraph tags 我将如何处理? (Chrome扩展程序) - How would I go about this? (Chrome extension) 我 go 如何根据左侧两列中是否有值向 Google 表格添加复选框? - How would I go about adding a checkbox to Google Sheets based on whether there is a value within either of 2 columns to the left? 我如何为一个碰到窗口顶部的元素添加顶部偏移? - How would I go about adding a top offset for an element that sticks when it hits the top of the window? 我想在我的 React 应用程序中添加一个文本框,我将如何处理这个问题? - I want to add a textbox to my React app, how would I go about this? 我 go 如何在没有服务器端代码的情况下在 OfficeJS 插件中使用 Microsoft Graph? - How would I go about using Microsoft Graph in an OfficeJS addin without server-side code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM