Jupyter-book: Thebelab kernel not changing between languages when browsing between pages

Created on 6 Feb 2019  路  6Comments  路  Source: executablebooks/jupyter-book

Original post/problem below.

There is currently a bug that causes the Thebelab kernel config setting (kernel_name) to not be refreshed/changes relative to the initial page the user accessed the Jupyter Book on.

This issue was discovered (for me) because the initial landing page in the template (content/intro.md) is not an ipynb, so when the (_build/intro.md) file is built, the kernel_name config value is not set. Due to a recent contribution, pages with no kernel_name set in their configs start Thebelab using python3. So for me, since all my notebooks were not using the python3 kernel (I was using SoS) , every user landing on the home page first were not using the right kernel to run the Jupyter Notebooks later one when browsing the other pages. A page reload would fix this issue (if on a page that was build using an ipynb), presumably because this would clear the cache and initiate a new Thebelab session (this time, on a page that has the proper kernel_name setup).

(original post)

I'm not sure which tool is the culprit here, so I'll start by posting my issue here at the top and work my way down.

I'm using Jupyter Book, the Thebelab feature, MyBinder, a Dockerfile, and the Jupyter Notebooks are running on the Script of Scripts kernel for multi-language support.

This issue is reproducible, I've done it several times in a row. It happens for other UX "pathways", not all, but for the sake of simplicity I will describe the simplest here.

The gist of the issue is that, the first time a user (that's me!) loads a Jupyter Book chapter, then tries to run a Thebelab cell, the magic command at the top of the cell (used/required by the Script of Scripts Jupyter kernel to indicate which language needs to be used for that cell, e.g. %use octave, %use sos (actually means python3)) is either not executed, not recognized, or something else. Or, the jupyter book isn't opened using the SoS kernel. This causes an error (for my case, since the first cell is some Octave/Matlab code, the an error is thrown I think because it still thinks it's in a python environment and can't read the Matlab syntax).

HOWEVER, if I simply immediately reload the web page, and follow the same steps, the kernel is loaded fine, and the cells execute without issue.

I took two videos of me reproducing this issue:

Video 1: https://www.youtube.com/watch?v=aeiB_LB2Q6w&feature=youtu.be

Video 2: https://www.youtube.com/watch?v=P0B9Zqr886Y&feature=youtu.be

Note that, I don't encounter this behaviour if I try running the cells within a separate MyBinder session (not Thebelab).

To replicate the steps exactly:

1 - Start at the landing page : https://qmrlab.org/t1_book
2 - Click the "Signal Modelling" page under "Inversion Recovery"
3 - Click the "Interact Inline" button.
4 - Go down to the first cell (starts with %use octave), and click run
5 - (optional) Since the first cell isn't supposed to have any output and only the second one does, click run on the second cell too (starts with %use sos)
6 - You should have encountered errors for both cells.
7 - Immediately reload the page.
8 - Follow steps 3-5, and a plotly figure should appear after the second cell after a minute or so.

Repo: https://github.com/qMRLab/t1_book
Github is setup to generate the site off of the gh-pages branch: https://github.com/qMRLab/t1_book/tree/gh-pages

bug

Most helpful comment

hmmm that's not good! I feel like each page should re-set its own kernel_name depending on the metadata so I think you've found a bug :-) could you re-name the issue and add a little explanation of the current problem to your first comment?

All 6 comments

I just recorded a third video, this time with a Javascript console open: https://youtu.be/h9s6JPnvXxo

I'm not a web dev, so this wasn't my first instinct, but should have been.

Looks like when I try to execute a cell before my Binder docker image (which has the SoS tools installed) isn't done launching, that Thebelab is maybe short-circuiting it and connecting to it's default (python only) server/kernel because mine isn't ready yet? Does that make sense?

Let me know if you would classify this as a Thebelab issue, and if so I'll repost it there.

SOLVED.

Issue was that, since the landing page (intro.md) wasn't/isn't generated from a Jupyter Notebook (written in markdown from scratch), it didn't have the kernel_name property set. This means that if the user first encounters this page, then kernel_name in the thebelab config will be set to python3 (because of the line above), and this parameter does not get set to another name if/when you click on a page that was generated by a python notebook (e.g. it doesn't change to sos during this session). However, if I reload the page, then the thebelab config is set up again, and since I'm on a page that has a kernel_name property (to sos), then it works fine.

Fix: Replace intro.md with makrdown cells in a Jupyter Notebook using the same kernel as my other notebooks (for my case, sos), make clean then make book.

Just as a heads up, this means that if people have different kernel_names between chapters (e.g. one in python and one in R), I suspect this might create the same issue for them (but I haven't tested it).

I can add a note about this in the FAQ, but it smells like something that might need a fix (e.g. kernel_name should be set anytime a new page/chapter is clicked).

hmmm that's not good! I feel like each page should re-set its own kernel_name depending on the metadata so I think you've found a bug :-) could you re-name the issue and add a little explanation of the current problem to your first comment?

I can confirm this. I encountered the same issue in a book that uses both Python and Octave notebooks. Sometimes the Octave notebooks load the Python kernel, and sometimes the Python notebooks load the Octave kernel.

I am not a web developer so I am not sure what is going on... I just figured that the kernel was cached by my browser somehow. It is not a big deal for me, because a reload of the page is an easy fix, and eventually the book I am working on will exclusively use one language, namely Octave.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TomDonoghue picture TomDonoghue  路  4Comments

muzny picture muzny  路  4Comments

spring-haru picture spring-haru  路  5Comments

matrs picture matrs  路  3Comments

AakashGfude picture AakashGfude  路  4Comments