- Difficulty level: easy
- Time need to lean: 10 minutes or less
You can include headers, lists, figures, tables in your Jupyter notebook using markdown cells. These markdown cells are rendered by Jupyter itself and do not interact with the kernels. Consequently, it is not possible to pass information (e.g. results from analysis) to markdown cells to generate dynamic output. In contrast, RStudio/RMarkdown has long allowed the inclusion of expressions in markdown texts.
To overcome this problem, you can install a markdown kernel with commands
pip install markdown-kernel
python -m markdown.kernel install
and write markdown code in code cells with a markdown kernel.
Hello, this is a **code cell in markdown kernel**, not a markdown cell.
The significance of the markdown kernel is that you can pass information from SoS to it through the %expand
magic. For example, suppose you have defined a function to calculate Fibonacci sequence,
def fibo(n):
return n if n <= 1 else (fibo(n-1) + fibo(n-2))
You can write use it in Python expressions as follows:
The Fibonacci sequence has value {fibo(1)} when `n=1` and {fibo(10)}
when `n=10`, which can be calculated recursively by
`fibo(10)=fibo(9) + fib(8)={fibo(9)}+{fibo(8)}`, and so on.
The --in
option of magic %expand
allows you to expand the cell content in specific subkernel, if its language module supports the expand protocol. This allows the content in markdown cells to be expanded in other languages such as R.
For example,
res
res <- rnorm(4)
--in R
We generated a random array of length {length(res)}, ranging from {min(res)} to {max(res)}
If you have a block of text from RStudio in RMarkdown format, you can specify the Rmarkdown-style delimiters to evaluate the RMarkdown text directly.
`r ` --in R
We generated a random array of length `r length(res)`, ranging from `r min(res)` to `r max(res)`