2

I am wondering if there's a way to remove cell tags when converting a jupyter notebook to julia script. At the moment, I use the Weave package and it works fine except leaving some cell tags #+ inbetween the lines of syntax. What's the best way to remove these #+ and get a cleaner conversion?

julia> using Weave

julia> convert_doc("mynotebook.ipynb", "mynotebook.jl")

#+ 

using DataFrames
using Weave

#+ 
codedancer
  • 1,504
  • 9
  • 20

1 Answers1

1

These characters seem to be hardcoded (source).

You could fork the repo and change this behavior or you could use some sort of command line utility on the converted file to remove every line where the #+ characters exist and any other unnecessary newlines.

Let's say we have a notebook that looks like this:

notebook

This will be converted to this:


#+



#+

print("print from cell 2")


#+

print("print from cell 3")

We could use a tool like sed to delete all lines that start with #+ using:

sed '/^#+/d' file.jl

This returns:






print("print from cell 2")



print("print from cell 3")

There are still a lot of newlines so we could use cat -s on this output:

-s, --squeeze-blank suppress repeated empty output lines

sed '/^#+/d' file.jl | cat -s

Which returns:


print("print from cell 2")

print("print from cell 3")

We can overwrite the content of the file with this output:

sed '/^#+/d' file.jl | cat -s > file.jl

If you're on Windows and you don't have a sed like utility installed that you could apply above idea with, you could look at this post.

5eb
  • 14,798
  • 5
  • 21
  • 65