How to create and open a jupyter notebook ipynb file directly from terminal

Open notebook in browser

jupyter notebook <notebook>.ipynb

Create empty, minimal notebook:

"""create-notebook.py
   Creates a minimal jupyter notebook (.ipynb)
   Usage: create-notebook <notebook>
"""
import sys
from notebook import transutils as _
from notebook.services.contents.filemanager import FileContentsManager as FCM

try:
    notebook_fname = sys.argv[1].strip('.ipynb')
except IndexError:
    print("Usage: create-notebook <notebook>")
    exit()

notebook_fname += '.ipynb'  # ensure .ipynb suffix is added
FCM().new(path=notebook_fname)

Alias create-notebook script:

alias create-notebook='python $(pwd)/create-notebook.py'

Putting it all together

create-notebook my_notebook && jupyter notebook my_notebook.ipynb


This can't be the most desirable method. Maybe there's an option native to Jupyter or the extensions, but I haven't come across one, nor have I come across the need to do this. The documentation suggests the developers are encouraging users to land at the dashboard.

Start with an empty notebook Untitled.ipynb. To generate it, save the default file that is created when you create a new notebook from the jupyter dashboard. This empty notebook will be used as a template for creating new, empty notebooks at the command line. The contents of Untitled.ipynb for me, jupyter version 4.4.0, look like this:

$ cat Untitled.ipynb
{
 "cells": [],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}

The file contains the bare minimum needed to launch a notebook using jupyter notebook Untitled.ipynb (and eventually mynotebook.ipynb), any less and it will raise a NotJSONError. You can add some metadata to the template if you want to include a default kernel.

From here, use command substitution to open a new, empty notebook from the command line where Untitled.ipynb is the path to the template notebook created above and mynotebook.ipynb is the name of the notebook you wish to create:

$ jupyter notebook $(cat Untitled.ipynb >mynotebook.ipynb && echo mynotebook.ipynb)

You may try below command.

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

According to Jupyter nbconvert manual below, nbconvert with --execute command supports running a notebook.

enter image description here Hope it works for you.


I had the same pain point and created nbplot to fix it: https://github.com/nburrus/nbplot . It was designed to quickly plot files in a notebook from the command line, but overall it's just a tiny tool to generate notebooks from templates and open them in the browser. Here is how you would use it with the included empty template to answer your question:

pip3 install --upgrade nbplot
nbplot -t empty -o mynotebook.ipynb

It will try to be smart about reusing existing notebook servers instead of always starting a new one, and it's easy to add custom notebook templates to ~/.nbplot/ if you don't want to start with an empty notebook.