Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,46 @@ import os
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
HTML_TEMPLATE = """
|
9 |
<!DOCTYPE html>
|
10 |
<html>
|
@@ -16,6 +56,7 @@ HTML_TEMPLATE = """
|
|
16 |
pre { background: #f4f4f4; padding: 10px; border: 1px solid #ddd; }
|
17 |
.error { color: red; }
|
18 |
.warning { color: orange; }
|
|
|
19 |
</style>
|
20 |
</head>
|
21 |
<body>
|
@@ -23,6 +64,7 @@ HTML_TEMPLATE = """
|
|
23 |
<form method="POST" action="/">
|
24 |
<textarea name="code" placeholder="Paste your Python code here">{{ code }}</textarea><br>
|
25 |
<input type="submit" value="Run Code">
|
|
|
26 |
</form>
|
27 |
{% if warning %}
|
28 |
<h2 class="warning">Warning:</h2>
|
@@ -36,51 +78,10 @@ HTML_TEMPLATE = """
|
|
36 |
<h2 class="error">Error:</h2>
|
37 |
<pre class="error">{{ error }}</pre>
|
38 |
{% endif %}
|
|
|
39 |
</body>
|
40 |
</html>
|
41 |
"""
|
42 |
|
43 |
-
@app.route("/", methods=["GET", "POST"])
|
44 |
-
def run_code():
|
45 |
-
code = ""
|
46 |
-
output = ""
|
47 |
-
error = ""
|
48 |
-
warning = ""
|
49 |
-
|
50 |
-
if request.method == "POST":
|
51 |
-
code = request.form.get("code", "")
|
52 |
-
|
53 |
-
# Check if code contains Toga imports
|
54 |
-
if "import toga" in code or "from toga" in code:
|
55 |
-
warning = ("Toga apps create GUI windows, which cannot be displayed in a web browser. "
|
56 |
-
"The code will run, but the GUI will appear on the server (if a display is available). "
|
57 |
-
"Consider downloading the code to run locally.")
|
58 |
-
|
59 |
-
# Write the code to a temporary file
|
60 |
-
temp_file = "temp_code.py"
|
61 |
-
with open(temp_file, "w") as f:
|
62 |
-
f.write(code)
|
63 |
-
|
64 |
-
try:
|
65 |
-
# Run the code in a subprocess with restricted environment
|
66 |
-
result = subprocess.run(
|
67 |
-
[sys.executable, temp_file],
|
68 |
-
capture_output=True,
|
69 |
-
text=True,
|
70 |
-
timeout=5 # Short timeout for GUI apps to avoid hanging
|
71 |
-
)
|
72 |
-
output = result.stdout
|
73 |
-
error = result.stderr
|
74 |
-
except subprocess.TimeoutExpired:
|
75 |
-
error = "Execution timed out after 5 seconds."
|
76 |
-
except Exception as e:
|
77 |
-
error = f"An error occurred: {str(e)}"
|
78 |
-
finally:
|
79 |
-
# Clean up the temporary file
|
80 |
-
if os.path.exists(temp_file):
|
81 |
-
os.remove(temp_file)
|
82 |
-
|
83 |
-
return render_template_string(HTML_TEMPLATE, code=code, output=output, error=error, warning=warning)
|
84 |
-
|
85 |
if __name__ == "__main__":
|
86 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
+
@app.route("/", methods=["GET", "POST"])
|
9 |
+
def run_code():
|
10 |
+
code = ""
|
11 |
+
output = ""
|
12 |
+
error = ""
|
13 |
+
warning = ""
|
14 |
+
iframe = ""
|
15 |
+
|
16 |
+
if request.method == "POST":
|
17 |
+
code = request.form.get("code", "")
|
18 |
+
|
19 |
+
if "import toga" in code or "from toga" in code:
|
20 |
+
warning = ("Toga apps create GUI windows, which run on the server's virtual display. "
|
21 |
+
"View the GUI below (may take a moment to load). Download the code to run locally.")
|
22 |
+
iframe = '<iframe src=":6080" width="800" height="600" frameborder="0"></iframe>'
|
23 |
+
|
24 |
+
temp_file = "temp_code.py"
|
25 |
+
with open(temp_file, "w") as f:
|
26 |
+
f.write(code)
|
27 |
+
|
28 |
+
try:
|
29 |
+
result = subprocess.run(
|
30 |
+
[sys.executable, temp_file],
|
31 |
+
capture_output=True,
|
32 |
+
text=True,
|
33 |
+
timeout=5
|
34 |
+
)
|
35 |
+
output = result.stdout
|
36 |
+
error = result.stderr
|
37 |
+
except subprocess.TimeoutExpired:
|
38 |
+
error = "Execution timed out after 5 seconds."
|
39 |
+
except Exception as e:
|
40 |
+
error = f"An error occurred: {str(e)}"
|
41 |
+
finally:
|
42 |
+
if os.path.exists(temp_file):
|
43 |
+
os.remove(temp_file)
|
44 |
+
|
45 |
+
return render_template_string(HTML_TEMPLATE, code=code, output=output, error=error, warning=warning, iframe=iframe)
|
46 |
+
|
47 |
+
# Update HTML_TEMPLATE to include iframe
|
48 |
HTML_TEMPLATE = """
|
49 |
<!DOCTYPE html>
|
50 |
<html>
|
|
|
56 |
pre { background: #f4f4f4; padding: 10px; border: 1px solid #ddd; }
|
57 |
.error { color: red; }
|
58 |
.warning { color: orange; }
|
59 |
+
iframe { margin-top: 20px; }
|
60 |
</style>
|
61 |
</head>
|
62 |
<body>
|
|
|
64 |
<form method="POST" action="/">
|
65 |
<textarea name="code" placeholder="Paste your Python code here">{{ code }}</textarea><br>
|
66 |
<input type="submit" value="Run Code">
|
67 |
+
<a href="/download?code={{ code | urlencode }}"><button>Download Code</button></a>
|
68 |
</form>
|
69 |
{% if warning %}
|
70 |
<h2 class="warning">Warning:</h2>
|
|
|
78 |
<h2 class="error">Error:</h2>
|
79 |
<pre class="error">{{ error }}</pre>
|
80 |
{% endif %}
|
81 |
+
{{ iframe | safe }}
|
82 |
</body>
|
83 |
</html>
|
84 |
"""
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
if __name__ == "__main__":
|
87 |
app.run(debug=True, host="0.0.0.0", port=7860)
|