Artificial-superintelligence
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,105 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
import
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
for url in search(query, num=num_results, stop=num_results, pause=2):
|
11 |
-
results.append(url)
|
12 |
-
except Exception as e:
|
13 |
-
st.error(f"Error fetching search results: {e}")
|
14 |
-
return results
|
15 |
|
16 |
-
#
|
17 |
-
def
|
18 |
try:
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
return
|
23 |
-
except Exception as e:
|
24 |
-
return "Error loading website", None
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
st.
|
37 |
-
st.
|
38 |
|
39 |
-
#
|
40 |
-
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
st.write("Top Results:")
|
48 |
-
for idx, url in enumerate(results):
|
49 |
-
st.markdown(f"{idx+1}. [Visit Site]({url})")
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
else:
|
59 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
st.write(response.decode("utf-8"))
|
70 |
-
else:
|
71 |
-
st.error(f"Error: {response}")
|
|
|
1 |
import streamlit as st
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import uuid
|
6 |
+
from pathlib import Path
|
7 |
|
8 |
+
# Generate a unique workspace for the user
|
9 |
+
session_id = str(uuid.uuid4())
|
10 |
+
workspace_dir = f"temp_workspace_{session_id}"
|
11 |
+
Path(workspace_dir).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Helper function to execute shell commands
|
14 |
+
def run_shell_command(command):
|
15 |
try:
|
16 |
+
result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, text=True)
|
17 |
+
return result
|
18 |
+
except subprocess.CalledProcessError as e:
|
19 |
+
return f"Error: {e.output}"
|
|
|
|
|
20 |
|
21 |
+
# Custom CSS for styling
|
22 |
+
st.markdown(
|
23 |
+
"""
|
24 |
+
<style>
|
25 |
+
.stButton>button {
|
26 |
+
background-color: #4CAF50;
|
27 |
+
color: white;
|
28 |
+
border: none;
|
29 |
+
padding: 10px 20px;
|
30 |
+
text-align: center;
|
31 |
+
text-decoration: none;
|
32 |
+
font-size: 16px;
|
33 |
+
margin: 4px 2px;
|
34 |
+
cursor: pointer;
|
35 |
+
border-radius: 5px;
|
36 |
+
}
|
37 |
+
.stButton>button:hover {
|
38 |
+
background-color: #45a049;
|
39 |
+
}
|
40 |
+
textarea {
|
41 |
+
font-family: "Courier New", monospace;
|
42 |
+
font-size: 14px;
|
43 |
+
}
|
44 |
+
</style>
|
45 |
+
""",
|
46 |
+
unsafe_allow_html=True,
|
47 |
+
)
|
48 |
+
|
49 |
+
st.title("Advanced Streamlit IDE")
|
50 |
+
|
51 |
+
# Editor for writing Python code
|
52 |
+
st.subheader("Code Editor")
|
53 |
+
default_code = """import streamlit as st
|
54 |
|
55 |
+
st.title("Welcome to the Advanced Streamlit IDE!")
|
56 |
+
st.write("Start coding your Python project here.")"""
|
57 |
+
code = st.text_area("Write your Python code below:", value=default_code, height=300)
|
58 |
|
59 |
+
# Option to install packages
|
60 |
+
st.subheader("Install Python Packages")
|
61 |
+
packages = st.text_input("Enter package names separated by space (e.g., numpy pandas):")
|
62 |
|
63 |
+
if st.button("Install Packages"):
|
64 |
+
with st.spinner("Installing packages..."):
|
65 |
+
install_command = f"pip install --target={workspace_dir} {packages}"
|
66 |
+
install_output = run_shell_command(install_command)
|
67 |
+
st.text(install_output)
|
|
|
|
|
|
|
68 |
|
69 |
+
# Run the code in a temporary workspace
|
70 |
+
if st.button("Run Code"):
|
71 |
+
temp_file_path = os.path.join(workspace_dir, "temp_app.py")
|
72 |
+
with open(temp_file_path, "w") as temp_file:
|
73 |
+
temp_file.write(code)
|
74 |
+
|
75 |
+
with st.spinner("Running your code..."):
|
76 |
+
run_command = f"PYTHONPATH={workspace_dir} python {temp_file_path}"
|
77 |
+
run_output = run_shell_command(run_command)
|
78 |
+
st.text(run_output)
|
79 |
+
|
80 |
+
# Export code
|
81 |
+
if st.button("Export Code"):
|
82 |
+
temp_file_path = os.path.join(workspace_dir, "temp_app.py")
|
83 |
+
if os.path.exists(temp_file_path):
|
84 |
+
with open(temp_file_path, "r") as temp_file:
|
85 |
+
exported_code = temp_file.read()
|
86 |
+
st.download_button("Download Code", data=exported_code, file_name="exported_code.py", mime="text/plain")
|
87 |
else:
|
88 |
+
st.error("No code to export!")
|
89 |
+
|
90 |
+
# Cleanup workspace on session end
|
91 |
+
def cleanup_workspace():
|
92 |
+
if os.path.exists(workspace_dir):
|
93 |
+
shutil.rmtree(workspace_dir)
|
94 |
+
|
95 |
+
# Streamlit lifecycle hook for cleanup
|
96 |
+
st.on_event("teardown", cleanup_workspace)
|
97 |
|
98 |
+
st.markdown("---")
|
99 |
+
st.info(
|
100 |
+
"""
|
101 |
+
- Write and execute Python code in a temporary workspace.
|
102 |
+
- Install and use packages dynamically within your session.
|
103 |
+
- All data is deleted once you leave the site.
|
104 |
+
"""
|
105 |
+
)
|
|
|
|
|
|