'Convert IPython notebook JSON to Python code programatically
I have JSON format of an IPython notebook in memory. I would like to convert this into python code. Can i use nbconvert for this?
I tried something like the following but it does not work. I guess the JSON isn't really in the expected NotebookNode object format.
nbconvert.PythonExporter().from_notebook_node(JSON)
Solution 1:[1]
You are right, nbconvert doesn't accepts JSON (or dict) to convest, so you need to convert to NotebookNode, using a code like:
import nbformat
mynotebooknode = nbformat.reads(your_json_in_memory, as_version=4)
and use the code nbconvert that you posted to transform in a python file:
nbconvert.PythonExporter().from_notebook_node(mynotebooknode)
Ps: I don't know why, but a lot of funcs uses 's' at the end for represent memory and not a file, like: json.dump(file) -> json.dumps(str_in_memory) json.load(file) -> json.loads(str_in_memory) And that the case here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sergio Gao |