I prefer to write my python code on VSCode because of its intellisense and autocomplete features. But I would rather see and debug the code on a Jupyter notebook to better visualize the data I am working with.
I know I can load any file into Jupyter by using the magical %load
or %loadpy
commands. But they load the entire file into a single cell.
Since I wanted to see the intermediary results of certain operations, I would like to import the file in such a way that each line on the file is assigned to a cell on the notebook. Unless it's a function or conditional statement (in other words, anything with indentation); in these cases it should add the whole block into one cell.
How can I do that?
You can create a new Jupyter notebook file programmatically with
nbformat
module. Let me demonstrate all the steps with a working code.Below are cells in a Jupyter notebook. This cell has a magic command that creates a file, named
script001.py
.Code in the second cell below creates a new notebook, named
split_cells.ipynb
.When you open the new notebook,
split_cells.ipynb
, you will have the cells like this:This notebook is ready to run as you wish. Hope it helps.
AFAIK, there is no way to do this upon loading your file into a Jupyter notebook. However, you can easily split your big cell into multiple cells, by using ctrl+shift+-. This will split your cell at the position where your cursor is located at that time.
I don't know how big your files are, but it is a really fast and efficient way to split cells so maybe that works for you =)