Skill Scalable Prototype- Run all cells and go directly to notebook

I am working on a skill scalable prototype. The idea is users with no coding experience can explore a data pipeline while users with some coding can get into the code and update it. Based on some feedback I have found no coders turn off if they see the code up front and will stop at initial obstacles so each cell has a “hide code” and “run code” button.

I have the following binder which walk users through a data pipeline for crop yields.
Open crop yield location interactive or crop yield regional interactive

Two Questions:

  1. The challenge is I need binder to run the first cell on start up which I did with some html code in the first cell. This works on a local jupyter instance where it saves the last state of the notebook but not when a user opens up the binder image. Is there anyway to make this work?

  2. I would like a binder link to each notebook so it just opens when clicked but every time I try to link directly to the file I get a 404 error. Is there anyway to resolve this?

1: The most reliable way is to write a notebook extension. For example I’ve got an extension that automatically runs all notebook cells if ?autorun=true is in the URL query (it also does other stuff):

Relevant bit of code:

Though note I’m definitely not an expert in writing extensions, so there may be a better way to do this. In any case you should be able to find a way to execute just the first cell.

Edit: It looks like your first cell is executed automatically when run locally because your notebook is trusted, by default notebooks are untrusted since executing arbitrary HTML/Javascript is a security risk:


2: Linking directly to a mybinder notebook is not as easy as copying and pasting the URL, because each launch on mybinder gets a different URL to the running server. Instead you need to take the URL for launching the binder repo, and and a ?filepath= or ?urlpath=

E.g. https://mybinder.org/v2/gh/projectmesadata/cropyield/c1e3df46799ca8e3cf9afc5491fcc7a0251efb4b?filepath=Crop%20Yield%20-%20Data%20Download%20(must%20run%20first)-Interactive.ipynb launches a new binder instance and loads Crop Yield - Data Download (must run first)-Interactive.ipynb

mybinder has some extensions to make this easier, if you’ve opened a notebook clicking the Binder button should give you a shareable URL:

image

2 Likes

@manics Awesome, thanks. I am definitely a novice at js and extensions. Quick question based on your edit, my understanding is the code will never run on start up and must wait on the user to run it…

But couldn’t @Tom_Pike use a postBuild to set the notebook to trusted? Then maybe the first cell would run? Or No?

1 Like

Thinking about it, you might be right: Security in the Jupyter notebook server — Jupyter Notebook 6.1.5 documentation
The database containing details of trusted notebooks is completely separate from the notebooks, but if the database is present in the built image it might work?

1 Like

There is an example of setting it to trusted here as described here. I still don’t know if that will allow it to run when opened but I was able to open interactive content upon reopening previously saved notebooks that way.
An important aspect of a postBuild file is to copy one from a working source and then edit locally and not on the Github browser-based site. If you do it via the browser the permissions seem to get messed up and it doesn’t get run.

2 Likes

@fomightez and @manics Thanks to both of you I learned a ton!

The final product in action

1 Like

@Tom_Pike Great, thanks for sharing!