Export jupyter to .jl does not preserve markdown cells

Hi,

this is a problem that I’m constantly facing… I usually develop my Julia code in jupyter (as per the IJulia package). When my codes get a usable form, I export them as .jl files (with the Export functionality in jupyter), and that makes a nice ascii file that does not contain any of the markdown cells… not a single one. This is very painful as I use markdown to document, so all the
documentation is gone, and I have to select cells in jupyter (one by one) and copy the contents in the newly generated julia file.
I know this is not the case with Python notebooks, where markdonw cells are converted to comments, which is exactly what I was looking for (I have never tried that but have read it on the internet).

Now the question is: is there a way to have this functionality? I mean, I want all my documentation to be included as commented lines in the exported .jl file, and I think this makes a lot of sense.

Best,

Ferran.

1 Like

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.

4 Likes

Maybe Weave.jl or Literate.jl solve this problem? It looks like Weave.jl can export from notebook to .jl file.

Not 100% sure though.

Oh! That was both fast and useful :slight_smile:
Thanks a lot, it works nicely.

Still I do not understand why this functionality is not included by default, but the solution you provided works well.

Best,

Ferran.

I think that they default to not including markdown because for an arbitrary language backend they don’t know what is allowed in comments? But I agree that it would be nice to support it for Julia similar to Python; maybe file an issue at GitHub - jupyter/notebook: Jupyter Interactive Notebook?

As an alternative to Jupyter, Pluto.jl saves its notebooks in directly runnable .jl files.

As another alternative, Jupyter notebook files are directly runnable with @nbinclude("somenotebook.ipynb") from NBInclude.jl.

2 Likes

Thanks, that’s a nice feature.