I want to convert my work as a script. Selectively.
I like to do my development in jupyter notebooks. When I convert them to scripts I need to save the whole notebook as a script and then remove what I dont need. WHY -.- I want to make a script to save only the cells I finally want for my script by adding an #export to them.
Let us first import JSON since jupyter notebooks are JSON files
After that we allocate a dictionary and parse the notebook. Since JSONs are just pretty dictionaries, we convert the whole file into one.
dict2 = Dict() open(ARGS, "r") do f global dict2 dict2=JSON.parse(f) end
Now to identify the cells we need we have to add a #export to the start of the cell. Once we do that, we can go through all the cells and take the ones which have this #export in it. Then we filter this out so it does not appear in the end script. We add all this to a string.
gstr = ""; for a in dict2["cells"] if "#export\n" in a["source"] temp = a["source"] temp = filter!(e->e|"#export\n",temp) gstr*= join(temp) gstr*="\n" end end
Then we write the file and we are done :)
io = open(ARGS*".jl", "w") write(io, gstr) close(io)