Executing code in sanboxed environments

Executing code in sanboxed environments#

You can use sand-bob to execute code in sandboxed / containerized environments.

from sand_bob import execute
execute("""
for i in range(0, 10):
    print(i)
""")

Execution Output

0
1
2
3
4
5
6
7
8
9
---
kernelspec:
  name: python3
  display_name: python3
jupytext:
  text_representation:
    extension: .md
    format_name: myst
    format_version: '0.13'
    jupytext_version: 1.13.8
---

```{code-cell} ipython3

for i in range(0, 10):
    print(i)

```

Prompt

No prompt available

Execution Details

  • Final result: 9
  • Build Time: 0.62s
  • Run Time: 4.66s
  • Execution Time: 5.32s
  • Files:
    • /display_output/notebook_executed.ipynb
LLM backend
TaskFunctionModel
Generate codeprompt_scadsai_llmopenai/gpt-oss-120b
Fix codeprompt_scadsai_llmopenai/gpt-oss-120b
Determine dependenciesprompt_scadsai_llmopenai/gpt-oss-120b
Generate code feedbackprompt_scadsai_llmgoogle/gemma-4-26B-A4B-it
Summarize codeprompt_scadsai_llmopenai/gpt-oss-120b
Notebook conversionprompt_scadsai_llmopenai/gpt-oss-120b

If you use libraries, you need to specify the corresponding dependencies.

execute("""
import numpy as np

matrix = np.random.random((3,3))

print(matrix)
""", dependencies=["numpy"])

Execution Output

[[0.41337016 0.12724019 0.11536555]
 [0.26815116 0.94449088 0.81643972]
 [0.41359182 0.58590436 0.79020072]]
---
kernelspec:
  name: python3
  display_name: python3
jupytext:
  text_representation:
    extension: .md
    format_name: myst
    format_version: '0.13'
    jupytext_version: 1.13.8
---

```{code-cell} ipython3

import numpy as np

matrix = np.random.random((3,3))

print(matrix)

```

Prompt

No prompt available

Execution Details

  • Dependencies: numpy
  • Final result: [0.41359182 0.58590436 0.79020072]]
  • Build Time: 1.23s
  • Run Time: 4.12s
  • Execution Time: 5.39s
  • Files:
    • /display_output/notebook_executed.ipynb
LLM backend
TaskFunctionModel
Generate codeprompt_scadsai_llmopenai/gpt-oss-120b
Fix codeprompt_scadsai_llmopenai/gpt-oss-120b
Determine dependenciesprompt_scadsai_llmopenai/gpt-oss-120b
Generate code feedbackprompt_scadsai_llmgoogle/gemma-4-26B-A4B-it
Summarize codeprompt_scadsai_llmopenai/gpt-oss-120b
Notebook conversionprompt_scadsai_llmopenai/gpt-oss-120b

… otherwise you will see an error message.

execute("""
import numpy as np

matrix = np.random.random((3,3))

print(matrix)
""")

Error: ModuleNotFoundError

An error occurred while executing the following cell:
------------------
import numpy as np

matrix = np.random.random((3,3))

print(matrix)

------------------


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import numpy as np
      2 
      3 matrix = np.random.random((3,3))
      4 

ModuleNotFoundError: No module named 'numpy'

---
kernelspec:
  name: python3
  display_name: python3
jupytext:
  text_representation:
    extension: .md
    format_name: myst
    format_version: '0.13'
    jupytext_version: 1.13.8
---

```{code-cell} ipython3

import numpy as np

matrix = np.random.random((3,3))

print(matrix)

```

Prompt

No prompt available

Execution Details

  • Build Time: 0.00s
  • Run Time: 3.68s
  • Execution Time: 3.71s
  • Traceback:
    An error occurred while executing the following cell:
    ------------------
    import numpy as np
    
    matrix = np.random.random((3,3))
    
    print(matrix)
    
    ------------------
    
    
    ---------------------------------------------------------------------------
    ModuleNotFoundError                       Traceback (most recent call last)
    Cell In[1], line 1
    ----> 1 import numpy as np
          2 
          3 matrix = np.random.random((3,3))
          4 
    
    ModuleNotFoundError: No module named 'numpy'
    
    
LLM backend
TaskFunctionModel
Generate codeprompt_scadsai_llmopenai/gpt-oss-120b
Fix codeprompt_scadsai_llmopenai/gpt-oss-120b
Determine dependenciesprompt_scadsai_llmopenai/gpt-oss-120b
Generate code feedbackprompt_scadsai_llmgoogle/gemma-4-26B-A4B-it
Summarize codeprompt_scadsai_llmopenai/gpt-oss-120b
Notebook conversionprompt_scadsai_llmopenai/gpt-oss-120b