# GPU sessions from a notebook

> Launch a GPU machine from Jupyter with a maximum cost, get the SSH and JupyterLab links, and have it stopped when you are done.

The Python SDK's `nodelodge.notebook` module launches a Node Lodge Secure machine from a notebook cell, waits until it is ready, shows how to connect, and stops it when your block of work ends, even after an error or an interrupted cell.

## Set up

In a terminal, or in a notebook cell with `%pip`:

```bash
pip install ./sdk/python
```

Set your API key before starting Jupyter (the key needs the scopes `workloads:read` and `workloads:write`):

```bash
export NODELODGE_API_KEY=nl_live_...
```

Your SSH public key is read from `~/.ssh/id_ed25519.pub`. See [SSH keys](https://nodelodge.com/docs/ssh-keys).

## Launch, work, stop

```python
from nodelodge.notebook import gpu_session

with gpu_session(max_cost_usd=5, min_vram_gb=48) as gpu:
    display(gpu)
    input("Press Enter to stop the machine")
```

`display(gpu)` shows the machine, the most it can cost, the SSH command and the JupyterLab link. When the block ends, the session stops: billing stops and the machine and its disk are deleted.

- `max_cost_usd` is required. It is reserved on your credit as the budget of the session, which never costs more: billing follows the real time the machine runs. Add `hours=` to stop it after that many hours.
- Pick the machine with `gpu="H100"`, `min_vram_gb=48`, or an `offer_id=`. Without them, the cheapest bookable machine is used.
- If the machine does not start, or you interrupt the cell while it starts, the session is stopped before the error is shown.
- While the machine starts, each launch step is printed once, as it begins. With `launch(..., wait=False)`, call `gpu.wait_ready(on_step=print)` later: your function receives each new step (`allocated`, `booting`, `verifying`).

## JupyterLab on the machine

Every Node Lodge Secure machine runs JupyterLab, ready when the session is. Run `gpu.jupyter_command` in a terminal: it opens SSH with JupyterLab forwarded to port 8888 of your computer. While it runs, open `gpu.jupyter_url` in your browser. JupyterLab is reachable only through your SSH connection.

## Without a `with` block

```python
from nodelodge.notebook import launch

gpu = launch(max_cost_usd=5, gpu="A6000")
# ... later
gpu.stop()
```

A session you did not stop is stopped when the notebook kernel restarts or shuts down. Stopping it yourself, as soon as you are done, is what keeps the cost down: you pay for the time the session runs.

For every SDK method, see [CLI and SDK](https://nodelodge.com/docs/cli-and-sdk).
