Home > Back-end >  Jupyter notebook code cell to code block convert
Jupyter notebook code cell to code block convert

Time:12-31

I have a notebook that has executable code cells and I want to batch convert them into markdown version code blocks. How to do that with .ipynb?

I think it need to write some complexed regex to do the conversion.

Suppose I know the code cell has type scheme

Before:

enter image description here

CodePudding user response:

Jupyter notebooks are encoded in the "json" notation. So, you can use Python's json module to import the ipynb file as a dictionary, modify the dictionary as needed, then save it as an ipynb file.

The change you're looking to make, at least in terms of appearance, seems as though you can change the encoding of the file as follows:

  • Loop through the cells of the notebook, in order
  • When you encounter a markdown block that is followed by a code block, append the "source" from the code block to the "source" of the markdown block before
  • Remove the code block

The following seems to work:

import json
with open('Test_in.ipynb','r') as f:
    data = json.load(f)

i = 0
while i 1 < len(data['cells']):
    cell, cell_next = data['cells'][i:i 2]
    if (cell['cell_type'] == 'markdown' 
        and cell_next['cell_type'] == 'code'):
        cell['source'].extend(["\n","```\n"])
        cell['source'].extend(cell_next['source'])
        cell['source'].extend(["\n","```\n","\n"])
        data['cells'].pop(i 1)
    else:
        i =1

with open("Test_out.ipynb",'w') as f:
    json.dump(data,f)

Here is the source for the Test_in.ipynb file that I used:

{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b554df9d",
   "metadata": {},
   "source": [
    "Example line 1\n",
    "Example line 2\n",
    "\n",
    "Example line 3\n",
    "\n",
    "    Example line 4\n",
    "\n",
    "Example line 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18286437",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(\"example line 6\")\n",
    "a = \"the number 7\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0a686ae",
   "metadata": {},
   "source": [
    "Example line 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a85d324c",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"example line 9\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e98c06b1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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": 5
}
  • Related