在线编译器Django

时间:2018-04-23 10:01:05

标签: python django flask

我正在开发一个Django项目,该项目涉及为python代码创建在线编译器。我在Flask框架中找到了一个项目,我想调整Django的代码。有人可以帮助我,因为我是django的初学者,我真的需要一些帮助。感谢

runco​​de.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 %}

1 个答案:

答案 0 :(得分:0)

the answear是:

0