Export jupyter to .jl does not preserve markdown cells

Yes. A .ipynb file is just JSON text, so you can easily write a script using the JSON.jl package to extract the code and markdown. Moreover, you can use the Markdown stdlib to parse and pretty-print the markdown content:

using JSON, Markdown

function ipynb2jl(ipynfile)
    jlfile = replace(ipynfile, r"(\.ipynb)?$" => ".jl")
    nb = open(JSON.parse, ipynfile, "r")
    open(jlfile, "w") do f
        for cell in nb["cells"]
            if cell["cell_type"] == "code"
                print(f, "\n\n")
                print.(Ref(f), cell["source"])
            elseif cell["cell_type"] == "markdown"
                md = Markdown.parse(join(cell["source"]))
                print(f, "\n\n# ", replace(repr("text/plain", md), '\n' => "\n# "))
            end
        end
    end
end

Update: this functionality is now available via the nbexport function of the NBInclude.jl package.

6 Likes