Jupyter-book: code-cell inside a "special block" in a markdown cell of Jupyter Notebook

Created on 8 Jun 2020  路  10Comments  路  Source: executablebooks/jupyter-book

I tried to execute a code inside a "special block" in a markdown cell in Jupyter Notebook:

`` {tip}
Here is a sentence.
```{code-cell} python3
z = 100

Here is another sentence.
````
``````

It generates an error:

WARNING: Unknown directive type "code-cell".
```
Is it an expected result?

Most helpful comment

The following worked!

Place the following in a markdown cell:
`` {tip}
Here is a sentence.

print("Hello World!")

{glue:}glued_txt
````
``````

Place the following in a code cell with a tag remove-cell:

from myst_nb import glue
my_variable = "Hello World!"
glue("glued_txt", my_variable)

All 10 comments

Yep - unfortunately you can't put code cells inside of things like tips :-/

I think we should have code-cell return a more informative error though

There's a workaround if you'd like to include a code-cell in a directive which does not support it. I have discussed it in issue #671. Check it out and let us know if it is helpful.

@choldgraf I think that would be useful. We can also include an example of the workaround in the documentation if you think that will help the users.

@najuzilu though doesn't that only work for the toggle directive?

@choldgraf it actually works for the admonition directives as well. Here's the example I tested:

`` {tip}
Here is a sentence.

print("Hello World!")

{glue:}glued_txt
````

% The code cell below is not displayed
```{code-cell} ipython3
:tags: [remove-cell]

from myst_nb import glue
my_variable = "Hello World!"
glue("glued_txt", my_variable)
```
``````
This is what it looks like:

Screen Shot 2020-06-09 at 10 27 19 AM

ahhh right! I missed that part of your explanation, good point 馃憤

maybe we can add an extra section here: https://jupyterbook.org/content/content-blocks.html#notes-warnings-and-other-admonitions like "how to insert code outputs into your admonition blocks" and show a short example, then cross-link to the glue docs?

@najuzilu
I inserted your code in a markdown cell of a Jupyter Notebook, and

jb clean . --all
jb build .

but the following error messages were generated:

WARNING: Unknown directive type "code-cell".

WARNING: Couldn't find key `glued_txt` in keys defined across all pages.

What am I missing here?

myst-nb: 0.8.1
jupyter-book: 0.7.0b4
Python: 3.8.2

@choldgraf would glue work on a Jupyter Notebook? I work primarily with .md files.

well, if you're working in a notebook, then you shouldn't use {code-cell}, you should just write code in the notebook like you normally would (and put glue() in that code).

The following worked!

Place the following in a markdown cell:
`` {tip}
Here is a sentence.

print("Hello World!")

{glue:}glued_txt
````
``````

Place the following in a code cell with a tag remove-cell:

from myst_nb import glue
my_variable = "Hello World!"
glue("glued_txt", my_variable)

yes, that's it! Perhaps we can add the note about remove_cell to the docs here: https://jupyterbook.org/content/content-blocks.html#insert-code-cell-outputs-into-admonitions ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

muzny picture muzny  路  4Comments

cedeerwe picture cedeerwe  路  3Comments

choldgraf picture choldgraf  路  3Comments

nozebacle picture nozebacle  路  3Comments

choldgraf picture choldgraf  路  4Comments