Artificial-superintelligence commited on
Commit
6a4b0a8
·
verified ·
1 Parent(s): 0f94234

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -81
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
- # Generate a unique workspace for the user
10
- session_id = str(uuid.uuid4())
11
- workspace_dir = f"temp_workspace_{session_id}"
12
- Path(workspace_dir).mkdir(parents=True, exist_ok=True)
13
 
14
- # Helper function to execute shell commands
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 styling
29
- st.markdown(
30
- """
31
  <style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  .stButton>button {
33
  background-color: #4CAF50;
34
  color: white;
35
- border: none;
36
  padding: 10px 20px;
37
- text-align: center;
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
- # Editor for writing Python code
59
- st.subheader("Code Editor")
60
- default_code = """import streamlit as st
61
 
62
- st.title("Welcome to the Advanced Streamlit IDE!")
63
- st.write("Start coding your Python project here.")"""
64
- code = st.text_area("Write your Python code below:", value=default_code, height=300)
 
65
 
66
- # Option to install packages
67
- st.subheader("Install Python Packages")
68
- packages = st.text_input("Enter package names separated by space (e.g., numpy pandas):")
69
 
70
- if st.button("Install Packages"):
71
- with st.spinner("Installing packages..."):
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
- # Run the code in a temporary workspace
77
- if st.button("Run Code"):
78
- temp_file_path = os.path.join(workspace_dir, "temp_app.py")
79
- with open(temp_file_path, "w") as temp_file:
80
- temp_file.write(code)
81
-
82
- with st.spinner("Running your code..."):
83
- run_command = f"PYTHONPATH={workspace_dir} python {temp_file_path}"
84
- run_output = run_shell_command(run_command)
85
- st.text(run_output)
86
 
87
- # Export code
88
- if st.button("Export Code"):
89
- temp_file_path = os.path.join(workspace_dir, "temp_app.py")
90
- if os.path.exists(temp_file_path):
91
- with open(temp_file_path, "r") as temp_file:
92
- exported_code = temp_file.read()
93
- st.download_button("Download Code", data=exported_code, file_name="exported_code.py", mime="text/plain")
 
 
94
  else:
95
- st.error("No code to export!")
 
 
 
 
96
 
97
- st.markdown("---")
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)