# Export jupyter to .jl does not preserve markdown cells

**URL:** <https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556>\
**Category:** General Usage\
**Tags:** markdown\
**Created:** [March 19, 2021, 4:32pm UTC](https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556 "2021-03-19T16:32:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)\
**Post date:** [March 19, 2021, 4:32pm UTC](https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556/1 "2021-03-19T16:32:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 19, 2021, 5:02pm UTC](https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556/2 "2021-03-19T17:02:10Z")

</div>

> [@Ferran\_Mazzanti](#):
>
> 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.

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:

```julia
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](https://github.com/stevengj/NBInclude.jl) package.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 19, 2021, 5:09pm UTC](https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556/3 "2021-03-19T17:09:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)\
**Post date:** [March 19, 2021, 5:11pm UTC](https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556/4 "2021-03-19T17:11:53Z")

</div>

Oh! That was both fast and useful 🙂  
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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 19, 2021, 5:44pm UTC](https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556/5 "2021-03-19T17:44:49Z")

</div>

> [@Ferran\_Mazzanti](#):
>
> Still I do not understand why this functionality is not included by default, but the solution you provided works well.

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](https://github.com/jupyter/notebook)?

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [March 19, 2021, 7:56pm UTC](https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556/6 "2021-03-19T19:56:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 19, 2021, 9:41pm UTC](https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556/7 "2021-03-19T21:41:25Z")

</div>

As another alternative, Jupyter notebook files are directly runnable with `@nbinclude("somenotebook.ipynb")` from [NBInclude.jl](https://github.com/stevengj/NBInclude.jl).

---

<div class="post-metadata">

**Author:** ![TS-CUBED](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ts-cubed/32/25292_2.png) [@TS-CUBED](https://discourse.julialang.org/u/TS-CUBED)\
**Post date:** [June 18, 2021, 4:32pm UTC](https://discourse.julialang.org/t/export-jupyter-to-jl-does-not-preserve-markdown-cells/57556/8 "2021-06-18T16:32:33Z")

</div>

> [@stevengj](#):
>
> As another alternative, Jupyter notebook files are directly runnable with `@nbinclude("somenotebook.ipynb")` from [NBInclude.jl](https://github.com/stevengj/NBInclude.jl)

Thanks, that’s a nice feature.
