在线编译器Django

uxhixvfz  于 2023-03-04  发布在  Go
关注(0)|答案(1)|浏览(151)

我正在做一个Django项目,涉及到为python代码创建一个在线编译器。我在Flask框架中找到了一个项目,我想为Django修改代码。有人可以帮助我,因为我是一个Django初学者,我真的需要一些帮助。谢谢
runcode.py

import subprocess
import sys
import os

class RunPyCode(object):

def __init__(self, code=None):
    self.code = code
    if not os.path.exists('running'):
        os.mkdir('running')

def _run_py_prog(self, cmd="a.py"):
    cmd = [sys.executable, cmd]
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    result = p.wait()
    a, b = p.communicate()
    self.stdout, self.stderr = a.decode("utf-8"), b.decode("utf-8")
    return result

def run_py_code(self, code=None):
    filename = "./running/a.py"
    if not code:
        code = self.code
    with open(filename, "w") as f:
        f.write(code)
    self._run_py_prog(filename)
    return self.stderr, self.stdout

webdev.py

from flask import Flask, render_template, request
from runcode import runcode
app = Flask(__name__)

default_py_code = """import sys
import os

if __name__ == "__main__":
    print "Hello Python World!!"
"""

default_rows = "15"
default_cols = "60"

@app.route("/py")
@app.route("/runpy", methods=['POST', 'GET'])
def runpy():
    if request.method == 'POST':
        code = request.form['code']
        run = runcode.RunPyCode(code)
        rescompil, resrun = run.run_py_code()
        if not resrun:
            resrun = 'No result!'
    else:
        code = default_py_code
        resrun = 'No result!'
        rescompil = "No compilation for Python"

    return render_template("main.html",
                           code=code,
                           target="runpy",
                           resrun=resrun,
                           rescomp=rescompil,#"No compilation for Python",
                           rows=default_rows, cols=default_cols)

if __name__ == "__main__":
    app.run()

a.py

import sys
import os

if __name__ == "__main__":
    print ("Hello Python World!!")

base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>WebDevTools</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="/static/devc.css">
</head>
<body id="index" class="home">
<div id="links">
<ul>
<li><a href="/py">Python Code</a></li>
</ul>
</div>
<div id="content">
    <!-- ************  CODING ZONE  ************ -->
    <div id="code">
        <form method="post" action="{{ url_for(target) }}">
            <div id="title-code" class="head-section">
              Source Code
            </div>
            <input id="launch-button" class="head-section" type="submit" value="Launch" />
            {% block code %}
            {% endblock %}
        </form>
    </div>

    <!-- ************ RUNNING ZONE RESULTS ************ -->

    <div id="result">
        <div id="title-result" class="head-section">
            Output result
        </div>
        {% block run %}
        {% endblock %}
    </div>

    <!-- ************ COMPILATION RESULTS ZONE ************ -->

    <div id="compile">
        <div id="title-compile" class="head-section">
            Compilation / Output
        </div>
        {% block comp %}
        {% endblock %}
    </div>
</div>
</body>
</html>

main.html

{% extends "base.html" %}
{% block code %}
<textarea id="text-code" name="code" rows={{ rows }} cols={{ cols }}>
{{ code }}
</textarea><br/>
<div id="text-code-ace" class="hidden">{{ code }}</div>
{% endblock %}

{% block run %}
<textarea id="text-result" rows="18" cols="70" readonly>{{ resrun }}</textarea>
{% endblock %}

{% block comp %}
<textarea id="text-compile" rows="7" cols="140" readonly>{{ rescomp }}</textarea>
{% endblock %}
bkkx9g8r

bkkx9g8r1#

答案是:

class New(View):
template_name   =   'webapp/new.html'
form_class      =   SofaProjectForm

def get(self,request):
    form = self.form_class(None)
    return render(request,'webapp/new.html',{'form':form})

def post(self,request):
    if request.method == 'POST':

        form = self.form_class(request.POST)
        code = request.POST['code']
        run = runcode.RunPyCode(code)
        rescompil, resrun = run.run_py_code()
        resrun=resrun
        rescomp=rescompil

        if not resrun:
            resrun = 'No result!'
    else:
        code = default_py_code
        resrun = 'No result!'
        rescompil = "No compilation for Python"

    return render (request,'webapp/new.html', {'code':code,'resrun':resrun,'rescomp':rescomp})

相关问题