versioninfo()
Julia Version 0.3.9 Commit 31efe69 (2015-05-30 11:24 UTC) Platform Info: System: Linux (x86_64-unknown-linux-gnu) CPU: Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz WORD_SIZE: 64 BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge) LAPACK: libopenblas LIBM: libopenlibm LLVM: libLLVM-3.3
using PyCall
@pyimport IPython.nbformat.current as nbf
/usr/local/lib/python2.7/dist-packages/IPython/nbformat/current.py:19: UserWarning: IPython.nbformat.current is deprecated. - use IPython.nbformat for read/write/validate public API - use IPython.nbformat.vX directly to composing notebooks of a particular version """)
nb = nbf.new_notebook()
Dict{Any,Any} with 4 entries: "worksheets" => {} "nbformat_minor" => 0 "metadata" => Dict{Any,Any}() "nbformat" => 3
This notebook will simply have three cells that read print 0
, print 1
, etc:
text = "This is an auto-generated notebook."
code = "1+2"
cells = [
nbf.new_text_cell("markdown", text),
nbf.new_code_cell(code)
]
2-element Array{Dict{Any,Any},1}: {"cell_type"=>"markdown","source"=>"This is an auto-generated notebook.","metadata"=>Dict{Any,Any}()} {"outputs"=>{},"cell_type"=>"code","language"=>"python","metadata"=>Dict{Any,Any}(),"input"=>"1+2","collapsed"=>false}
Now that we have the cells, we can make a worksheet with them and add it to the notebook:
push!(nb["worksheets"], nbf.new_worksheet(cells=cells))
1-element Array{Any,1}: {"cells"=>{{"cell_type"=>"markdown","source"=>"This is an auto-generated notebook.","metadata"=>Dict{Any,Any}()},{"outputs"=>{},"cell_type"=>"code","language"=>"python","metadata"=>Dict{Any,Any}(),"input"=>"1+2","collapsed"=>false}},"metadata"=>Dict{Any,Any}()}
Next, we write it to a file on disk that we can then open as a new notebook.
Note: This should be as easy as: nbf.write(nb, fname)
, but the current api is a little more verbose and needs a real file-like
object.
fname = "test.ipynb"
open(fname, "w") do f
nbf.write(nb, f, "ipynb")
end
PyError (:PyObject_Call) <type 'exceptions.AttributeError'> AttributeError("'dict' object has no attribute 'worksheets'",) File "/usr/local/lib/python2.7/dist-packages/IPython/nbformat/current.py", line 188, in write s = writes(nb, **kwargs) File "/usr/local/lib/python2.7/dist-packages/IPython/nbformat/current.py", line 154, in writes return versions[version].writes_json(nb, **kwargs) File "/usr/local/lib/python2.7/dist-packages/IPython/nbformat/v3/nbjson.py", line 48, in writes nb = split_lines(nb) File "/usr/local/lib/python2.7/dist-packages/IPython/nbformat/v3/rwbase.py", line 85, in split_lines for ws in nb.worksheets: while loading In[7], in expression starting on line 3 in pyerr_check at /home/juser/.julia/v0.3/PyCall/src/exception.jl:58 in pycall at /home/juser/.julia/v0.3/PyCall/src/PyCall.jl:91 in fn at /home/juser/.julia/v0.3/PyCall/src/conversions.jl:188 in anonymous at In[7]:4 in open at iostream.jl:137
nb["worksheets"]
1-element Array{Any,1}: {"cells"=>{{"cell_type"=>"markdown","source"=>"This is an auto-generated notebook.","metadata"=>Dict{Any,Any}()},{"outputs"=>{},"cell_type"=>"code","language"=>"python","metadata"=>Dict{Any,Any}(),"input"=>"1+2","collapsed"=>false}},"metadata"=>Dict{Any,Any}()}
This notebook can be run at the command line with:
ipython -c '%run test.ipynb'
Or you can open it as a live notebook.
run(`ipython -c '%run test.ipynb'`)