Linux command
jupytext 命令
网络
复制后可按需替换文件名、目录或参数。
常用示例
Convert a notebook to a Python percent script
jupytext --to py:percent [notebook.ipynb]
Convert a Python script back to a notebook
jupytext --to ipynb [script.py]
Convert a notebook to Markdown and write to a specific output file
jupytext --to md --output [output.md] [notebook.ipynb]
Pair a notebook with a Python percent script for version control
jupytext --set-formats ipynb,py:percent [notebook.ipynb]
Sync all paired representations, updating whichever file is outdated
jupytext --sync [notebook.ipynb]
Pipe the text representation through black to reformat in place
jupytext --pipe black [notebook.ipynb]
Convert a Markdown notebook to ipynb and execute it
jupytext --to notebook --execute [notebook.md]
说明
Jupytext converts Jupyter notebooks to plain-text formats such as Python scripts and Markdown files, and back again. This makes notebooks suitable for version control, code review, and editing in a standard IDE. The most common workflow is to pair a `.ipynb` notebook with a `.py` or `.md` file using `--set-formats`. Once paired, running `--sync` keeps both files up to date: the paired text file captures inputs cleanly for version control, while the `.ipynb` retains cell outputs. The `percent` format (`py:percent`) is the recommended script format. Cells are delimited by `# %%` markers and the files are valid Python. The `light` format uses minimal markers. Markdown-based formats include plain `md`, `md:myst` (MyST Markdown, compatible with Jupyter Book), and `Rmd` (R Markdown). Jupytext integrates with JupyterLab as an extension: text notebooks open with a notebook icon and can be launched directly. It also works with pre-commit hooks for automated formatting.
参数
- --to _FORMAT_
- Target format. Common values: `py`, `py:percent`, `py:light`, `ipynb`, `md`, `md:myst`, `Rmd`. The part after `:` selects the sub-format.
- --from _FORMAT_
- Explicitly specify the source format, required when reading from stdin.
- -o _FILE_, --output _FILE_
- Output file path. Use `-` to write to stdout.
- --sync
- Sync all paired representations of a notebook based on file timestamps, updating whichever copy is outdated.
- --set-formats _FORMATS_
- Pair the notebook with one or more additional formats (e.g. `ipynb,py:percent`). Writes pairing metadata into the notebook.
- --update
- When converting to an existing `.ipynb` file, preserve existing outputs and metadata and only update the input cells.
- --pipe _CMD_
- Pipe the text representation of the notebook through an external command (e.g. `black` or `isort`). Use `{}` as a placeholder for a temporary file when the command does not accept stdin.
- --pipe-fmt _FORMAT_
- Format to use when piping (default: `py:percent`).
- --check _CMD_
- Run a command on the text representation and fail if it returns a non-zero exit code. Use `{}` as a placeholder for a temporary file.
- --execute
- Execute the notebook cells after conversion (requires `nbconvert`).
- --set-kernel _NAME_
- Set the kernel in the notebook YAML header. Use `-` to use the kernel matching the current Python executable.
- --update-metadata _JSON_
- Update notebook-level metadata with the provided JSON string (e.g. `'{"kernelspec":{"name":"python3"}}'`).
- --opt _KEY=VALUE_
- Set a jupytext option, such as `comment_magics=false`.
- --test
- Test that the round-trip conversion (notebook -> text -> notebook) is lossless.
- --test-strict
- Like `--test`, but performs a strict byte-for-byte comparison of the two notebooks.
- --help
- Display help information.
FAQ
What is the jupytext command used for?
Jupytext converts Jupyter notebooks to plain-text formats such as Python scripts and Markdown files, and back again. This makes notebooks suitable for version control, code review, and editing in a standard IDE. The most common workflow is to pair a `.ipynb` notebook with a `.py` or `.md` file using `--set-formats`. Once paired, running `--sync` keeps both files up to date: the paired text file captures inputs cleanly for version control, while the `.ipynb` retains cell outputs. The `percent` format (`py:percent`) is the recommended script format. Cells are delimited by `# %%` markers and the files are valid Python. The `light` format uses minimal markers. Markdown-based formats include plain `md`, `md:myst` (MyST Markdown, compatible with Jupyter Book), and `Rmd` (R Markdown). Jupytext integrates with JupyterLab as an extension: text notebooks open with a notebook icon and can be launched directly. It also works with pre-commit hooks for automated formatting.
How do I run a basic jupytext example?
Run `jupytext --to py:percent [notebook.ipynb]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --to _FORMAT_ do in jupytext?
Target format. Common values: `py`, `py:percent`, `py:light`, `ipynb`, `md`, `md:myst`, `Rmd`. The part after `:` selects the sub-format.