bash_kernel icon indicating copy to clipboard operation
bash_kernel copied to clipboard

run another notebook

Open parmentelat opened this issue 6 years ago • 8 comments

is there an easy way to have notebook2 run (like in source) the contents of notebook1 ?

my use case is rather simple; I have a rather long git tutorial, that I'd like to cut into pieces; of course ideally each notebook should be able to control the status of the git repo at the beginning, which can easily done by running the previous pieces

I remember I have done that with python notebooks occasionally, although it was admittedly abstruse; but well, asking does not cost anything, does it ? would be of tremendous help !

parmentelat avatar May 02 '19 14:05 parmentelat

If you want them to run to write files and things, you can always run them from bash with something like jupyter nbconvert --execute.

If you want to run them and have variables etc. left in the bash namespace, I don't think there's currently a way to do that.

takluyver avatar May 02 '19 15:05 takluyver

I was exploring this approach; I could manually convert a notebook into a bash file using the classic notebook UI and 'download file as' from the menubar; trying to automate further I was considering some sort of make-based approach to produce the .sh files out-of-band, but jupyter nbconvert --to bash does not seem to be an option, what am I missing ?

thanks !

parmentelat avatar May 02 '19 15:05 parmentelat

--to script is the generic name for 'convert this notebook to a standard source code file for its kernel language'.

takluyver avatar May 02 '19 15:05 takluyver

Oooook ! thanks a lot

parmentelat avatar May 02 '19 15:05 parmentelat

You're welcome. I'd also be open to adding a function in bash_kernel called something like source-notebook which would take another notebook file, and run its code in the current kernel. I haven't looked into how much work that would involve, though.

takluyver avatar May 02 '19 17:05 takluyver

a quick status on my end for the record:
at this point I have a simple makefile that updates one .sh file per notebook;
the .sh need to go into the git repo of course (I'm using s/t in the spirit of mybinder)

I can then invoke said .sh file with a formula like

bash 01-my-first-repo.sh >& /dev/null
  • redirection is optional, it's just confusing to receive all the inputs without the markdown cells
  • I'm invoking bash because I was too lazy to have the makefile do the chmod +x

parmentelat avatar May 02 '19 19:05 parmentelat

where should I start looking if I wanted to dig around your idea of a source-notebook ?

parmentelat avatar May 02 '19 19:05 parmentelat

If you're running the scripts with bash (rather than source) afterwards, that should have a similar effect to running the notebooks with jupyter nbconvert --execute. It's probably a bit faster, though.

For source-notebook, I can see two ways to implement it:

  • Write something (probably a bit of embedded Python to run from bash) that extracts the code from a notebook to a temp file, and then source that.
  • Pass the notebook path back to the Python kernel code somehow, and have it extract the cells and run them separately.

takluyver avatar May 03 '19 07:05 takluyver