How to terminate jupyter cell with python input prompt window? - python

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.

Related

How to make VS Code Jupiter Notebook to stop code execution before a given cell?

How to make VS Code Jupiter Notebook to stop code execution before a given cell? I know that it is possible to start code execution from a certain cell and down by clicking the 'Execute Cell and Below' button, but then all the code will be executed to the end of the file. And it is necessary that the execution stops before the specified cell, without executing the entire code to the end. Many thanks!
Using VS Code 1.64.2, select the chosen cell, then click on "Execute Above Cells":

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.

ipython notebook clear all code

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

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!

Why does powershell freeze for a bit when running my python scripts

Sometime I look back at my terminal when there is a python script running and the console output has frozen, then I right-click on the terminal and the console output (printing to screen) beings again.
Its a bit disconcerting because sometimes I think my script has broken.
Do others also experience this? Anybody know a fix?
Thanks in advance for any responses
If it's intermittent with all other factors being unchanged, it sounds like you've inadvertently selected some text in the PowerShell console and it's halted updating output so that you can do something with it.
Next time, be careful to look to see if you have something selected before clicking.
I agree with #alroc's suggestion; i.e. the cause could be accidentally clicking on the console.
A solution to prevent this is to right click on the powershell console window's title bar, select properties, uncheck Quick Edit Mode, then click OK. This disables some features (i.e. copy by select & enter, paste by right click), but means if you accidentally click on the screen it does no harm.
Another solution's to simply press escape (or right click in the script's window) if the script's taking a while - generally that'll do no harm (i.e. it won't terminate your script), but it will exit the edit session, allowing the script to resume if it had been paused due to edit mode.
To play with these, run the below script, then click on the screen whilst it's running (this script just outputs a bunch of numbers).
1..99999999
To terminate the script completely, press ctrl+c.

Categories