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

PythonTerminalJupyter Notebook

Python Problem Overview


The command jupyter notebook will open the Jupyter directory tree page where you can create a ipynb file.

Is there a way to skip that page and create and open the ipynb file directly on the browser?

I was thinking something like jupyter notebook mynotebook.ipynb

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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)

Solution 3 - Python

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.

Solution 4 - Python

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.

Solution 5 - Python

Jupyter will start running on Tornado server on localhost. The link is something like http://localhost/Tree When you open a notebook, this is done in another page. You can try to write a batch script to call kupyter notebok, then call your browser with the address to your notebook. I take it that if the notebook doesn't exist, is not created, then it will not work (the page is note created, it cannot be opened).

Solution 6 - Python

You can create a new notebook from the command line using jupytext:

# create python file
touch foo.py

# add kernel information
jupytext --set-kernel - foo.py

# convert to notebook
jupytext --to notebook foo.py

# open in browser
jupyter notebook foo.ipynb

https://jupytext.readthedocs.io/en/latest/using-cli.html


These commands could be wrapped in a shell script named something like jupyinit:

#!/bin/bash
# Create a new Jupyter notebook from the command line
# 
# Examples
# jupyinit env_name py_file.py py_file.ipynb

touch $2
jupytext --set-kernel $1 $2
jupytext --to notebook --execute $2
jupytext --set-formats ipynb,py $3

The OP's example could then be created with:

$ jupyinit myenv mynotebook.py mynotebook.ipynb

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmultigoodverseView Question on Stackoverflow
Solution 1 - PythonstacksonstacksView Answer on Stackoverflow
Solution 2 - PythonKevinView Answer on Stackoverflow
Solution 3 - PythonthewaywewereView Answer on Stackoverflow
Solution 4 - PythonnburrusView Answer on Stackoverflow
Solution 5 - PythonPetronellaView Answer on Stackoverflow
Solution 6 - PythonjstaView Answer on Stackoverflow