def script_string_to_notebook_object(str): """ Convert a python script represented as string `str` to a notebook object. """ return py2jn.py_string_to_notebook(str, nbver=4)
def script_string_to_notebook(str, pth): """ Convert a python script represented as string `str` to a notebook with filename `pth`. """ nb = py2jn.py_string_to_notebook(str) py2jn.write_notebook(nb, pth)
def test_str_to_str(samplepy, sampleipynb): with open(samplepy, 'r') as fin: pystr = fin.read() nb = py2jn.py_string_to_notebook(pystr) outstr = py2jn.write_notebook_to_string(nb) with open(sampleipynb, 'r') as fin: refstr = fin.read() assert outstr == refstr
def markdown_to_notebook(infile, outfile): """Convert a markdown file to a notebook file.""" # Read infile into a string with open(infile, 'r') as fin: str = fin.read() # Enclose the markdown within triple quotes and convert from # python to notebook str = '"""' + str + '"""' nb = py2jn.py_string_to_notebook(str) py2jn.tools.write_notebook(nb, outfile, nbver=4)
def rst_to_notebook(infile, outfile): """Convert an rst file to a notebook file.""" # Read infile into a string with open(infile, 'r') as fin: rststr = fin.read() # Convert string from rst to markdown mdfmt = 'markdown_github+tex_math_dollars+fenced_code_attributes' mdstr = pypandoc.convert_text(rststr, mdfmt, format='rst', extra_args=['--atx-headers']) # In links, replace .py extensions with .ipynb mdstr = re.sub(r'\(([^\)]+).py\)', r'(\1.ipynb)', mdstr) # Enclose the markdown within triple quotes and convert from # python to notebook mdstr = '"""' + mdstr + '"""' nb = py2jn.py_string_to_notebook(mdstr) py2jn.tools.write_notebook(nb, outfile, nbver=4)