Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search Gists
Sign in Sign up

Instantly share code, notes, and snippets.

@chsasank
Last activeMarch 8, 2025 17:27

    Select an option

    Save chsasank/7218ca16f8d022e02a9c0deb94a310fe to your computer and use it in GitHub Desktop.
    Convert jupyter notebook to sphinx gallery notebook styled examples.
    """Convert jupyter notebook to sphinx gallery notebook styled examples.
    Usage: python ipynb_to_gallery.py <notebook.ipynb>
    Dependencies:
    pypandoc: install using `pip install pypandoc`
    """
    importpypandocaspdoc
    importjson
    defconvert_ipynb_to_gallery(file_name):
    python_file=""
    nb_dict=json.load(open(file_name))
    cells=nb_dict['cells']
    fori,cellinenumerate(cells):
    ifi==0:
    assertcell['cell_type']=='markdown', \
    'First cell has to be markdown'
    md_source=''.join(cell['source'])
    rst_source=pdoc.convert_text(md_source,'rst','md')
    python_file='"""\n'+rst_source+'\n"""'
    else:
    ifcell['cell_type']=='markdown':
    md_source=''.join(cell['source'])
    rst_source=pdoc.convert_text(md_source,'rst','md')
    commented_source='\n'.join(['# '+xforxin
    rst_source.split('\n')])
    python_file=python_file+'\n\n\n'+'#'*70+'\n'+ \
    commented_source
    elifcell['cell_type']=='code':
    source=''.join(cell['source'])
    python_file=python_file+'\n'*2+source
    python_file=python_file.replace("\n%","\n# %")
    open(file_name.replace('.ipynb','.py'),'w').write(python_file)
    if__name__=='__main__':
    importsys
    convert_ipynb_to_gallery(sys.argv[-1])
    @weber-s
    Copy link

    weber-s commentedApr 30, 2018
    edited
    Loading

    Hi,

    This script is very useful! However lots of people now uses magic command such as%matplotlib inline and so, the resulting file is not executable by regular python.

    I suggest adding something like:

    python_file=python_file.replace("\n%","\n# %")

    just before the writing of the file, l. 35.

    thanks again :)

    @chsasank
    Copy link
    Author

    Modified!

    @nabriis
    Copy link

    Really useful script. When using it I found that the pdoc conversion of markdown adds "\r\n" after every new line of the file I tried.
    This causes errors in the conversion since the filer writer will add write 2 new lines splitting rst cells.

    A quick fix was to add

    rst_source=rst_source.replace('\r','')

    after line 23 and 28.

    There might be a more clever way with how to write the file in the end.

    @rabinadk1
    Copy link

    May be replace

    nb_dict=json.load(open(file_name))

    with

    withopen(file_name)asf:nb_dict=json.load(f)

    in line 14 and replace

    open(file_name.replace('.ipynb','.py'),'w').write(python_file)

    with

    withopen(file_name.replace('.ipynb','.py'),'w')asf:f.write(python_file)

    in line 38.

    @HealthyPear
    Copy link

    I had the opposite issue of@weber-s : my first cell was magic code so as a quick fixed I modified the first conditional block as

    ifcell["cell_type"]=="markdown":md_source="".join(cell["source"])rst_source=pdoc.convert_text(md_source,"rst","md")python_file='"""\n'+rst_source+'\n"""'elifcell["cell_type"]=="code":source="".join(cell["source"])python_file=python_file+"\n"*2+sourceelse:raiseValueError(f"Unsupported starting cell type{cell["cell_type"]}.")

    Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

    [8]ページ先頭

    ©2009-2025 Movatter.jp