Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,9 @@ from flask import Flask, request, render_template_string
|
|
2 |
import subprocess
|
3 |
import sys
|
4 |
import os
|
5 |
-
from io import StringIO
|
6 |
-
import contextlib
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
-
# HTML template for the code input and output display
|
11 |
HTML_TEMPLATE = """
|
12 |
<!DOCTYPE html>
|
13 |
<html>
|
@@ -18,6 +15,7 @@ HTML_TEMPLATE = """
|
|
18 |
textarea { width: 100%; height: 300px; }
|
19 |
pre { background: #f4f4f4; padding: 10px; border: 1px solid #ddd; }
|
20 |
.error { color: red; }
|
|
|
21 |
</style>
|
22 |
</head>
|
23 |
<body>
|
@@ -26,6 +24,10 @@ HTML_TEMPLATE = """
|
|
26 |
<textarea name="code" placeholder="Paste your Python code here">{{ code }}</textarea><br>
|
27 |
<input type="submit" value="Run Code">
|
28 |
</form>
|
|
|
|
|
|
|
|
|
29 |
{% if output %}
|
30 |
<h2>Output:</h2>
|
31 |
<pre>{{ output }}</pre>
|
@@ -43,10 +45,17 @@ def run_code():
|
|
43 |
code = ""
|
44 |
output = ""
|
45 |
error = ""
|
|
|
46 |
|
47 |
if request.method == "POST":
|
48 |
code = request.form.get("code", "")
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
# Write the code to a temporary file
|
51 |
temp_file = "temp_code.py"
|
52 |
with open(temp_file, "w") as f:
|
@@ -58,12 +67,12 @@ def run_code():
|
|
58 |
[sys.executable, temp_file],
|
59 |
capture_output=True,
|
60 |
text=True,
|
61 |
-
timeout=
|
62 |
)
|
63 |
output = result.stdout
|
64 |
error = result.stderr
|
65 |
except subprocess.TimeoutExpired:
|
66 |
-
error = "Execution timed out after
|
67 |
except Exception as e:
|
68 |
error = f"An error occurred: {str(e)}"
|
69 |
finally:
|
@@ -71,7 +80,7 @@ def run_code():
|
|
71 |
if os.path.exists(temp_file):
|
72 |
os.remove(temp_file)
|
73 |
|
74 |
-
return render_template_string(HTML_TEMPLATE, code=code, output=output, error=error)
|
75 |
|
76 |
if __name__ == "__main__":
|
77 |
-
app.run(debug=True, host="0.0.0.0", port=
|
|
|
2 |
import subprocess
|
3 |
import sys
|
4 |
import os
|
|
|
|
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
|
|
8 |
HTML_TEMPLATE = """
|
9 |
<!DOCTYPE html>
|
10 |
<html>
|
|
|
15 |
textarea { width: 100%; height: 300px; }
|
16 |
pre { background: #f4f4f4; padding: 10px; border: 1px solid #ddd; }
|
17 |
.error { color: red; }
|
18 |
+
.warning { color: orange; }
|
19 |
</style>
|
20 |
</head>
|
21 |
<body>
|
|
|
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>
|
29 |
+
<pre class="warning">{{ warning }}</pre>
|
30 |
+
{% endif %}
|
31 |
{% if output %}
|
32 |
<h2>Output:</h2>
|
33 |
<pre>{{ output }}</pre>
|
|
|
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:
|
|
|
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:
|
|
|
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=5000)
|