forked from jupyter-book/jupyter-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjupytext.Rmd
125 lines (94 loc) · 3.43 KB
/
jupytext.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
---
jupyter:
kernelspec:
display_name: Python
language: python
name: python3
---
(file-types:custom)=
# Custom notebook formats and Jupytext
You can designate additional file types to be converted to notebooks and then executed/parsed in the same manner as regular notebooks.
:::{tip}
This page itself is written as an [RMarkdown](https://rmarkdown.rstudio.com/) notebook!
:::
```yaml
sphinx:
config:
nb_custom_formats:
.mysuffix: mylibrary.converter_function
```
- The string should be a Python function that will be loaded by `import mylibrary.converter_function`
- The function should take a file's contents (as a `str`) and return an [nbformat.NotebookNode](nbformat:api)
If the function takes additional keyword arguments, then you can specify these as a dictionary in a second argument.
For example this is what the default conversion would look like:
```yaml
sphinx:
config:
nb_custom_formats:
.ipynb:
- nbformat.reads
- as_version: 4
```
:::{important}
By default, Markdown cells in the notebook will be parsed using the same MyST parser configuration as for other Markdown files.
But, if this is incompatible with your file format, then you can specify for the Markdown to be parsed as **strictly CommonMark**, using a third argument:
```yaml
sphinx:
config:
nb_custom_formats:
.ipynb:
- nbformat.reads
- as_version: 4
- true
```
:::
Finally, for text-based formats, MyST-NB also searches for an optional `source_map` key in the output notebook's metadata.
This key should be a list mapping each cell to the starting line number in the original source file, for example for a notebook with three cells:
```json
{
"metadata": {
"source_map": [10, 21, 53]
}
}
```
This mapping allows for "true" error reporting, as described in [](myst-nb:myst/error-reporting).
## Using Jupytext
[Jupytext](https://jupytext.readthedocs.io/en/latest/) is an excellent Python tool for two-way conversion
between Jupyter Notebook `.ipynb` files and
[a variety of text-based files](https://jupytext.readthedocs.io/en/latest/formats.html).
Jupyter Book natively supports the Jupytext file format: [notebooks with MyST Markdown](./myst-notebooks.md), but you can add other formats like [RMarkdown](https://rmarkdown.rstudio.com/) or Python files.
The configuration looks like:
```yaml
sphinx:
config:
nb_custom_formats:
.Rmd:
- jupytext.reads
- fmt: Rmd
```
:::{warning}
Note that some execution features (such as in-line code execution in RMarkdown) are not available in Jupyter Book.
:::
Now you can use RMarkdown blocks:
```{python echo=TRUE}
print("Hallo I'm an RMarkdown block!")
```
```{python echo=TRUE}
print("Hallo I'm an RMarkdown block!")
```
:::{important}
For full compatibility with `myst-parser`, it is necessary to use `jupytext>=1.6.0`.
:::
(file-types:custom:jupytext)=
## Convert a Jupytext file into a MyST notebook
Alternatively, if you'd like to convert your pre-existing Jupytext files into the MyST notebook format,
to use directly in your book, install Jupytext and then run the following command:
```bash
jupytext --to myst path/to/yourfile
```
Note that you may also pass a wildcard that will be used to convert multiple
files. For example:
```bash
jupytext --to myst ./*.py
```
See [the Jupytext CLI documentation](https://jupytext.readthedocs.io/en/latest/using-cli.html) for more information.