Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -172,33 +172,47 @@ def run_notebook_cell(notebook_name: str, cell_number: int) -> str:
|
|
| 172 |
notebook_path = NOTEBOOKS_DIR / notebook_name
|
| 173 |
|
| 174 |
if not notebook_path.exists():
|
| 175 |
-
return f"Error: Notebook not found"
|
| 176 |
|
| 177 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
with open(notebook_path, 'r') as f:
|
| 179 |
nb = json.load(f)
|
| 180 |
|
| 181 |
cells = [c for c in nb.get('cells', []) if c.get('cell_type') == 'code']
|
| 182 |
|
| 183 |
if cell_number < 1 or cell_number > len(cells):
|
|
|
|
| 184 |
return f"Error: Cell {cell_number} not found. Available: 1-{len(cells)}"
|
| 185 |
|
| 186 |
cell = cells[cell_number - 1]
|
| 187 |
source = ''.join(cell.get('source', []))
|
| 188 |
|
| 189 |
-
# Execute the code
|
| 190 |
import io
|
| 191 |
from contextlib import redirect_stdout, redirect_stderr
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
stdout_capture = io.StringIO()
|
| 194 |
stderr_capture = io.StringIO()
|
| 195 |
|
| 196 |
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
|
| 197 |
try:
|
| 198 |
-
exec(source,
|
| 199 |
except Exception as e:
|
|
|
|
| 200 |
return f"Error: {str(e)}"
|
| 201 |
|
|
|
|
|
|
|
| 202 |
output = stdout_capture.getvalue()
|
| 203 |
errors = stderr_capture.getvalue()
|
| 204 |
|
|
@@ -213,6 +227,10 @@ def run_notebook_cell(notebook_name: str, cell_number: int) -> str:
|
|
| 213 |
return result
|
| 214 |
|
| 215 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
return f"Error: {str(e)}"
|
| 217 |
|
| 218 |
# ============================================================================
|
|
|
|
| 172 |
notebook_path = NOTEBOOKS_DIR / notebook_name
|
| 173 |
|
| 174 |
if not notebook_path.exists():
|
| 175 |
+
return f"Error: Notebook not found at {notebook_path}"
|
| 176 |
|
| 177 |
try:
|
| 178 |
+
# Change to notebooks directory so relative paths work
|
| 179 |
+
original_cwd = os.getcwd()
|
| 180 |
+
os.chdir(NOTEBOOKS_DIR)
|
| 181 |
+
|
| 182 |
with open(notebook_path, 'r') as f:
|
| 183 |
nb = json.load(f)
|
| 184 |
|
| 185 |
cells = [c for c in nb.get('cells', []) if c.get('cell_type') == 'code']
|
| 186 |
|
| 187 |
if cell_number < 1 or cell_number > len(cells):
|
| 188 |
+
os.chdir(original_cwd)
|
| 189 |
return f"Error: Cell {cell_number} not found. Available: 1-{len(cells)}"
|
| 190 |
|
| 191 |
cell = cells[cell_number - 1]
|
| 192 |
source = ''.join(cell.get('source', []))
|
| 193 |
|
| 194 |
+
# Execute the code with proper namespace
|
| 195 |
import io
|
| 196 |
from contextlib import redirect_stdout, redirect_stderr
|
| 197 |
|
| 198 |
+
# Create a namespace with common imports
|
| 199 |
+
namespace = {
|
| 200 |
+
'__name__': '__main__',
|
| 201 |
+
'__file__': str(notebook_path),
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
stdout_capture = io.StringIO()
|
| 205 |
stderr_capture = io.StringIO()
|
| 206 |
|
| 207 |
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
|
| 208 |
try:
|
| 209 |
+
exec(source, namespace)
|
| 210 |
except Exception as e:
|
| 211 |
+
os.chdir(original_cwd)
|
| 212 |
return f"Error: {str(e)}"
|
| 213 |
|
| 214 |
+
os.chdir(original_cwd)
|
| 215 |
+
|
| 216 |
output = stdout_capture.getvalue()
|
| 217 |
errors = stderr_capture.getvalue()
|
| 218 |
|
|
|
|
| 227 |
return result
|
| 228 |
|
| 229 |
except Exception as e:
|
| 230 |
+
try:
|
| 231 |
+
os.chdir(original_cwd)
|
| 232 |
+
except:
|
| 233 |
+
pass
|
| 234 |
return f"Error: {str(e)}"
|
| 235 |
|
| 236 |
# ============================================================================
|