Ask Your Question

scripting (jupyter) notebooks

asked 2017-03-09 04:27:32 -0500

Is there a way to execute a notebook file from the command line and automatically run all cells?

I am looking for something like

sage -n jupyter --no-browser --script test1.ipynb 

however, --script is deprecated and --no-browser doesn't stop sage from opening a browser window for me. Worse yet, the cells in the notebook are not executed automatically either.

Background: My aim is to automatically run a set of different sage notebooks (which are basically scripts to generate data files) in a directory, similar to how you would compile various source files to binaries. The notebooks are a collaborative effort and in a version control system and every developer would be able to generate those data files locally on their machine by running some shell script or through a Makefile.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2017-03-09 18:04:20 -0500

updated 2017-03-10 02:33:11 -0500

This is at least one possibility

$ sage -sh
$ jupyter convert --to='python' --output-dir=/tmp test1.ipynb
$ sage /tmp/

However, I am not sure how much the conversion is reliable. Why not storing your scripts as python files instead of notebooks?

edit flag offensive delete link more


I guess jupyter bconvert should be replaced with jupyter bconvert

tmonteil gravatar imagetmonteil ( 2017-03-10 01:12:37 -0500 )edit

Excellent, that is great. It is nbconvert instead of convert for me, but that seems to do the trick. It does not seem to convert expressions like x^2 to x**2, but I can live with that. In fact, I now see there's an --execute option to nbconvert which (aside from producing e.g. a python file) is what I wanted in the first place.

To answer your question: I see the notebooks as "source code", as everything else can be generated from them, like the python files as you suggest. But then working with the python files is not as nice as with the notebooks, so I'll keep the notebooks under version control instead.

Björn gravatar imageBjörn ( 2017-03-10 03:09:31 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-03-09 04:27:32 -0500

Seen: 73 times

Last updated: Mar 10