How can I download (save) all the codes from Jupyter Notebook?

still_learning :

I have written many programs in Jupyter Notebook. I have been downloading manually in HTML and python format. I would like to ask if there is a faster way to download all the programs that I have written there in a folder.

Many thanks

Infinity :

Jupyter notebook is a JSON file.

You can open it as a textual file and save all cells which have a code as the cell_type.

import json


PATH_TO_NOTEBOOK = r"/home/yam/notebook.ipynb"


def get_notebook(notebook_path):
    with open(notebook_path, 'r', encoding='utf-8') as notebook:
        return json.load(notebook)


def is_code_cell(cell):
    return cell['cell_type'] == "code"


def get_source_from_code_cell(cell):
    return ''.join(cell['source'])


def save_as_python_file(filename, code):
    with open(f'{filename}.py', 'w', encoding='utf-8') as f:
        f.write(code)


def get_code_cells_content(notebook_cells):
    yield from (
        (i, get_source_from_code_cell(current_cell))
        for i, current_cell in enumerate(notebook_cells, 1)
        if is_code_cell(current_cell)
    )


notebook = get_notebook(PATH_TO_NOTEBOOK)
for filename, code in get_code_cells_content(notebook['cells']):
    save_as_python_file(filename, code)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to save a jupyter notebook with outputs

How can I check source code of a module in Jupyter notebook?

How can I download all emails with attachments from Gmail?

How can I display an image from a file in Jupyter Notebook?

How can I add a table of contents to a Jupyter / JupyterLab notebook?

How can I download a file from a URL and save it in Rails?

How can I build a GUI to use inside a jupyter notebook?

How can i run Jupyter notebook in admin privilege?

How can I save a Jupyter Notebook (iPython notebook) programmatically?

How do I set the save location of a session in Jupyter notebook?

How to download all files and folder hierarchy from Jupyter Notebook?

How to save feature importance plot of xgboost to a file from Jupyter notebook

How to save errors from Jupyter Notebook cells to a file?

Can I run a jupyter notebook with an R kernel from a jupyter notebook with a python kernel?

How can I plot 4000 images line by line in jupyter notebook?

How can I hide code and rerun all cells in a jupyter notebook?

How to download a .py file from Jupyter lab notebook?

How can I import code and markdown cells from Python to Jupyter notebook?

How can I convert a Rmd document to a jupyter notebook

Jupyter Notebook didn't save for over a day. How can I recover lost work?

File download in Voila from Jupyter Notebook

How do I save output images using Jupyter notebook?

How can I debug a dieing jupyter notebook ipython kernel?

How can I download all observations from Hue/Hive output?

How can I download all files from webdav page?

How can I run .py file from another directory in jupyter notebook?

How can I add/merge values from one existing column to another column - Python - Pandas - Jupyter Notebook

How can i pause this code in Jupyter Notebook?

How can I get to show a picture from file on my jupyter notebook?