Start and auto execute a notebook from a commandline

2,274 views
Skip to first unread message

Ondřej Čertík

unread,
Mar 4, 2016, 7:52:17 PM3/4/16
to jup...@googlegroups.com
Hi,

I think I would like a feature that I can just execute:

jupyter notebook mynb.ipynb

and this would open a terminal window and load the notebook (it does
this already) *and* execute it automatically (it does not do this
currently). I am fine if this is an option like:

jupyter notebook --execute mynb.ipynb

(I could then always create some kind of a bash shortcut for this.)

That way this will be a full replacement for a python script to
generate a plot in a pop up window (which never really worked for me
anyway -- I save them to a png) or gnuplot, where you can just launch
one command from a terminal and it will do the plots given the data
files.

My workflow is that I generate the data files using some computational
code (typically written in Fortran or C++). I just want to check some
plots. It's a hassle to do

1) jupyter notebook mynb.ipynb
2) wait 4s (I measured it, this includes opening a browser,
initializing a kernel etc.)
3) go to Cell -> Run All.
4) wait another few seconds to load matplotlib and do the plots

Much rather, I would just do:

1) jupyter notebook --execute mynb.ipynb
2) Do something else until the plots pop up.


Is there some way to do this currently? I haven't figured out a way so far.

If it is not there, would somebody please give me a few pointers how
this should be implemented? I'll be happy to implement this and send a
PR.

Thanks,
Ondrej

Erik Stephens

unread,
Mar 6, 2016, 11:38:45 AM3/6/16
to Project Jupyter
I think nbconvert (http://nbconvert.readthedocs.org/en/latest/) will meet most, if not all, of your requirements.

--
Erik

MinRK

unread,
Mar 6, 2016, 12:32:13 PM3/6/16
to jup...@googlegroups.com
The full command to execute a notebook and save the result overwriting the original is:

    jupyter nbconvert --execute --inplace notebook.ipynb

-MinRK

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/7cd1fc74-a338-4c23-9544-d2e864e9e1dd%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Ondřej Čertík

unread,
Mar 7, 2016, 10:43:05 AM3/7/16
to jup...@googlegroups.com
Hi Min and Erik,

On Sun, Mar 6, 2016 at 10:31 AM, MinRK <benja...@gmail.com> wrote:
> The full command to execute a notebook and save the result overwriting the
> original is:
>
> jupyter nbconvert --execute --inplace notebook.ipynb

Actually, it is:

jupyter nbconvert --execute --inplace --to notebook notebook.ipynb

otherwise it just produces `notebook.html` (with or without
--inplace). This indeed runs the notebook from a command line, which I
was also trying to figure out in order to test my notebooks
automatically (and I checked that if there is an exception, that it
will fail).

However, the problem is that I can't edit the plots afterwards. The
closest to what I want is:

jupyter nbconvert --execute --inplace --to notebook notebook.ipynb &&
jupyter notebook notebook.ipynb

When it opens the notebook, the plots are indeed current. But I still
have to rerun the notebook if I want to modify the plots.

One solution would be to leave the kernel from the "jupyter nbconvert"
command running, and then reconnect to it using the "jupyer notebook"
command. The other solution would be to implement "--execute" into the
"jupyter notebook" command itself.

Ondrej
> https://groups.google.com/d/msgid/jupyter/CAHNn8BWO_y1q7oeaDrQN7mmj7u0YXhOJKO2a6ajtqbfBAE1d1g%40mail.gmail.com.

MinRK

unread,
Mar 7, 2016, 11:25:01 AM3/7/16
to jup...@googlegroups.com
On Mon, Mar 7, 2016 at 4:43 PM, Ondřej Čertík <ondrej...@gmail.com> wrote:
Hi Min and Erik,

On Sun, Mar 6, 2016 at 10:31 AM, MinRK <benja...@gmail.com> wrote:
> The full command to execute a notebook and save the result overwriting the
> original is:
>
>     jupyter nbconvert --execute --inplace notebook.ipynb

Actually, it is:

jupyter nbconvert --execute --inplace --to notebook notebook.ipynb

otherwise it just produces `notebook.html` (with or without
--inplace). This indeed runs the notebook from a command line, which I
was also trying to figure out in order to test my notebooks
automatically (and I checked that if there is an exception, that it
will fail).

These are bugs in nbconvert that are fixed in master. I forgot that we haven't released since those fixes.
 

However, the problem is that I can't edit the plots afterwards. The
closest to what I want is:

jupyter nbconvert --execute --inplace --to notebook notebook.ipynb &&
jupyter notebook notebook.ipynb

When it opens the notebook, the plots are indeed current. But I still
have to rerun the notebook if I want to modify the plots.

One solution would be to leave the kernel from the "jupyter nbconvert"
command running, and then reconnect to it using the "jupyer notebook"
command. The other solution would be to implement "--execute" into the
"jupyter notebook" command itself.

Gotcha, something more analogous to `python -i`. I'm sure that's possible, but it probably wouldn't be pretty to implement in the current notebook application.

-MinRK


 

Ondřej Čertík

unread,
Mar 7, 2016, 11:46:06 AM3/7/16
to jup...@googlegroups.com
That's exactly right.

Another option is to essentially allow to trigger an action once the
notebook is loaded, in this case the action is "Cell -> Run All".

I haven't used gnuplot for probably good 15 years, but I just saw a
colleague using it to investigate some graphs from a running
simulation, and my notebook workflow is *so* slow compared to his,
because it takes forever to launch and execute the notebook, before I
can interactively start playing with the result.

Ondrej
Reply all
Reply to author
Forward
0 new messages