File size: 2,378 Bytes
a4ea8a9
e4a160c
 
6a4b0a8
e4a160c
5add52e
6a4b0a8
 
 
5add52e
6a4b0a8
0f94234
6a4b0a8
 
e4a160c
6a4b0a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a160c
 
 
 
6a4b0a8
e4a160c
6a4b0a8
e4a160c
 
6a4b0a8
e4a160c
6a4b0a8
 
5add52e
6a4b0a8
 
 
 
5add52e
6a4b0a8
 
5add52e
6a4b0a8
 
5add52e
6a4b0a8
 
 
 
 
 
 
 
 
e4a160c
6a4b0a8
 
 
 
 
 
 
 
 
5add52e
6a4b0a8
 
 
 
 
e4a160c
6a4b0a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import streamlit as st
import subprocess
import os
import tempfile
import shutil

# Set up a temporary working directory
if "temp_dir" not in st.session_state:
    st.session_state.temp_dir = tempfile.mkdtemp()

temp_dir = st.session_state.temp_dir

# Custom CSS for Google Colab-like design
st.markdown("""
    <style>
    .terminal {
        background-color: #1e1e1e;
        color: #00ff00;
        font-family: monospace;
        padding: 10px;
        border-radius: 5px;
        overflow-y: auto;
        max-height: 300px;
    }
    .terminal-input {
        background-color: #1e1e1e;
        color: #00ff00;
        font-family: monospace;
        border: none;
        outline: none;
        padding: 10px;
        width: 100%;
        border-radius: 5px;
    }
    .stButton>button {
        background-color: #4CAF50;
        color: white;
        padding: 10px 20px;
        border: none;
        border-radius: 5px;
        cursor: pointer;
    }
    </style>
""", unsafe_allow_html=True)

# App title
st.title("Google Colab-like Terminal in Streamlit")

# Command execution area
st.subheader("Terminal")
if "output" not in st.session_state:
    st.session_state.output = ""

# Display terminal output
st.markdown(f'<div class="terminal">{st.session_state.output}</div>', unsafe_allow_html=True)

# Command input
command = st.text_input("Enter your command:", placeholder="e.g., pip install numpy")

# Execute the command
if st.button("Run"):
    try:
        # Execute the command and capture output
        result = subprocess.run(command, shell=True, cwd=temp_dir, capture_output=True, text=True)
        output = result.stdout if result.returncode == 0 else result.stderr
        st.session_state.output += f"$ {command}\n{output}\n"
    except Exception as e:
        st.session_state.output += f"$ {command}\nError: {e}\n"

# File management
st.subheader("Files")
if st.button("Show Files"):
    files = os.listdir(temp_dir)
    if files:
        st.write("Files in the working directory:")
        for file in files:
            file_path = os.path.join(temp_dir, file)
            st.download_button(label=f"Download {file}", data=open(file_path, "rb"), file_name=file)
    else:
        st.write("No files found.")

# Cleanup temporary files when the session ends
def cleanup():
    shutil.rmtree(temp_dir, ignore_errors=True)

st.on_session_end(cleanup)