Ipyparams returns None for query params when using 'Run All' cells

I am using ipyparams to read params from query. I can read them when I run one cell at a time. But if I choose to run all cells then I get None for params.

for a url like: https://xyz.com/hub/user-redirect/notebooks/QueryParamsTest.ipynb?id=457112880

I read from a cell as:

import ipyparams
id = ipyparams.params['id']

I get values for id if I click Run button for the cell but not if choose to run all cells using Run All. Has anybody found an alternative to this problem? I could move all cells together and use Run button but breaking them up in multiple cells keeps my output neat. Also, pressing Run for each cell is painful.

The way ipyparams works is by setting up a comm (the underlying mechanism for interactive Widgets) when it is imported, and the params are delivered back to the kernel asynchronously. As a result, it takes a finite amount of time between when ipyparams is imported until the params becomes available.

The reason this doesn’t work when you use run all is because all cell executions are already queued on the kernel before the comm is setup and begins trying to send the params back to the kernel.

A more robust (but also much more complex!) approach would be to use a JupyterLab extension to deliver additional info with each execution message in the message metadata. But this could not be setup from within the notebook itself.

The quickest way to get this to work is going to be to run the import ipyparams in a first cell by hand. After that cell has completed, Run All should work fine. So you can still use Run All, you just have to do one step first.

ipyparams also makes a less-than-robust choice to run this setup at import time rather than via a repeatable function call, so if you re-run the import cell, it won’t set up the necessary client-side javascript any time after the first.

@minrk thanks for your reply. is there a specific JupyterLab extension that I can use? Also, do you mean I should be using JupyterLab instead of JupyterHub?

sorry, JupyterLab and JupyterHub are orthogonal choices. You can use both, or you can use the classic notebook. But if you’re going to write an extension to solve this problem, a JupyterLab extension is probably the way to go.

If you are using the classic (notebook<7) UI, you can perhaps get this going with a snippet of custom javascript, but that’s proven a challenge to maintain in the long run.