JSON (JavaScript Object Notation) is a lightweight data-interchange format useful for non-tabular data. It is easy for humans to read and write. It is easy for machines to parse and generate. It supports self-documentation.
Useful to install JSON-formatting browser plugin, such as "JSONView", "JSON Viewer", "JSON Formatter".
Supplementary reference: Jennifer Widom database lectures.
Examples of use: Browser data, NHTSA Complaints database, and ... Jupyter Notebooks.
import requests
url = "http://www.acsu.buffalo.edu/~danet/Sp18/MTH448/class7/class7_files/jupyter_example.ipynb"
s = requests.get(url).text
s[0:500]
import json
d = json.loads(s)
len(d)
type(d)
for k in d:
print(k)
d['metadata']
d['nbformat']
d['nbformat_minor']
d['cells']
Obviously, all the notebook information is in the key 'cells'
Write a function ipynb_to_py.py that uses json to convert a jupyter notebook '.ipynb' file into a python executable '.py' script.
It should enter include all code in the 'code cells' and it should include all the text in the markdown cells as commented text using.
Submit your code as a ipynb to UBLearns. I will test your code to make sure it works. That is, it converts any .ipynb into a working .py python executable script.
Below is a start...
import json
# open ipynb and load it as a long string
jupyter_notebook_filename = 'jupyter_example.ipynb'
file_object = open(jupyter_notebook_filename)
s = file_object.read()
# decode string into a dictionary using json
d = json.loads(s)
#print type of cells and the information in cells
cells = d['cells']
for cell in cells:
print('type of cell = ' + cell['cell_type'])
lines_in_cell = cell['source']
for lines in lines_in_cell:
print(lines)
# step 1: open new file_object that replaces .ipynb with .py'
# step 2: print the lines of the cells into the .py file
# start line with # if the cell type if markdown
# otherwise just write the line
# close the file_object