Artificial-superintelligence commited on
Commit
1b38efb
·
verified ·
1 Parent(s): 1598830

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -81
app.py CHANGED
@@ -1,86 +1,40 @@
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)
 
 
 
 
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)