Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +58 -0
- backend.py +29 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from streamlit_ace import st_ace
|
4 |
+
|
5 |
+
# Configure Streamlit page
|
6 |
+
st.set_page_config(
|
7 |
+
page_title="Online Python Compiler",
|
8 |
+
page_icon="🐍",
|
9 |
+
layout="wide"
|
10 |
+
)
|
11 |
+
|
12 |
+
st.title("🐍 Online Python Compiler")
|
13 |
+
|
14 |
+
# Custom CSS for better design
|
15 |
+
st.markdown(
|
16 |
+
"""
|
17 |
+
<style>
|
18 |
+
body { background-color: #E3F2FD; }
|
19 |
+
.stTextArea textarea { font-size: 16px; font-family: monospace; }
|
20 |
+
.stButton button { background-color: #1E90FF; color: white; }
|
21 |
+
</style>
|
22 |
+
""",
|
23 |
+
unsafe_allow_html=True,
|
24 |
+
)
|
25 |
+
|
26 |
+
# Default Python code template
|
27 |
+
default_code = '''# Write your Python code here
|
28 |
+
print("Hello, World!")
|
29 |
+
'''
|
30 |
+
|
31 |
+
# Code editor (using Ace Editor)
|
32 |
+
code = st_ace(
|
33 |
+
value=default_code,
|
34 |
+
language="python",
|
35 |
+
theme="monokai",
|
36 |
+
key="python_editor",
|
37 |
+
font_size=16,
|
38 |
+
height=300,
|
39 |
+
auto_update=True,
|
40 |
+
)
|
41 |
+
|
42 |
+
# Run button
|
43 |
+
if st.button("▶ Run Code"):
|
44 |
+
if code.strip():
|
45 |
+
with st.spinner("Running your code..."):
|
46 |
+
try:
|
47 |
+
# Send code to backend
|
48 |
+
response = requests.post("http://127.0.0.1:5000/run", json={"code": code})
|
49 |
+
output = response.json().get("output", "Error in execution.")
|
50 |
+
|
51 |
+
# Display output
|
52 |
+
st.subheader("📌 Output:")
|
53 |
+
st.code(output, language="text")
|
54 |
+
|
55 |
+
except requests.exceptions.RequestException as e:
|
56 |
+
st.error(f"⚠️ Error connecting to the backend: {e}")
|
57 |
+
else:
|
58 |
+
st.warning("⚠️ Please write some Python code before running.")
|
backend.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import subprocess
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
@app.route("/run", methods=["POST"])
|
7 |
+
def run_code():
|
8 |
+
try:
|
9 |
+
code = request.json.get("code", "")
|
10 |
+
|
11 |
+
if not code:
|
12 |
+
return jsonify({"output": "No code provided!"}), 400
|
13 |
+
|
14 |
+
# Run Python code in a subprocess
|
15 |
+
process = subprocess.run(
|
16 |
+
["python", "-c", code],
|
17 |
+
capture_output=True,
|
18 |
+
text=True
|
19 |
+
)
|
20 |
+
|
21 |
+
output = process.stdout if process.stdout else process.stderr
|
22 |
+
return jsonify({"output": output})
|
23 |
+
|
24 |
+
except Exception as e:
|
25 |
+
return jsonify({"output": f"Error: {str(e)}"}), 500
|
26 |
+
|
27 |
+
|
28 |
+
if __name__ == "__main__":
|
29 |
+
app.run(debug=True, port=5000)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
streamlit_ace
|
3 |
+
flask
|
4 |
+
requests
|