Файли
openidea/docs/en/guide/notebook.mdx
T
LU XIANGRUIтаwildcatDownstairs 4ff5739854 feat(notebook): show the running kernel's variables in a Jupyter Variables tool window (#6517)
A Jupyter Variables tool window (bottom, under the stripe's overflow until first opened; also in Find Action) lists the variables of the last active notebook's kernel: name, type and a truncated repr, hiding private names, modules and IPython's own names. It refreshes after a run, a kernel restart and a switch of notebook, and never starts a kernel.

The variables are read with a silent execute_request carrying a user expression (no output, history, execution count or busy/idle status), only while the kernel is idle; a run started meanwhile waits for it, bounded to 10 seconds. A notebook tab restored at startup picks up its session once it opens.

Closes #6508

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-27 23:02:19 +08:00

113 рядки
7.9 KiB
Plaintext
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
---
title: "Notebooks"
description: "Open, edit and run Jupyter notebooks (.ipynb) with a local Python kernel."
---
OpenIDEA opens `.ipynb` files in a notebook editor. You can edit code and Markdown cells, run them on a Python kernel that the IDE starts for you, and save the result back to the same file.
## Requirements
- A Python interpreter with **ipykernel** installed (`pip install ipykernel`). OpenIDEA never installs packages for you. If ipykernel is missing, the editor shows an error with the hint _Install ipykernel in the selected Python environment, then restart the kernel_.
- A trusted project. Running cells is blocked while the project is in Restricted Mode.
- Notebooks in nbformat 4. Other versions are refused and the file is left untouched.
## Open a notebook
Open any `.ipynb` file from the Project tool window, Go to File or the command line. It opens in the notebook editor instead of as raw JSON.
The toolbar at the top of the editor has:
| Control | What it does |
| ------------------------------- | -------------------------------------------------------------------------------------- |
| **Run Cell** | Runs the selected code cell. |
| **Run All** | Runs every code cell from top to bottom. Execution stops at the first cell that fails. |
| **Interrupt Kernel** | Interrupts the cell that is running. |
| **Restart Kernel** | Restarts the kernel and clears its state. |
| Interpreter list | The Python interpreter for this notebook (**Select Python interpreter**). |
| **Refresh Python interpreters** | Detects Python environments again. |
| Kernel status | `starting`, `idle`, `busy` or `dead`. |
| **Undo** / **Redo** / **Save** | Notebook-level undo, redo and save. |
| **Reload Notebook from Disk** | Discards unsaved changes, stops execution and reloads the file. |
## Choose the Python interpreter
The interpreter list shows the Python environments OpenIDEA detects, plus the project interpreter. If you don't pick one, the notebook uses the project's Python interpreter, or the first detected environment.
Your choice is remembered per notebook. Change it while no cell is running.
<Tip>
For a virtual environment, select its own `python` (for example `.venv/bin/python`). OpenIDEA does not resolve the
symlink to the base interpreter, so the kernel runs inside the environment.
</Tip>
Each open notebook gets its own kernel process. Closing the notebook shuts its kernel down.
## Edit cells
Every cell has a small toolbar:
- **Cell type** list: **Code**, **Markdown** or **Raw**.
- **Run Cell** (code cells), or **Edit Markdown** / **Preview Markdown** (Markdown cells). Double-click a rendered Markdown cell to edit it.
- **Move Cell Up**, **Move Cell Down**, **Insert Code Cell Below**, **Delete Cell**.
Use **Add Code Cell** and **Add Markdown Cell** at the end of the notebook to append cells.
Code cells show the execution count (`[3]`) next to the cell toolbar. Raw cells and cell types OpenIDEA doesn't know are shown read-only; their content is kept as it is.
## Code intelligence
Code cells get completion, quick documentation on hover, parameter info and error highlighting from the project's Python language server, as in a `.py` file. The server reads the code cells in order, so names defined in one cell are known in the cells below it, and IPython syntax such as `%matplotlib inline` or `!pip list` isn't reported as an error.
`⌘`-click (macOS) or `Ctrl`-click (Windows/Linux) a name to go to its declaration, in another cell or in a library.
This needs a language server that supports notebooks: **basedpyright** (the default) and **Ruff** do. With other Python servers, code cells only get syntax highlighting.
## Keyboard
| Action | Keys |
| -------------------------- | --------------------------------------------------------------------------- |
| Run the selected code cell | `⇧Enter` or `⌃Enter` (macOS), `Shift+Enter` or `Ctrl+Enter` (Windows/Linux) |
| Save | `⌘S` / `Ctrl+S` |
| Undo | `⌘Z` / `Ctrl+Z` |
| Redo | `⌘⇧Z` / `Ctrl+Shift+Z` or `Ctrl+Y` |
These keys are handled by the notebook editor itself and are not part of the keymap settings.
## Outputs
Outputs are written into the notebook file when you save, the same way Jupyter does.
| Output | How it is shown |
| ------------------------------------------ | ----------------------------------------------------------------------- |
| `stdout` / `stderr` streams | Text with ANSI colours; `stderr` is highlighted. |
| Errors | The traceback. |
| `image/png`, `image/jpeg`, `image/svg+xml` | Inline image. |
| `text/html` | Static HTML in a sandbox: no scripts, no network, only embedded images. |
| `text/plain` | Text. |
| `application/javascript` | Shown as source, not run. |
| Other MIME types | A notice; the data is kept in the file. |
## Convert to a Python script
Right-click a `.ipynb` file in the Project tool window and choose **Convert to Python File** (or run it from Find Action while the notebook is open). OpenIDEA writes `<name>.py` next to the notebook (`<name>-1.py` if that name is taken) and opens it. The notebook itself is not changed; an open notebook is converted with its unsaved edits.
The script uses `# %%` cell markers, the format VS Code and Jupytext read:
- Code cells are copied as they are. IPython magics and shell lines (`%matplotlib inline`, `!pip list`) are commented out, and so is a whole cell that starts with a cell magic such as `%%bash`, so the script runs with plain `python`.
- Markdown cells become `# %% [markdown]` followed by commented lines; raw cells become `# %% [raw]`.
- Outputs are not written to the script.
**Convert to Jupyter Notebook** on a `.py` file does the reverse: it writes `<name>.ipynb` with a Python 3 kernel and opens it. Each `# %%` (or `#%%`) line starts a cell, `[markdown]` and `[raw]` cells lose their `# ` prefix, and a file without markers becomes a single code cell. Lines commented out on export stay comments.
## Jupyter Variables
**View › Tool Windows › Jupyter Variables** (or **Jupyter Variables** in Find Action) lists the variables of the last active notebook's kernel: name, type and a short value. It refreshes when a run finishes, after a kernel restart and when you switch to another notebook. Names starting with `_`, modules and the names IPython defines itself are not shown.
The list is read from the kernel without a visible run: it adds no output, history entry or execution count. It never starts a kernel; run a cell first.
## Limits
- Only Python kernels are supported. Kernels for other languages and remote Jupyter servers are not.
- Interactive widgets (ipywidgets) and JavaScript outputs do not run.
- Undo and redo are not available while cells are running.