Can anyone advise how to get nbconvert to work with a notebook that uses a bash kernel?

Hi all. This is my first post… I hope it hits the mark.
I have created two notebooks to demonstrate a problem with nbconvert.

  1. ConvertMePython.ipynb uses a python kernel and contains the markdown cell:
    This notebook is running a python kernel. Next is print("Hello World!")
    followed by the code cell
    print("Hello World!")
  2. ConvertMebash.ipynb uses a bash kernel and contains the markdown cell:
    This notebook is running a bashkernel. Next is printf "Hello World!\n"
    followed by the code cell
    printf "Hello World!\n"

nbconvert works perfectly with the first notebook

> jupyter nbconvert --execute --to html ConvertMePython.ipynb 
[NbConvertApp] Converting notebook ConvertMePython.ipynb to html
[NbConvertApp] Writing 575045 bytes to ConvertMePython.html

…but fails with the second:

jupyter nbconvert --execute --to html ConvertMeBash.ipynb 
[NbConvertApp] Converting notebook ConvertMeBash.ipynb to html
Traceback (most recent call last):
  File "/opt/conda/bin/jupyter-nbconvert", line 10, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.9/site-packages/jupyter_core/application.py", line 269, in launch_instance
    return super().launch_instance(argv=argv, **kwargs)

#... lines deleted for brevity...

  File "/opt/conda/lib/python3.9/site-packages/nbclient/client.py", line 919, in _check_raise_for_error
    raise CellExecutionError.from_cell_and_msg(cell, exec_reply_content)
nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell:
------------------
printf 'Hello World!\n'
------------------


: 1


I can get nbconvert to render the second notebook with the following command line

> jupyter nbconvert --execute --allow-errors --to html ConvertMeBash.ipynb 
[NbConvertApp] Converting notebook ConvertMeBash.ipynb to html
[NbConvertApp] Writing 575224 bytes to ConvertMeBash.html

however, the resulting HTML looks like this:
image

I’ve searched for a similar issue, but to no avail. Can anyone advise how to get nbconvert to work with a notebook that uses a bash kernel?

Well, I cannot solve this for you but I have a hunch. Probably nbconvert checks for the success of the commands by looking at the return/exit code and the logic for bash is flawed? Or maybe it is using the wrong shell (bash vs. sh vs. …)? Have you checked for the exact implementation used?

When I ran the success test at Reading the return value (status) of a command - Linux Shell Scripting Cookbook - Third Edition [Book] (only replacing $CMD with $@, small error on their side), the exit value of your printf command just seemed right. In your script, however, it shows : 1. It is possible that that is the actual return code of your command?

3 Likes

Great suggestion. Thank you… so I tried commenting out the bash command and…

> jupyter nbconvert --execute --to html ConvertMeBash.ipynb 
[NbConvertApp] Converting notebook ConvertMeBash.ipynb to html
Traceback (most recent call last):
  File "/opt/conda/bin/jupyter-nbconvert", line 10, in <module>
    sys.exit(main())

#... lines deleted for brevity...
  File "/opt/conda/lib/python3.9/site-packages/nbclient/client.py", line 919, in _check_raise_for_error
    raise CellExecutionError.from_cell_and_msg(cell, exec_reply_content)
nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell:
------------------
#printf 'Hello World!\n'
------------------


: 1

… so even a shell comment causes issues. Which makes me think something deep and troubling is afoot.

Well, that looks really odd. Because in case a Python kernel was accidentially used, the comment should be just right as well. Maybe the kernel that is connected with the notebook is somehow broken or not available in the environment you use the nbconvert command in?

1 Like