ipython notebook clear all code - python

All I want to do is try some new codes in ipython notebook and I don't want to save it every time as its done automatically. Instead what I want is clear all the codes of ipython notebook along with reset of variables.
I want to do some coding and clear everything and start coding another set of codes without going to new python portion and without saving the current code.
Any shortcuts will be appreciated.
Note: I want to clear every cells code one time. All I want is an interface which appears when i create new python file, but I don't want to save my current code.

In Jupyter, do the following to clear all cells:
Press Esc to enter command mode.
Hold Shift. Select the first and last cells to select all cells.*
Press d twice to delete all selected cells.
Alternatively, if you simply want to try out code, consider running the ipython console, which is purely interactive in the REPL and does not require creating a new file. In a command prompt, type:
> ipython
Demo
Click outside the textbox to select a cell in command mode (thanks #moondra's).
Command Mode (Yes)
Edit Mode (No)

Well, you could use Shift-M to merge the cells (from top) - at least there's only one left to delete manually.

You can type dd to remove current selected cell. And if you continously press dd you can clean the screen.

Press the scissors (cut) button multiple times for each cell you want to delete.

Just do Ctrl+A , this selects all the individual blocks/cells and then press d twice.

Ok youngster, I will break this down for ya in simple steps:
go to the intended textbox and put your mouse cursor there
on keyboard press crtl-a and delete
Ta-dah all done
Glad to help

Related

How to terminate jupyter cell with python input prompt window?

First of all, do not laugh at me, <_<.
When using jupyter note book with pycharm, I run a cell like below:
for i in range(10000):
input()
So the window of the input() function keeps catching focus(This means I can not click any else of the pycharm window). And I do not know how to terminate this cell. Any suggestions?
There should be a stop (■) button on the header bar beside (▶ Run). Select the cell you want to terminate and select stop.

How to change PyCharm keyboard shortcuts on Mac

I want to change the run console command to pressing SHIFT and ENTER. I followed this tutorial but am unable to understand the actual step to input my personal commands.
I've made it all the way to the screenshot below and need more specific directions to input my personal commands. What do I do next?
That tutorial is about controlling/setting config for a specific run/script. To change keyboard shortcut/settings go your PyCharm preferences. Select Keymap on the left and in the search bar type "Run". You should see it under Run as the first option. Right click on it and you should see an "Add Keyboar Shortcut" and enter your preferred keyboard shortcut.
Keep in mind that Shift + Enter is already assigned so either change that or try another combination

VSCode: How to run active line without highlighting it?

In JupyterLab you can run code in many different ways. And I often use the option to run the active line (where the mouse pointer is) using Run > Run Selected text or current line in console with a keyboard shortcut. After doing this, the pointer jumps to the next line, and you can keep going.
Can the same thing be done using VSCode?
And just to be clear, the following is not what I'm looking for:
Ctrl+Enter will run the whole cell.
Shift+Enter when highlighting a line or or other parts of the code will run that part.
Shift+Enter with no highlighted code runs the whole cell and inserts a new cell below the active cell.
So, how can I run only the active line without highlighting it?
A similar question has been asked here: How to run the select code in VScode?, but that sends code to the Terminal and does not provide the answer I'm looking for.
Go to file > preferences > keyboard shortcuts, search for Run Selection / Line in interactive window and assign your desired keyboard shortcut. And make sure that there are not conflicts. I found out that several tasks had Shift + Enter assigned to it, and that's what had me confused in the first place.

how to run jupyter notebook from 5th cell to 100th cell, without running other part of the notebook?

Say I have a jupyter nootebook with 200 cells.
how to run from 5th cell to 100th cell, without running other part of the notebook?
Now I commend out 101th-200th and 1st-4th cell. I'm sure it is not the best practise.
One cannot just run cells 5 thru 100 easily with the Jupyter notebook but there are a few options. The first is just selecting each cell and running Merge Cell Above from the edit menu then just running the new cell. The second, and best way I've found to do this is:
First select the top cells to be ignored (or bottom if there are less on the bottom) and change the Cell Type to Raw NBConvert to prevent IPython from interpreting it.
Go to the cell after the one you want to run and select Run All Above (or below if less there).
Now you've only run those middle cells and can just go back and reset the cell type to code (instead of having to comment and then un-comment) and keep going.
* If you are using NBViewer you can hide the code or just set it to Markdown
Another option which requires less mouse movement and clicks than JGreenwell's answer, especially if you prefer accomplishing this with speedy keyboard work like I would:
Click in cell #1
Select all code (Ctrl+A)
Comment-out code (Ctrl+/)
Go to next cell (Shift+Enter) -- will execute cell, but is meaningless since all code is commented
Repeat steps 2-4 until cell #5
Select cell #200 and select Run All Above
Go back and un-comment cell 1-5 (Ctrl+A, Ctrl+/)
You can easily move between cells using the keyboard: Press Esc (command mode), Press J (up) or K (down) to select adjacent cell. Press Enter (to enter the code). Then press Ctrl+A then Ctrl+/ to un-comment. Repeat from Esc.

Ipython Notebook: Elegant way of turning off part of cells?

In my ipython notebook, there is part of cells that serves as preliminary inspection.
Now I want to turn it off, since after running it I know the status of the dataset, but I also want to keep it, so other people using this notebook can have this functionality.
How can I do it? Is there any example of doing it?
I can comment out these cells, but then switching between on and off would be quite laborious. And may not be quite convinent for other people.
I can abstract it into a function, but that itself has some methods, so the code would be quite convoluted, and may be hard to read?
Using Jupyter notebook you can click on a cell, press esc and then r. That converts it to a "raw" cell. Similar thing can be done to convert it back, esc + y. No comments needed, just key presses.
Within Jupyer notebook, go to Help -> Keyboard shortcuts for more.
Here's a snippet:
Command Mode (press Esc to enable)
↩ : enter edit mode
⇧↩ : run cell, select below
⌃↩ : run cell
⌥↩ : run cell, insert below
y : to code
m : to markdown
r : to raw
In Jupyter notebooks one can use this magic preamble at the beginning of a cell to avoid its execution:
%%script false --no-raise-error
You can use a condition at the cost of one extra indentation.
cellEnabled = 0
#cellEnabled = 1
if cellEnabled:
doA()
doB()
I had the same kind of desire and I eventually found out about the nbextension called Freeze. When you enable it, you get a nice freeze button in your toolbar. When you click it, the cell you're currently in will become "frozen". This means it will turn green (making it visually clear) and it will be ignored by the Run All process. It's also locked for editing, so you do need to unfreeze it (unlock button, two over to the left of the freeze button) before editing or running the cell. That's really easy to do though because it's just one button.
Let me know if this wasn't super clear. Otherwise, I hope this helps!

Categories