5

I am authoring a Jupyter notebook on my local machine that will eventually be run on a remote server (which is running Ubuntu). Every time I need to make a change I must export the notebook as a .py file and then call it from the command line of the server.

I'd like to be able to run this on the fly, calling one command that takes the current .ipynb file and executes it on the command line as if it were a .py, showing all the print statements and output you'd expect if the .py were run. I thought nbconverter might do the trick using something like the following command:

jupyter nbconvert --to script --execute nbconvert_test.ipynb

As it turnout, this does not convert the .ipynb to a .py file to be executed on the command line as I would like, but rather it creates a new file called nbconvert_test.py in the same directory which I would then have to run in a separate command. I'd really like to prevent the creation of that file every time I make even a small change, and to skip the extra step on the command line.

Any help is appreciated!

beniam
  • 89
  • 1
  • 2
  • 5
  • 2
    Possible duplicate of [Running .ipynb from terminal?](http://stackoverflow.com/questions/35545402/running-ipynb-from-terminal) – redacted Apr 25 '17 at 06:26
  • 1
    Thanks. Yes, that question is similar, but the answers provided to that question did not seem to interpret the request as looking for a solution in one line. – beniam Apr 25 '17 at 06:33
  • http://deeplearning.lipingyang.org/2018/03/28/run-jupyter-notebook-script-from-terminal/ – matanster Sep 03 '18 at 05:44

4 Answers4

7

You can send the jupyter nbconvert to stranded output and pipe that to python.

jupyter nbconvert --to script --execute --stdout test_nbconvert.ipynb | python
fizzyh2o
  • 1,237
  • 9
  • 18
2

A workaround is a small shell script that has three parts

  1. converting the notebook
  2. executing created script
  3. removing the script

create a file runnb.sh

#!/bin/sh
# First argument is the notebook you would like to run
notebook=$1
scriptname="$(basename $notebook .ipynb)".py

jupyter nbconvert --to script --execute ${notebook} && python ${scriptname}
rm ${scriptname}

use as such:

$ ./runnb.sh nbconvert_test.ipynb

EDIT: According to this answer, this command should do just fine jupyter nbconvert --execute test_nbconvert.ipynb (just leav out the --to flag

Community
  • 1
  • 1
redacted
  • 3,789
  • 6
  • 25
  • 38
  • Thanks Robin-I think without the --to flag it creates an html file in the same directory that includes the output, but doesn't fit the bill exactly. Your script in three lines may do the trick. I'll pay with that now...Thanks. – beniam Apr 25 '17 at 06:35
  • 1
    It doesn't work, it says xyz.py not found for rm command – Raymond Feb 16 '22 at 11:14
0

With the boar package, you can run your notebook within a python code, using:

from boar.running import run_notebook

outputs = run_notebook("nbconvert_test.ipynb")

For more information, see:

https://github.com/alexandreCameron/boar/blob/master/USAGE.md

Alex
  • 160
  • 11
0

I had a similar error xyz.py not found for rm command Then I ran the script inside the conda virtual environment and it worked.

conda activate
DOE
  • 1
  • 1
    You may want to direct this at Raymond's comment from 2016. As an aside, there's other modern ways to do this now in addition to `jupyter nbconvert`. One of my favorites is using [jupytext on the command line to execute a notebook](https://jupytext.readthedocs.io/en/latest/using-cli.html). There's also papermill, ploomber, and nbtoolbelt. See [here](https://discourse.jupyter.org/t/how-to-run-a-notebook-using-command-line/3475?u=fomightez) and [here](https://discourse.jupyter.org/t/run-ipynb-on-a-remote-workstation-so-that-all-results-will-be-save-in-the-same-pynb/13340/2?u=fomightez). – Wayne Apr 03 '22 at 20:17