When I work with Jupyter Notebooks, I usually have several cells, each with its own task. Often, I need to set up something like a list of IDs, or different dataframes for analysis and then run other cells based on that setup.
Changing the IDs or using different dataframes means I have to create a new cell and then run a different cell again, which disrupts my workflow.
I suggest adding a feature to split one cell into multiple parts that can be run independently. This would let me keep everything organized in one cell, but still run just the parts I need. It would make working with Jupyter Notebooks more convenient and efficient.
What do you think?
You can already “Independently Execute Sections of a Single Cell” in JupyterLab.
I just tested this described process in an answer to ‘How to run a single line or selected code in a Jupyter Notebook or JupyterLab cell?’ (the very top section) in JupyterLab 3.4.8. and 4.2.0 and it worked to just run the selected line or lines.
The only slightly odd thing I noticed is that the first time you run it, it just opens the console and so you have to run it again to run the code. (After that though, it runs subsequent calls immediately.) That console is attached to the active notebook namespace, and so it effectively runs the code in the notebook, even if the output of the cell isn’t updated.
To demonstrate to yourself it works with no need to touch your own computer or login to anything:
- Launch a temporary JupyterLab 4.2 session in your browser by pressing the ‘launch binder’ badge here.
- Make a cell with the following code and run it:
print("test1")
print("test 2")
print("test 3")
- I suggest adding setting some variables to some numerical values above or below the line
print("test 2")
, i.e., in roughly the middle of that cell, and then select those few lines with your mouse & use the shortcut to run those added lines. Following that, make a new cell in your notebook file at a later point below and put one of the new variables and run the cell and you’ll see it is defined as what was set by the added lines.