Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,86 @@
|
|
1 |
-
|
2 |
import subprocess
|
3 |
-
import tempfile
|
4 |
import os
|
|
|
5 |
import shutil
|
6 |
-
import requests
|
7 |
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
TEMP_DIR = tempfile.mkdtemp()
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
command = request.json['command']
|
20 |
-
|
21 |
-
if command.startswith('pip install'):
|
22 |
-
return execute_pip_install(command)
|
23 |
-
elif command.startswith('git clone'):
|
24 |
-
return execute_git_clone(command)
|
25 |
-
elif command.startswith('python'):
|
26 |
-
return execute_python_script(command)
|
27 |
-
elif command.startswith('download'):
|
28 |
-
return download_file(command)
|
29 |
-
elif command.startswith('!'):
|
30 |
-
return execute_shell_command(command[1:])
|
31 |
-
else:
|
32 |
-
return jsonify({'output': 'Unknown command. Try pip install, git clone, python, download, or !<shell command>.'})
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
except subprocess.CalledProcessError as e:
|
39 |
-
return jsonify({'output': f'Error: {e.stderr}'})
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True, cwd=TEMP_DIR)
|
44 |
-
return jsonify({'output': f'Repository cloned successfully to {TEMP_DIR}'})
|
45 |
-
except subprocess.CalledProcessError as e:
|
46 |
-
return jsonify({'output': f'Error: {e.stderr}'})
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, dir=TEMP_DIR) as temp_file:
|
51 |
-
temp_file.write(command[7:]) # Remove 'python ' from the beginning
|
52 |
-
temp_file_path = temp_file.name
|
53 |
-
|
54 |
-
result = subprocess.run(f'python {temp_file_path}', shell=True, check=True, capture_output=True, text=True)
|
55 |
-
os.unlink(temp_file_path)
|
56 |
-
return jsonify({'output': result.stdout})
|
57 |
-
except subprocess.CalledProcessError as e:
|
58 |
-
return jsonify({'output': f'Error: {e.stderr}'})
|
59 |
|
60 |
-
|
|
|
61 |
try:
|
62 |
-
|
63 |
-
|
64 |
-
if
|
65 |
-
|
66 |
-
with open(filename, 'wb') as f:
|
67 |
-
f.write(response.content)
|
68 |
-
return jsonify({'output': f'File downloaded successfully: {filename}'})
|
69 |
-
else:
|
70 |
-
return jsonify({'output': f'Error downloading file: HTTP {response.status_code}'})
|
71 |
except Exception as e:
|
72 |
-
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
-
|
82 |
-
def cleanup(
|
83 |
-
shutil.rmtree(
|
84 |
|
85 |
-
|
86 |
-
app.run(debug=True)
|
|
|
1 |
+
import streamlit as st
|
2 |
import subprocess
|
|
|
3 |
import os
|
4 |
+
import tempfile
|
5 |
import shutil
|
|
|
6 |
|
7 |
+
# Set up a temporary working directory
|
8 |
+
if "temp_dir" not in st.session_state:
|
9 |
+
st.session_state.temp_dir = tempfile.mkdtemp()
|
10 |
|
11 |
+
temp_dir = st.session_state.temp_dir
|
|
|
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 |
+
# Execute the command and capture output
|
64 |
+
result = subprocess.run(command, shell=True, cwd=temp_dir, capture_output=True, text=True)
|
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 |
+
st.session_state.output += f"$ {command}\nError: {e}\n"
|
69 |
|
70 |
+
# File management
|
71 |
+
st.subheader("Files")
|
72 |
+
if st.button("Show Files"):
|
73 |
+
files = os.listdir(temp_dir)
|
74 |
+
if files:
|
75 |
+
st.write("Files in the working directory:")
|
76 |
+
for file in files:
|
77 |
+
file_path = os.path.join(temp_dir, file)
|
78 |
+
st.download_button(label=f"Download {file}", data=open(file_path, "rb"), file_name=file)
|
79 |
+
else:
|
80 |
+
st.write("No files found.")
|
81 |
|
82 |
+
# Cleanup temporary files when the session ends
|
83 |
+
def cleanup():
|
84 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
85 |
|
86 |
+
st.on_session_end(cleanup)
|
|