alibidaran commited on
Commit
74b92f4
·
verified ·
1 Parent(s): bb52d78

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +20 -6
src/streamlit_app.py CHANGED
@@ -19,6 +19,8 @@ import time
19
  import openai
20
  import dotenv
21
  import os
 
 
22
  dotenv.load_dotenv()
23
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
24
 
@@ -126,18 +128,19 @@ st.sidebar.subheader("📦 Install Python Library")
126
  with st.sidebar.form("install_lib_form"):
127
  lib_name = st.text_input("Library name (e.g., numpy, pandas)")
128
  install_btn = st.form_submit_button("Install with pip")
 
129
  if install_btn and lib_name.strip():
130
  with st.spinner(f"Installing {lib_name}..."):
131
  try:
132
- # Define target directory for installation
133
- target_dir = "/.local/lib/python3.9/site-packages"
134
  os.makedirs(target_dir, exist_ok=True)
135
 
136
- # Add to sys.path so imports work immediately
137
  if target_dir not in sys.path:
138
  sys.path.insert(0, target_dir)
139
 
140
- # Run pip install into target dir
141
  result = subprocess.run(
142
  [
143
  sys.executable, "-m", "pip", "install",
@@ -147,10 +150,21 @@ with st.sidebar.form("install_lib_form"):
147
  ],
148
  capture_output=True, text=True
149
  )
 
150
  if result.returncode == 0:
151
- st.success(f"Successfully installed `{lib_name}`.")
 
 
 
 
 
 
 
 
 
152
  else:
153
- st.error(f"Error installing `{lib_name}`:\n{result.stderr}")
 
154
  except Exception as e:
155
  st.error(f"Exception: {e}")
156
  st.title("🧠 Python Code Generator & Runner")
 
19
  import openai
20
  import dotenv
21
  import os
22
+ import site
23
+
24
  dotenv.load_dotenv()
25
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
26
 
 
128
  with st.sidebar.form("install_lib_form"):
129
  lib_name = st.text_input("Library name (e.g., numpy, pandas)")
130
  install_btn = st.form_submit_button("Install with pip")
131
+
132
  if install_btn and lib_name.strip():
133
  with st.spinner(f"Installing {lib_name}..."):
134
  try:
135
+ # Use user site-packages (safe, writable)
136
+ target_dir = site.USER_SITE
137
  os.makedirs(target_dir, exist_ok=True)
138
 
139
+ # Add target_dir to sys.path so imports work
140
  if target_dir not in sys.path:
141
  sys.path.insert(0, target_dir)
142
 
143
+ # Install package
144
  result = subprocess.run(
145
  [
146
  sys.executable, "-m", "pip", "install",
 
150
  ],
151
  capture_output=True, text=True
152
  )
153
+
154
  if result.returncode == 0:
155
+ st.success(f"Successfully installed `{lib_name}`.")
156
+
157
+ # Try importing immediately
158
+ try:
159
+ module = importlib.import_module(lib_name)
160
+ importlib.reload(module)
161
+ st.info(f"📦 `{lib_name}` is now ready to use!")
162
+ except Exception as e:
163
+ st.warning(f"Installed but could not import: {e}")
164
+
165
  else:
166
+ st.error(f"Error installing `{lib_name}`:\n{result.stderr}")
167
+
168
  except Exception as e:
169
  st.error(f"Exception: {e}")
170
  st.title("🧠 Python Code Generator & Runner")