code-interpreter template when you want a hosted notebook-like runtime
with persistent kernel state, installed data libraries, rich results, and file
artifacts.
from nullspace import Machine
with Machine.create(template="code-interpreter") as machine:
result = machine.run_code("""
import pandas as pd
df = pd.DataFrame({"x": [1, 2, 3], "y": [1, 4, 9]})
df.describe()
""")
print(result.results[0].text)
import { Machine } from "@nullspace/sdk";
const machine = await Machine.create({ template: "code-interpreter" });
const result = await machine.code.run(`
import pandas as pd
df = pd.DataFrame({"x": [1, 2, 3], "y": [1, 4, 9]})
df.describe()
`);
console.log(result.results[0].text);
await machine.destroy();
nullspace machine code run mch_123 "import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3], 'y': [1, 4, 9]})
df.describe()"
# returns a Server-Sent Events stream of JSON events
curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
-H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
-H "Content-Type: application/json" \
-d '{"code": "import pandas as pd\ndf = pd.DataFrame({\"x\": [1, 2, 3], \"y\": [1, 4, 9]})\ndf.describe()"}'
ctx = machine.code_interpreter.create_code_context(language="python", cwd="/workspace")
machine.code_interpreter.run_code("value = 21", context=ctx)
result = machine.code_interpreter.run_code("value * 2", context=ctx)
print(result.results[0].text)
const ctx = await machine.code.createContext({ language: "python", cwd: "/workspace" });
await machine.code.run("value = 21", { context: ctx });
const result = await machine.code.run("value * 2", { context: ctx });
console.log(result.results[0].text);
nullspace machine code context create mch_123 --language python --cwd /workspace
# use the returned context id below
nullspace machine code run mch_123 "value = 21" --context <context-id>
nullspace machine code run mch_123 "value * 2" --context <context-id>
# create the context
curl -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/contexts" \
-H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
-H "Content-Type: application/json" \
-d '{"language": "python", "cwd": "/workspace"}'
# then run against the returned context id (SSE stream)
curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
-H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
-H "Content-Type: application/json" \
-d '{"code": "value = 21", "context_id": "<context-id>"}'
curl -N -X POST "${NULLSPACE_API_URL}/v1/machines/mch_123/code/execute" \
-H "Authorization: Bearer ${NULLSPACE_API_KEY}" \
-H "Content-Type: application/json" \
-d '{"code": "value * 2", "context_id": "<context-id>"}'