37

I have two different Jupyter notebooks for two different projects. I would like to merge them to get them both on the same notebook.

One way is to copy cells from one to the other one but it seems to be quite a bit of work for projects containing many cells. Is there a feature supporting importing Notebook documents or copying multiple cells at a time?

1
  • This clarified everything for me here
    – Woodly0
    Mar 4, 2022 at 10:50

10 Answers 10

23

You can merge notebooks with nbmerge

Install it with pip

pip install nbmerge

and merge your files with

nbmerge file_1.ipynb file_2.ipynb file_3.ipynb > merged.ipynb
2
  • Does not work in my case, where the notebook had a lot of plots and outputs. Dec 16, 2019 at 3:05
  • Does this have to be executed on a fresh Jupyter Notebook or the Anaconda command prompt - sorry if it is a stupid question, beginner here.
    – Ammanuel
    Aug 7, 2021 at 14:42
14

pip install nbmerge

nbmerge file_1.ipynb file_2.ipynb file_3.ipynb > merged.ipynb

full documentation - https://github.com/jbn/nbmerge

2
  • 1
    It doesn't work yet with unicode characters inside the jupyter notebook, the issue persist in its github page. Oct 22, 2019 at 23:34
  • 4
    Works with unicode for me. Replace > merged.ipynb with -o merged.ipynb
    – Pavel
    Jun 6, 2020 at 23:14
10

You can indeed copy multiple cells at a time - use Shift + J or Shift + Down to select the next cell in a downwards direction, and in an upwards direction with Shift + K or Shift + Up. You could then copy and paste them en masse to another notebook, and preserve the cell divisions by making sure the destination notebook is in Command mode (cells have blue border).

See http://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Notebook%20Basics.html# and https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/.

1
  • 2
    This is exactly what I need. Thanks a lot.
    – dom free
    Sep 11, 2020 at 9:03
8

install nbmerge:

pip install nbmerge

run the following:

!nbmerge file_1.ipynb file_2.ipynb file_3.ipynb > merged.ipynb

Answer for those who missed to exclaim

1

I've done this sort of thing a few times in the past using a text editor on the .ipynb file. It's a json format that's fairly easy to follow. My use case was to move a series of cells into another notebook, more or less as an archive activity.

Make sure you have a backup strategy in place in case you break the json format by leaving out a comma or some such!

You end up with an invalid checksum, which you are warned about when you first open the changed notebook(s). After saving them the first time, a new checksum is created and the warning goes away.

1

Make sure all the notebooks you want to merge are in the same folder/directory and put these code in new jupyter cell (pls don't forget the exclamation mark)

 !nbmerge file_1.ipynb file_2.ipynb file_3.ipynb > merged_ouput.ipynb 
0

You should have a look at the following issue on GitHub which tracks this feature. It will likely be made possible via a command line tool. The issue also links to scripts to do that.

Copy-Pasting cells between browser windows is unfortunately a tricky problem, as browser security often prevents getting enough data to do so. It might be possible in some cases using some third party extensions.

0

Copy/pasting parts of a notebook into another one is a use case for Jupytext. Jupytext can export (the inputs) of your notebooks as .py or .md files. Execute either

jupytext --set-formats ipynb,py --sync *.ipynb

or

jupytext --set-formats ipynb,md --sync *.ipynb

Then edit the text files, move the desired inputs from one file to the other, and update the corresponding .ipynb files with

jupytext --sync *.ipynb

Please note that only the outputs for which the inputs were unchanged are preserved in this operation.

Jupytext is also available as a plugin for Jupyter. In that context, you don't need to use the command line. You can pair your notebook to the desired text file using the File/Jupytext menu (Jupyter Notebook) or the Jupytext commands (Jupyter Lab). And you just have to refresh your paired notebook in the browser to get the latest edits on the paired text file.

0

First, create a new Jupyter Notebook in the same directory as the previous notebooks you wish to merge.

In the new Jupyter Notebook, run this: pip install nbmerge .

Then, restart your Kernel.

Finally, run this:

!nbmerge your_first_file.ipynb your_second_file.ipynb your_third_file.ipynb -o merged.ipynb

Check your directory for the newly created merged file (merged.ipynb)

0

You would first have to create a new Notebook in the same directory as the notebooks you wish to merge.

then type the following:

  1. pip install nbmerge

  2. import nbmerge

  3. !nbmerge name1.ipynb name2.ipynb name3.ipynb -o new_name.ipynb

This should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.