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

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!

Related

How to make down arrow on VSCode to go to end of line in Jupyter Notebooks?

I'm starting to use VSCode to work with Jupyter Notebooks (I've been using Google Colab before), and the thing that most annoys me is that when I'm at the last line of a cell and press the down arrow, nothing happens. In Google Colab, when you press the down button in the last line, it goes to the end of the line, and it makes me code much faster.
I know that VSCode has a lot of possibilities for customization and configuration, but I don´t have the knowledge for it. Is there a way to change that behaviour?
Pressing Ctrl+Down goes to the end of the line, and Ctrl+Up is for the start.
For more detailed keyboard shortcuts in VS Code, go to the article Visual Studio Code Key Bindings
For cursor to go from the beginning of the last line to the end of it when pressing arrow down (↓) instead of jumping to the next cell you should open Keyboard Shortcuts (File > Preferences > Keyboard Shortcuts [Ctrl+K Ctrl+S]) and remove or change bindings for "Notebook: Focus Next Cell Editor". Similarly you should change bindings for "Notebook: Focus Previous Cell Editor" if you want the same functionality for the up arrow (↑).

Jupyter programmatically execute an hotkey

I am trying to figure out if there is any code to execute a Jupyter hotkey.
I am specifically interested in "scroll up" hotkey: Shift + Space.
Is it possible to write code that, when executed, produce an effect equivalent to pressing Shift + Space, thus scrolling up the notebook?
I have not found anything relevant. I have tried to explore IPython module and its submodules, to no avail!
If what I am asking is impossible or "wrong", I would still appreciate an explanation. Why it is impossible to programmatically execute a Jupyter hotkey? What should I do instead?
You can edit keyboard shortcuts from Help menu as suggested in the documentation:
https://jupyter-notebook.readthedocs.io/en/stable/notebook.html?highlight=shift%20space#keyboard-shortcuts

Underline abruptly appears and deletes the code. How can I prevent this?

Sometimes when I'm editing python code in Jupyter Notebook, an underline abruptly appears.
I'm trying to edit the code anyway, then the underlined code is deleted when I click it or try to make a selection of some part of the code with Shift button on the keyboard or dragging of the mouse. I tried Ctrl+A to select and copy the whole code but Ctrl+A deleted the whole code.
Actually I don't know what is happening. I don't know exactly how I created the underline or how I deleted the underlined code, hence, I cannot prevent it from happening.
I'm new to Python and Jupyter and trying to use it.
But this has already happened 3-4 times. Please help me.
I had the same issue today and I didn't know why, that's the reason I find this post.
However, I was able to recover with the following method:
1, hold the Crtl + z, not the single click, and the deleted content will be back.
2, once you got everything deleted back, save the notebook and reopen it.

In Jupyter notebook, can the "enable scrolling" window auto-scroll down to keep up pace with the output?

I often write functions that print a lot, say every 100th go-around a long loop, which in the Terminal run fine, because my screen auto-scrolls down with the output; but on Jupyter, the output quickly moves downward, and I have to scroll down myself to keep up with it. This happens whether I'm in the "enable scrolling" or "disable scrolling" for the output.
So, my Q is: Is there a way to have Jupyter auto-scroll down with the output? I suppose this would make more sense inside of a scroll window (i.e. "enable scrolling".) Auto-scrolled output would give a more Terminal-like experience, which I am much more familiar and comfortable with. (And, to be honest, I also miss my The-Matrix-like aesthetic of streaming output.)
If it matters, I use Jupyter Lab primarily, but I believe the same is true on Jupyter Notebook.
Thanks so much, all!
nbextensions has an auto-scroll extension to do just that, though its incompatible with jupyter lab at the moment.
https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/nbextensions/autoscroll/README.html

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

Categories