Is it possible to move n cells from one notebook to a new notebook?
Programmatically move n cells from one notebook to a new one
Yes, you can use nbformat to take a notebook and limit the content of the new notebook to the a block of n cells of the original notebook.
"The nbformat package allows you to programmatically read and parse notebook files." - SOURCE, Tony Hirst's description
nbformat comes as part of Jupyter, and so it runs wherever you have your notebooks running without any need for any additional packages.
Making a notebook from the first n cells of a notebook
I'm going to base this mainly on code adapted from my post at the Jupyter Discourse forum here. I have other examples with nbformat-related code you can get to from links below that post.
You can paste this code in a notebook that's running where your input notebook is located:
number_cells_to_keep = 5 # number of first n cells in the input notebook to keep
import nbformat as nbf
ntbk = nbf.read("old_notebook.ipynb", nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for indx, cell in enumerate(ntbk.cells) if indx < number_cells_to_keep]
nbf.write(new_ntbk, "first_n_cells_of_input_notebook.ipynb", version=nbf.NO_CONVERT)
Edit the number on the first line to adjust the number of cells at the top of the original notebook that will be moved to the new notebook.
Making a notebook from a block of cells at a start point & spanning n number of cells
You could adapt the above code to select a specific internal range of cells.
This example illustrates starting the interval of cells at the fifth code cell and taking that and the following cells for a total of ten from the originating notebook to produce the new notebook:
cell_to_start_collecting_at = 5 # number of cell to start the span of cells to collect; first cell gets number 1 in bracket if run so use that numbering
length_of_cell_block_to_keep = 10 # length of sequential span of cells to keep
import nbformat as nbf
ntbk = nbf.read("old_notebook.ipynb", nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for indx, cell in enumerate(ntbk.cells) if cell_to_start_collecting_at - 2 < indx < (cell_to_start_collecting_at + length_of_cell_block_to_keep - 1)]
nbf.write(new_ntbk, "has_interval_of_cells_from_input_notebook.ipynb", version=nbf.NO_CONVERT)
Edit the first two lines to adjust the starting cell number and length of the block of cells to move to the new notebook.
The collecting of the cells for the new notebook using nbformat while iterating on the original notebook cells with for cell in ntbk.cells
or a variant could be adapted & made to be complex. For example, if you had a list of certain cells to go into the new notebook or only wanted to count code cells for the point at which to start.
Split out out some cells from a notebook using nbmanips
If you don't want to customize an approach using nbformat, you may be able to split out some cells from a notebook using nbmanips. It also is customizable on tags, too. See an example discussed here.
Use JupyterLab to drag by hand a sequence of cells to a new notebook
If you are trying to move a block of cells from one notebook to a new one using JupyterLab's graphical user interface, you open your new notebook and arrange it side-by-side next to the original notebook in your main pane by dragging the tabs with the notebook names to arrange them.
Then you can select the cells in the original notebook and then dragging from the top of that highlighted block, drag them over into the new notebook.
Save the edited version of the 'new' notebook.
Video illustrating the arranging and dragging approach is here.
It is featured under 'Drag cells between notebooks to quickly copy content' on the Notebooks page in the JupyterLab documentation.
© 2022 - 2024 — McMap. All rights reserved.