Create .ipynb from scratch (generic)

PHOTO EMBED

Thu Dec 29 2022 07:25:04 GMT+0000 (Coordinated Universal Time)

Saved by @sfull

import json

# Create a list of cells
cells = [
    # Markdown cell
    {
        "cell_type": "markdown",
        "metadata": {},
        "source": [
            "# My IPython Notebook\n",
            "This is a simple notebook with a Markdown cell and a code cell.\n",
        ]
    },
    # Code cell
    {
        "cell_type": "code",
        "metadata": {},
        "source": [
            "x = 1\n",
            "y = 2\n",
            "print(x + y)"
        ]
    }
]

# Create the notebook structure
notebook = {
    "cells": cells,
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.8.6"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 2
}

# Write the notebook to a file
with open("my_notebook.ipynb", "w") as f:
    json.dump(notebook, f)
content_copyCOPY