дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 17:21:13 +02:00
Ctrl+Enter (also Cmd+Enter on macOS) runs the notebook cell and keeps the caret in it, and Shift+Enter runs it and selects the next cell, adding a code cell at the end of the notebook. Run Cell, Run Cell and Select Below and Run Cell and Insert Below are actions in Find Action and the keymap settings; they take these keys only while the notebook has focus (notebookFocus), ahead of the editor's Split Line / Start New Line, and the notebook's own key handler is gone. Markdown cells render instead of running; while a cell runs, the keys do nothing on a code cell. Alt+Enter stays Show Intentions, so Insert Below has no default key except in the VS Code keymaps. Closes #7624 Co-authored-by: ntbang0901 <[email protected]>
143 рядки
11 KiB
Plaintext
143 рядки
11 KiB
Plaintext
---
|
||
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.
|
||
|
||
## Run on a Jupyter server
|
||
|
||
To run a notebook where the data or the GPU is, choose **Connect to Jupyter Server…** at the bottom of the interpreter list (or run it from Find Action) and enter:
|
||
|
||
- **URL**: the server's address, for example `http://localhost:8888/`, or a JupyterHub user server such as `https://hub.example.com/user/alice/`. A URL copied from JupyterLab, with `/lab` or `?token=…`, works too.
|
||
- **Token**: the server's token, or a JupyterHub API token. Not needed when the URL carries `?token=`.
|
||
|
||
OpenIDEA checks them against the server, keeps the token in the system credential store and lists the server's kernels in every notebook's interpreter list. The choice is remembered per notebook, like an interpreter.
|
||
|
||
Running, interrupting and restarting work as with a local kernel. OpenIDEA starts a kernel on the server for the notebook and shuts it down when you close the notebook. Only the source of the cells you run is sent; the notebook file stays on your computer and is saved there. If the connection to the server drops, OpenIDEA reconnects to the same kernel for about 15 seconds before giving up. The kernel's connection goes through the HTTP proxy of **Settings › Appearance & Behavior › System Settings › HTTP Proxy**, like the server's other requests. Signing in to JupyterHub through the browser is not supported; create an API token on the Hub's Token page instead.
|
||
|
||
To forget a server and its token, choose **Remove Jupyter Server…** in the interpreter list or in Find Action.
|
||
|
||
## 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 Cell | `⌘Enter` or `⌃Enter` (macOS), `Ctrl+Enter` (Windows/Linux) |
|
||
| Run Cell and Select Below | `⇧Enter` / `Shift+Enter` |
|
||
| Run Cell and Insert Below | None by default; `⌥Enter` / `Alt+Enter` in the VS Code keymap |
|
||
| Edit the selected cell | `Enter` |
|
||
| Save | `⌘S` / `Ctrl+S` |
|
||
| Undo | `⌘Z` / `Ctrl+Z` |
|
||
| Redo | `⌘⇧Z` / `Ctrl+Shift+Z` or `Ctrl+Y` |
|
||
|
||
**Run Cell** keeps the caret where it is. **Run Cell and Select Below** selects the next cell without editing it, so you can press it again to step through the notebook; at the last cell it adds a new code cell and starts editing it. **Run Cell and Insert Below** always adds a code cell. On a Markdown cell the three actions render its text instead of running it. While a cell is running, a code cell can't be run again.
|
||
|
||
You can change the keys of the run actions in **Settings › Keymap** (search for _Run Cell_), and run them from Find Action. The other keys are handled by the notebook editor itself.
|
||
|
||
## 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.
|
||
|
||
## Debug a cell
|
||
|
||
Click a code cell's gutter to set a breakpoint, as in a file. Then choose **Debug Cell** in the notebook toolbar, the cell's toolbar (the bug icon) or Find Action. The cell runs on the notebook's kernel under the kernel's own debugger:
|
||
|
||
- It stops at breakpoints in that cell and in the cells whose functions it calls.
|
||
- The **Debug** tool window shows the frames and variables. Stepping, Evaluate Expression and the inline values work as in a Python debug session, and a stop opens its cell at the line.
|
||
- The session ends when the cell finishes. **Stop** interrupts the cell; the kernel and its variables are kept.
|
||
|
||
This works the same for a kernel on a Jupyter server, and for the kernel chosen in the interpreter list even before it has run. It needs a Python kernel with ipykernel 6 or later and `debugpy` installed: for another kernel (an older ipykernel, R, Julia), Debug Cell starts it and then says it cannot debug.
|
||
|
||
A breakpoint goes to the cell's current code. In a cell edited since it last ran, it stops only once the cell runs again. When two cells have the same code, only the first one's breakpoints are used.
|
||
|
||
## Limits
|
||
|
||
- Local kernels are Python only. A Jupyter server's other kernels can run cells, but code intelligence is for Python.
|
||
- Interactive widgets (ipywidgets) and JavaScript outputs do not run.
|
||
- Undo and redo are not available while cells are running.
|