> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ns.rocks/llms.txt
> Use this file to discover all available pages before exploring further.

# Overview

> Analyze data with stateful notebook-style code in a machine.

Use the `code-interpreter` template when you want a hosted notebook-like runtime
with persistent kernel state, installed data libraries, rich results, and file
artifacts.

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  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)
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  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();
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  nullspace machine code run mch_123 "import pandas as pd

  df = pd.DataFrame({'x': [1, 2, 3], 'y': [1, 4, 9]})
  df.describe()"
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  # 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()"}'
  ```
</CodeGroup>

Code cells can share state through a context:

<CodeGroup>
  ```python Python SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  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)
  ```

  ```typescript TypeScript SDK theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  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);
  ```

  ```bash CLI theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  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>
  ```

  ```bash HTTP API theme={"theme":{"light":"one-light","dark":"one-dark-pro"}}
  # 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>"}'
  ```
</CodeGroup>

## Related

* [Pre-installed libraries](./pre-installed-libraries)
* [Code contexts](./contexts)
* [Runs and artifacts](./runs-artifacts)
