Artificial-superintelligence
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,40 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import shutil
|
6 |
|
7 |
-
|
8 |
-
if "temp_dir" not in st.session_state:
|
9 |
-
st.session_state.temp_dir = tempfile.mkdtemp()
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
# Custom CSS for Google Colab-like design
|
14 |
-
st.markdown("""
|
15 |
-
<style>
|
16 |
-
.terminal {
|
17 |
-
background-color: #1e1e1e;
|
18 |
-
color: #00ff00;
|
19 |
-
font-family: monospace;
|
20 |
-
padding: 10px;
|
21 |
-
border-radius: 5px;
|
22 |
-
overflow-y: auto;
|
23 |
-
max-height: 300px;
|
24 |
-
}
|
25 |
-
.terminal-input {
|
26 |
-
background-color: #1e1e1e;
|
27 |
-
color: #00ff00;
|
28 |
-
font-family: monospace;
|
29 |
-
border: none;
|
30 |
-
outline: none;
|
31 |
-
padding: 10px;
|
32 |
-
width: 100%;
|
33 |
-
border-radius: 5px;
|
34 |
-
}
|
35 |
-
.stButton>button {
|
36 |
-
background-color: #4CAF50;
|
37 |
-
color: white;
|
38 |
-
padding: 10px 20px;
|
39 |
-
border: none;
|
40 |
-
border-radius: 5px;
|
41 |
-
cursor: pointer;
|
42 |
-
}
|
43 |
-
</style>
|
44 |
-
""", unsafe_allow_html=True)
|
45 |
-
|
46 |
-
# App title
|
47 |
-
st.title("Google Colab-like Terminal in Streamlit")
|
48 |
-
|
49 |
-
# Command execution area
|
50 |
-
st.subheader("Terminal")
|
51 |
-
if "output" not in st.session_state:
|
52 |
-
st.session_state.output = ""
|
53 |
-
|
54 |
-
# Display terminal output
|
55 |
-
st.markdown(f'<div class="terminal">{st.session_state.output}</div>', unsafe_allow_html=True)
|
56 |
-
|
57 |
-
# Command input
|
58 |
-
command = st.text_input("Enter your command:", placeholder="e.g., pip install numpy")
|
59 |
-
|
60 |
-
# Execute the command
|
61 |
-
if st.button("Run"):
|
62 |
try:
|
63 |
-
#
|
64 |
-
|
65 |
-
output = result.stdout if result.returncode == 0 else result.stderr
|
66 |
-
st.session_state.output += f"$ {command}\n{output}\n"
|
67 |
except Exception as e:
|
68 |
-
|
69 |
-
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
#
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import sys
|
3 |
+
import io
|
4 |
+
import contextlib
|
|
|
5 |
|
6 |
+
app = Flask(__name__)
|
|
|
|
|
7 |
|
8 |
+
# Function to execute Python code safely
|
9 |
+
def execute_python_code(code):
|
10 |
+
# Capturing the output of Python execution
|
11 |
+
output = io.StringIO()
|
12 |
+
sys.stdout = output
|
13 |
+
sys.stderr = output
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
try:
|
16 |
+
# Executing the code
|
17 |
+
exec(code)
|
|
|
|
|
18 |
except Exception as e:
|
19 |
+
output.write(f"Error: {e}")
|
20 |
+
|
21 |
+
# Return the captured output
|
22 |
+
return output.getvalue()
|
23 |
+
|
24 |
+
@app.route('/')
|
25 |
+
def index():
|
26 |
+
return app.send_static_file('index.html')
|
27 |
+
|
28 |
+
@app.route('/execute', methods=['POST'])
|
29 |
+
def execute():
|
30 |
+
data = request.get_json()
|
31 |
+
code = data.get('code', '')
|
32 |
+
|
33 |
+
# Execute the Python code and capture the result
|
34 |
+
result = execute_python_code(code)
|
35 |
+
|
36 |
+
# Send the result back to the frontend
|
37 |
+
return jsonify({'result': result})
|
38 |
+
|
39 |
+
if __name__ == '__main__':
|
40 |
+
app.run(debug=True)
|