Skip to content

Instantly share code, notes, and snippets.

@stisa stisa/ipynb2nim.nim
Last active Sep 11, 2016

Embed
What would you like to do?
Parse nim based jupyter notebook into a sort of literate nim, markdown goes into documentation blocks and code is kept
import os,json,strutils
## TODO:
# - check input file type
# - a parser for markdown->html
let arguments = commandLineParams() # [0] should always be the input file
var nbf = arguments[0].readFile
var outfile :string
if arguments.len>1 and arguments[1]=="into":
outfile = arguments[2]
else:
outfile = "out.nim"
var nbjson = parseJson(nbf) # parse the notebook as json
var cnt : string = "" # content of the final file
proc jsonArrToStr(j:JsonNode):string # forward declaration, accumulate a JsonArray into a string
for cell in nbjson["cells"]:
var ct = cell["cell_type"].str
if (ct == "markdown"):
var src = cell["source"].jsonArrToStr
cnt&= ("\n##[" & src & "]##\n")
elif (ct=="code"):
var src = cell["source"].jsonArrToStr
cnt&=("\n"&src&"\n")
writefile(outfile, cnt)
proc jsonArrToStr(j:JsonNode):string =
result = ""
for e in j:
result&=e.str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.