Pass parameters to a notebook on jupyterhub/binderhub

I’ve been brainstorming with a few others on ways to link JupyterHub/BinderHub with a web-based data repository (in this case https://idr.openmicroscopy.org/ though the pattern is useful for other repositories). For example:

  1. User finds a dataset or item of interest in the repository
  2. That item is associated with one or more recommended notebooks.
  3. User clicks a mybinder.org link associated with the item that launches the notebook, configured to run the analysis on the selected item. In the case of IDR this link would be in the right-hand pane of the dataset browser along with other metadata associated with the dataset or image.

Assume the notebook in step 3 is generic and can work with multiple datasets/items- e.g. a notebook to calculate image statistics, or to fetch metadata, can be run on any image. It therefore needs a parameter identifying the image to analyse.

A user can obviously edit the parameter, or jupyter-widgets can be used to request input, but this requires the user to copy and paste an ID from the data browser to the notebook in mybinder.

What would be really nice is to have a way to pass those parameters as a URL query. For example, instead of

https://mybinder.org/v2/gist/manics/421f2927bb1dbdbc00754a7669eb3f69/master?filepath=idr-image.ipynb

pass the image id as:

https://mybinder.org/v2/gist/manics/421f2927bb1dbdbc00754a7669eb3f69/master?filepath=idr-image.ipynb&IMAGE_ID=2959331

or perhaps as json to avoid clashing with other params:

https://mybinder.org/v2/gist/manics/421f2927bb1dbdbc00754a7669eb3f69/master?filepath=idr-image.ipynb&notebookparams={"IMAGE_ID":2959331}

These links would be auto-generated for each item.

It’s partly related to what Papermill can do on the CLI, but in the browser. Is the best way to implement this a Jupyter notebook/lab extension, or are there better ways?

There is a bunch of thoughts on a related but different topic in https://github.com/jupyterhub/binderhub/issues/712 which also has a lot of links/issues referring to it.

If I understand you correctly you want to add parameters for the notebook, not the spawner (or BinderHub itself). This is what makes it a bit different from the issue I linked.

There are (at least?) two things to solve:

  • recognise that the query parameters aren’t meant for the spawner and pass them on
  • use a notebook to extract them from the final URL

No good idea right now but wanted to link the existing discussion.


One exciting but likely impractical idea: run a git repository server where https://gitrepos.idr.example.com/<datasetID> spits out a repo that is the same for all datasets except for the notebooks that it contains and what the dataset parameters are set to in the notebooks. Then your BinderHub link would refer to the repo for that dataset. (I have no idea if you can generate git repositories “on the fly” … that is why it is exciting and impractical :slight_smile: )

2 Likes

I managed to bodge a plugin based on nbgitpuller:

If you go to the app /urlparams and pass two query parameters paramsfile=config.json and params={"a":1,"b":false} it writes the file, then redirects you to urlpath

Example on mybinder:

Double escpaing the query params for BinderHub makes this difficult to hand-craft URLs, it would be nice if BinderHub could process just the parameters it recognises and pass the remainder unchanged to the app. nbgitpuller has the same problem.

Another snippet on this topic posted by @minrk on gitter earlier today:

Try: https://notebooks.gesis.org/binder/v2/gh/gesiscss/smm_demo/master?urlpath=%2Fnotebooks%2Fpython%2Fpolitician%2Ftwitter.ipynb%3Fparam%3Dvalue

filepath is okay, and usually better, because it encodes the location of the file within the repo, which binder then resolves to a url. This doesn’t, however, allow specifying url-specific things like params, so you have to fallback on urlpath if you want to do url things. Any and all url things should be supported, though. The url for a notebook is /notebooks/ not /tree/, and apparently the redirect from /tree to /notebooks doesn’t preserve params, which is why your link isn’t working.

2 Likes

Thanks! That works very well for Jupyter notebooks and the code looks relatively simple. The autorun=true option is pretty neat too!

I had a quick look at the JupyterLab extension docs (first time I’ve looked into extensions), am I correct in thinking this sort of extension would be quite complicated because the notebook isn’t loaded directly, instead it goes via JupyterLab?