0

Lets say I have a jupyter notebook .ipynb file created, with a cell block of type code and type markdown. If I open the file to see the raw contents, I see something like this

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f0e5fd3c-73ce-4772-acba-ec62dbbc4a90",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print(\"hello world\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "b9d6d461-d25d-4ae1-b806-5f2c0d3c8332",
   "metadata": {},
   "source": [
    "this is normal text"
   ]
  }
 ],
 "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.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

How would I go about programmatically getting the raw contents of a current notebook that is being worked on? Is such a thing possible? I've tried looking into the JupyterLab Contents API and IPython but can't seem to find a solution to get the raw contents of the file. Any help or advice is appreciated, thanks!

Jenny C
  • 3
  • 3
  • Does this answer your question? [convert json ipython notebook(.ipynb) to .py file](https://stackoverflow.com/questions/37797709/convert-json-ipython-notebook-ipynb-to-py-file) – Daniel Apr 11 '22 at 14:44
  • Simply write some Python code to read the notebook file contents as you would for a standard text file or JSON file. – w. Patrick Gale Jul 12 '23 at 16:13

2 Answers2

0

There is a button to export it. For example, opening it in vscode, beside the variables in the top right of the screen, there are 3 dots. Click on that > export > Python Script

March_G
  • 114
  • 5
  • Sorry, I should've specified in the title. I'm looking for a way to programmatically get the raw contents of the file. – Jenny C Apr 11 '22 at 14:44
  • I see, well it is still easy to implement since it follows the same format **for j in k['cells']: x = j['source'] print(x)** a simple loop would give you flexibility over what you want to choose, which in this case is the content and that is stored in the ['source'] of each cell – March_G Apr 11 '22 at 14:56
  • Could you please elaborate? Where is the k['cells'] and j['source'] coming from here? – Jenny C Apr 11 '22 at 15:05
  • k is a any variable that holds all the data of the ipynb (in this case the one you linked). Now, when initiating the loop, you have to enter the dictionary 'cell' (the first value of the dictionary). Next, [] indicates that you are navigating through lists, so you each **List** using j, which is stored in the **dictionary** cells. Finally, in the list you are navigating in you choose the dictionary called source, since it has the information needed. – March_G Apr 11 '22 at 17:13
  • @March_G, I think you forgot to mention `nbformat`? @Jenny_C, see the very top of my reply [here](https://stackoverflow.com/posts/71244733/edit) (~top three parapgraphs) to start learning about `nbformat`. I have a number of examples [posted at the Jupyter Discourse Forum here](https://discourse.jupyter.org/search?q=nbformat%20%40fomightez) of programmatically parsing notebooks by iterating on the cells in the notebook using `nbformat`. – Wayne Apr 11 '22 at 17:22
0

Try this:

import codecs

jupyter_file = codecs.open(filePath, 'r')
source_data = jupyter_file.read()

print(source_data)

source_data gives the raw content of .ipynb file as a JSON string. You can use json.loads() to parse that string and use it or dump it in a separate file as you need.

SM1312
  • 516
  • 4
  • 13