Creating startup files for jupyter notebooks in binder

Hi,
I am very new to binder, so please forgive this very newbie question. I do have Jupyter notebook up and running on mybinder, which is exciting! But, I would like to refine it a bit.

My overall goal is to create a web page with interactive matplotlib graphics and animations that will be as easy as possible for users to use. Although it may seem trivial, I would like users to not even have to execute a command already in the notebook when they open it. If this was a notebook on a local machine, I could put a script in the startup directory, which I have identified as:
/home/jovyan/.ipython/profile_default/startup
Is there a way that I can add a file to this directory, or otherwise control the jupyter configuration on binder?
Thanks in advance for any suggestions.
David

We have config files called postBuild and start that are basically bash scripts that could be used to perform tweaks to the environment before your user arrives.

https://repo2docker.readthedocs.io/en/latest/config_files.html#postbuild-run-code-after-installing-the-environment

I would also look into voila for generating the webpage from the Notebook

https://voila.readthedocs.io/en/stable/deploy.html#deployment-on-binder

3 Likes

Sarah,
Thanks very much. So just so I am clear, do I add a file called postBuild to my repository, with a bash script that creates the directory structure
/home/jovyan/.ipython/profile_default/startup
and then put my python startup script there? What directory does the bash script run from?

I will definitely look into Voila. My plan had been to follow the approach described here:


But, it looks like Voila might be simpler.
Thanks again,
David

There is also Jupyter Book which uses Sphinx to host the static webpages and supports mybinder buttons for interactivity as well. They’ve just undergone a huge rebuild and its great to use https://blog.jupyter.org/announcing-the-new-jupyter-book-cbf7aa8bc72e

As far as the postBuild script goes, yes just add it to your repo and put in the commands you’d like to run in it. I’m not sure which directory it executes in though so may require some trial and error to get working.

1 Like

I think that postBuild runs relative to \home\jovyan.

If you want to set persistent environment variables that are available in the MyBinder container, or run commands on startup, you need to do this in the start file, which should take the form:

#!/bin/bash

# set environment variables
# ...

# execute run commands
# ...

exec "$@"

Thanks for these suggestions. I am able to get a postBuild file to work and to create a startup python script, which runs when a notebook is opened. The catch is that the output of the script isn’t displayed in the notebook. After a bit digging, I understand that is just the way things are when commands are executed upon opening a notebook. It’s fine for loading modules or setting the notebook configuration, but not for what I need.

So, I’ve been working with Voila for a bit, on my local machine. This looks very nice, but I am running into a problem there, too. I can make ipywidgets appear and work, but matplotlib output doesn’t appear.

I will start a new thread on the Voila problem.

Thanks again,
David