Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,86 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import subprocess
|
| 3 |
import os
|
|
|
|
| 4 |
import shutil
|
| 5 |
-
import uuid
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
import atexit
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
Path(workspace_dir).mkdir(parents=True, exist_ok=True)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
def run_shell_command(command):
|
| 16 |
-
try:
|
| 17 |
-
result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)
|
| 18 |
-
return result
|
| 19 |
-
except subprocess.CalledProcessError as e:
|
| 20 |
-
return f"Error: {e.output}"
|
| 21 |
-
|
| 22 |
-
# Cleanup workspace when the script exits
|
| 23 |
-
def cleanup_workspace():
|
| 24 |
-
if os.path.exists(workspace_dir):
|
| 25 |
-
shutil.rmtree(workspace_dir)
|
| 26 |
-
atexit.register(cleanup_workspace)
|
| 27 |
|
| 28 |
-
# Custom CSS for
|
| 29 |
-
st.markdown(
|
| 30 |
-
"""
|
| 31 |
<style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
.stButton>button {
|
| 33 |
background-color: #4CAF50;
|
| 34 |
color: white;
|
| 35 |
-
border: none;
|
| 36 |
padding: 10px 20px;
|
| 37 |
-
|
| 38 |
-
text-decoration: none;
|
| 39 |
-
font-size: 16px;
|
| 40 |
-
margin: 4px 2px;
|
| 41 |
-
cursor: pointer;
|
| 42 |
border-radius: 5px;
|
| 43 |
-
|
| 44 |
-
.stButton>button:hover {
|
| 45 |
-
background-color: #45a049;
|
| 46 |
-
}
|
| 47 |
-
textarea {
|
| 48 |
-
font-family: "Courier New", monospace;
|
| 49 |
-
font-size: 14px;
|
| 50 |
}
|
| 51 |
</style>
|
| 52 |
-
|
| 53 |
-
unsafe_allow_html=True,
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
st.title("Advanced Streamlit IDE")
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
st.
|
| 60 |
-
default_code = """import streamlit as st
|
| 61 |
|
| 62 |
-
|
| 63 |
-
st.
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
st.
|
| 68 |
-
packages = st.text_input("Enter package names separated by space (e.g., numpy pandas):")
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
install_command = f"pip install --target={workspace_dir} {packages}"
|
| 73 |
-
install_output = run_shell_command(install_command)
|
| 74 |
-
st.text(install_output)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
if st.button("Run
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
st.text(run_output)
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
else:
|
| 95 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
st.
|
| 98 |
-
st.info(
|
| 99 |
-
"""
|
| 100 |
-
- Write and execute Python code in a temporary workspace.
|
| 101 |
-
- Install and use packages dynamically within your session.
|
| 102 |
-
- All data is deleted once you leave the site.
|
| 103 |
-
"""
|
| 104 |
-
)
|
|
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|