I'm writing python using jupyter notebook and I have two cells that can influence each other.
I'm wondering is it possible to leave some certain cells out after I click Restart & Run All so that I can test the two cells independently?
One option based on Davide Fiocco's answer of this post and that I just tested is to include %%script magic command on each cell you don't want to execute.
For example
%%script false --no-raise-error
for i in range(100000000000000):
print(i)
If you put those two cells at the end of the page, you can run all cells above a certain cell with a single click.
That or you can put a triple-quote at the beginning and end of the two cells, then un-quote the cells to test them.
One option is to create a parameter and run the cells accordingly
x = 1
# cell 1
if x == 1:
// run this cell
# cell 2
if x != 1:
// run the other cell
In this example, you will skip cell 2.
I recently discovered an easy way to do this.
You may have noticed that cells can be set as type Code or Markdown - this lets you prepare the notebook with headers and explanatory text (in Markdown), but also sections of executable code (the default). This can be set from a drop-down already on the screen if using via Jupyter Lab. In Jupyter Notebook I think it's under the Cells menu.
You can also use keyboard shortcuts (first hit Escape if needed to get out of text-entry mode: Y for Code, Mfor Markdown, or Rfor Raw.
Wait, what's that about Raw? It appears to just take away the code highlighting and make the cell not executable! So Esc+R to make it Raw, then execute like you wanted to, then Esc+Y if you want to re-enable that block.
Alternative: If you want a quicker way to comment out all the lines but leave it as a Code block, make sure you are in edit mode (click on the cell contents), do Ctrl+A (for select-all), and then Ctrl+/ (for "comment this line"). I tested with python and it inserts # at the beginning of each selected line.
Related
I can't get it to work reliably, sometimes it just outputs line by line and the response does not make sense. I don't think I'm doing it right, it's a pretty simple instruction. I use it a lot in python, but not really sure how to get it to "execute" the instruction.
An example would be something like:
Create a table with 1 row and 7 columns. The columns headings are
"column1" and "column2", etc. The first row should have the values
"row1" and "row1", etc.
To which it outputs just a bunch of repeating "" with each tab, or sometimes just additional comments instead of html.
In python I just start typing out def to show that I want to put the instructions so far into a function. But sometimes it happens that I have to do line by line tabbing there as well, not sure why?
I am trying to use Jupyter + Python. Here is an example of the output
You can see the because the column 'correspondencedata' is too long, so it can not be shown fully in the output.
Can I change this so that a horizontal scroll bar will occur when a column has too long content?
You want to use pd.set_option('max_colwidth', nbr_pixel) before.
If you use a number big enough it will always show the entire content of your cells.
Like, pd.set_option('max_colwidth', 4000)
For more informations:
## To see the actual settings :
pd.get_option("display.max_colwidth")
## To reset with default value
pd.reset_option("max_colwidth")
Documentation
Is it possible to execute the previous cell from in jupyterlab ?
something like:
import IPython
IPython.exec_cell(-1)
I want to execute the previous cell (if it has been updated without having been re-executed).
I have a cell that contains the test from the previous. If I modify the function, I want to only execute the cell that contains the test.
I need to import and manipulate memory heavy data in jupyter.
Because I tend to have rather long notebooks were several data sets will be importer I need to clear them continuously by hand.
This is tideous.
If possible, i would like to have a tool which clears all variables introduced in a cell and only those without the need of addressing them by hand after they fullfilled there purpose.
I could of course overwrite variables, however as they all serve rather different purposes this will drastically reduce the readabiliy of the code.
To summarize:
Cell 1:
variable overhead #this will be used in the entire notebook
Cell 2:
import or generate data & manipulate data
clear all variables introduced in cell without the need of addressing
every single one of them by hand <-- this is what i am looking for.
Thank you very much!
You can reset variables in the Jupyter Notebook by putting the following magic command in the code:
%reset_selective -f [var1, var2, var3]
If you add such lines in your code it should remain readable.
To answer your question completely - At the moment I don't think there exists a command that would automatically find all variables created in a specific cell and reset only them. (Someone please correct me if I am wrong.)
But you can use the following code that deletes exactly those namespace objects which were newly created in a cell. It is probably what you wanted:
from IPython import get_ipython
my_variables = set(dir()) # Write this line at the beginning of cell
# Here is the content of the cell
my_variables = list(set(dir()) - my_variables) # Write these 2 lines at the end of cell
get_ipython().magic('%reset_selective -f [{}]'.format(','.join(my_variables)))
It's not a clean solution but the cells which data I need to keep are not computationally expensive.
Therefore I found it most convenient to simply do:
%reset -f
exec In[n:m]
Jupyter has a feature in being able to execute one cell at a time. If a cell has a lot of statements it's often possible (desirable) to split it into smaller single statement cells, except when a block is involved,e.g if, for, def, etc.
this question was asked earlier in a different way:
Execute algorithm step by step in Jupyter
and answered
What is the right way to debug in iPython notebook?
While invoking a debugger may be the best option available, it does seem kludgy, and it likely would not work with non Python kernels.
What would be ideal is to have nested cells, and have a way to execute the entire block or the subcell.
for a Python example splitting a cell containing:
if 0 == 1:
zero = 1
else:
zero = 0
into, say, two cells:
if 0 == 1:
zero = 1
and
else:
zero = 0
likewise for Julia or R.
a debugger solution would not be my preference.
Unfortunately, this is not possible. The reason for this is that the else condition by itself would cause an error. You can split the cell using control+shift+subtract but once you try and run the last cell an error occurs. You can see the exact example of this in the picture I have included. Please let me know if you have any further questions/comments!
]1