Edit this page on our live server and create a PR by running command !create-pr in the console panel

Uisng the markdown kernel

  • Difficulty level: easy
  • Time need to lean: 10 minutes or less

Markdown cell and markdown kernel

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.

In [1]:

Hello, this is a code cell in markdown kernel, not a markdown cell.

%expand in cells with a Markdown kernel

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,

In [2]:

You can write use it in Python expressions as follows:

In [3]:

The Fibonacci sequence has value 1 when n=1 and 55 when n=10, which can be calculated recursively by fibo(10)=fibo(9) + fib(8)=34+21, and so on.

%expand using expressions in non-SoS kernels

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,

In [4]:
In [5]:

We generated a random array of length 4, ranging from -0.960540217119432 to 0.6641938545656

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.

In [6]:

We generated a random array of length 4, ranging from -0.960540217119432 to 0.6641938545656