Stream stdout
def on_stdout(message):
print(message.line, end="")
execution = machine.code_interpreter.run_code(
"for i in range(3): print(f'line {i}')",
language="python",
on_stdout=on_stdout,
envs={"RUN_MODE": "docs"},
timeout=30,
)
const execution = await machine.code.run(
"for i in range(3): print(f'line {i}')",
{
language: "python",
onStdout: (message) => process.stdout.write(message.line),
envs: { RUN_MODE: "docs" },
timeout: 30,
},
);
nullspace machine code run mch_123 "for i in range(3): print(f'line {i}')" \
--language python --env RUN_MODE=docs --timeout 30
# 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": "for i in range(3): print(f'\''line {i}'\'')", "language": "python", "envs": {"RUN_MODE": "docs"}, "timeout": 30}'
Stream results and errors
def on_result(result):
print("result", result.text)
def on_error(error):
print(error.name, error.value)
machine.code_interpreter.run_code(
"1 + 1",
on_result=on_result,
on_error=on_error,
)
await machine.code.run("1 + 1", {
onResult: (result) => console.log("result", result.text),
onError: (error) => console.log(error.name, error.value),
});
nullspace machine code run mch_123 "1 + 1"
# result and error events arrive on the 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": "1 + 1"}'
Stream artifacts
def on_artifact(artifact):
print(artifact.id, artifact.kind)
machine.code_interpreter.run_code(
"from IPython.display import SVG\nSVG(data='<svg></svg>')",
on_artifact=on_artifact,
)
await machine.code.run(
"from IPython.display import SVG\nSVG(data='<svg></svg>')",
{ onArtifact: (artifact) => console.log(artifact.id, artifact.kind) },
);
nullspace machine code run mch_123 "from IPython.display import SVG
SVG(data='<svg></svg>')"
# artifact events arrive on the 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": "from IPython.display import SVG\nSVG(data=\"<svg></svg>\")"}'
machine.run_code() is a convenience wrapper for standard output, standard
error, results, artifacts, errors, language, environment, context, and timeout
callbacks.