Generate Jupyter notebook ".ipynb" in Python

How to generate a Jupyter notebook from Python.

import nbformat

nb = nbformat.v4.new_notebook()

title = "#title"

code = """
%matplotlib inline
from sympy import *
# to print with mathjax on jupyter notebook
init_printing()
"""

nb["cells"] = [
    nbformat.v4.new_markdown_cell(title),
    nbformat.v4.new_code_cell(code)
]

with open("output.ipynb", "w") as f:
    nbformat.write(nb, f)

With this, it seems that you can do various useful things.

Generate a custom .ipynb in PyCharm

PyCharm is a Python integrated development environment (IDE), which has very powerful complementary features and is the strongest Python development environment. In fact, PyCharm can work with Jupyter, and Jupyter's expressive power + PyCharm's powerful complementary function is quite comfortable.

メニュー_011.png

However, when dealing with mathematical formulas, PyCharm's Jupyter output is poor, so it is better to use the browser version of Jupyter. So I don't use PyCharm's Jupyter, instead I use ʻExtranal Tools`.

Create an .ipynb generation script

Create the following script somewhere that is easy to refer to.

#!/usr/bin/env python3
# coding:utf-8
import sys
import os
import nbformat

output_dir = sys.argv[1]
output_file = input("input ipynb name:")

nb = nbformat.v4.new_notebook()

title = "#title"

code = """
%matplotlib inline
from sympy import *
init_session()
"""

nb["cells"] = [
    nbformat.v4.new_markdown_cell(title),
    nbformat.v4.new_code_cell(code)
]

if not output_file.endswith(".ipynb"):
    output_file += ".ipynb"

with open(os.path.join(output_dir, output_file), "w") as f:
    nbformat.write(nb, f)

print("ok.")

Register as PyCharm's External Tool

Open PyCharm's File (F) menu and select Settings ...

メニュー_016.png

Select "Tools"-> "External Tools" in the settings dialog and press the "+" button.

Settings_017.png

The setting dialog will appear, so add the following items.

item input
Name Please enter a suitable name
Description Please enter an appropriate explanation
Program Python path where Jupyter is installed
Parameters The script path from earlier$FileDir$

Edit Tool_018.png

You can now generate .ipynb from PyCharm's right-click menu!

Select "External Toos"-> "Registered Tool Name" from the right-click menu of PyCharm.

メニュー_019.png

When you enter the file name in the console, various pre-filled .ipynb files will be generated from the beginning.

python35 - [~-PycharmProjects-python35] - ...-document-document.ipynb - PyCharm Community Edition 2016.3.2_020.png

reference

https://pypi.python.org/pypi/nbformat https://github.com/jupyter/nbformat

Recommended Posts

Generate Jupyter notebook ".ipynb" in Python
Memory leak in Python Jupyter Lab (Notebook)?
3 Jupyter notebook (Python) tricks
Generate rounded thumbnails in Python
Display HTML in Jupyter notebook
Multiprocessing error in Jupyter Notebook
Generate U distribution in Python
Generate QR code in Python
Generate 8 * 8 (64) cubes in Blender Python
python3.8 venv environment jupyter notebook
Convert jupyter notebook .ipynb files to python executable .py files
Snippet settings for python jupyter notebook
Python memo Anaconda x Jupyter Notebook
[Python] Generate QR code in memory
View graphs inline in Jupyter Notebook
Easy to use Jupyter notebook (Python3.5)
Git management of Jupyter notebook (ipynb) differences in easy-to-read with JupyterLab
Jupyter Notebook 6.0.2 cannot be installed in the Python 2.7 environment created in Anaconda
Generate a first class collection in Python
Generate AWS-S3 signed (time-limited) URLs in Python
Automatically generate Python Docstring Comment in Emacs
How to execute commands in jupyter notebook
Module to generate word N-gram in Python
Linking python and JavaScript with jupyter notebook
Generate a class from a string in Python
Generate C language from S-expressions in Python
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Jupyter Notebook memo
Introducing Jupyter Notebook
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Powerful Jupyter Notebook
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Bash in Jupyter
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Jupyter notebook password
Jupyter Notebook memo
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python