How to run a notebook using command line

I know the user can convert the notebook into a script and run python in the terminal.
Is there any way more convenient to do this task like python to run notebook.

Yes, you can directly run a notebook file from the command line. Two main ways I use are nbconvert and papermill. See the third paragraph here for links to documentation for each and some examples.

By the way since you mentioned .py files, jupytext can convert a script back to a notebook and run that notebook in one command, here. It also does the conversion from notebook to script on the command line, too.

Here’s an from example from the nbconvert 6.0 docs using the nbconvert api:

Thanks, I find the solution.

Executing notebooks from the command line
The same functionality of executing notebooks is exposed through a command line interface or a Python API interface. As an example, a notebook can be executed from the command line with:

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

Is it possible to just execute the notebook, but not generate an output file?
I’d like to just time the execution speed of a bunch of notebooks.

we have an issue to discuss and track this here, in case you’d like to weigh in:

1 Like

papermill {test_file_name.ipynb} /dev/null {args...} is a convenient way to do that

1 Like

@MSeal has papermill considered adding a --no-save flag, or something like this? For folks that are not as familiar with coding (or for folks who are on windows), /dev/null may be a bit of a stretch to remember and use. Happy to open an issue to discuss if you think it’d be helpful

The library is a little opinionated in that it expects when something goes wrong you want to read the output, so it hasn’t been made easy to drop the output. Another option btw is to make the output the same as the input (which will write in place) or to just output to a dummy output path. A --no-save flag could be a decent issue to discuss. The implementation would be a little difficult to make conditional on the --no-save flag since the click configuration as it it’s setup to always expect an input and an output path. Happy to discuss more in github if you wanted.

Thanks for all the feedback. I left a comment on