Jupyter - Hotkeys Flashcards
Esc
Esc will take you into command mode where you can navigate around your notebook with arrow keys.
A
A to insert a new cell above the current cell
B
B to insert a new cell below
M
M to change the current cell to Markdown
Y
Y to change it back to code
D + D
D + D (press the key twice) to delete the current cell
Enter
Enter will take you from command mode back into edit mode for the given cell.
Shift + Tab
Shift + Tab will show you the Docstring (documentation) for the the object you have just typed in a code cell - you can keep pressing this short cut to cycle through a few modes of documentation.
Ctrl + Shift + -
Ctrl + Shift + - will split the current cell into two from where your cursor is
Esc + F
Esc + F Find and replace on your code but not the outputs.
Esc + O Toggle cell output.
Toggle cell output.
Shift + Down
Shift + Up
Shift + J or Shift + Down selects the next sell in a downwards direction. You can also select sells in an upwards direction by using Shift + K or Shift + Up.
(Once cells are selected, you can then delete / copy / cut / paste / run them as a batch. This is helpful when you need to move parts of a notebook.)
Shift + M
You can also use Shift + M to merge multiple cells.
?[insert library, method, or variable here]
Don’t forget also that by prepending a library, method or variable with ?, you can access the Docstring for quick reference on syntax.
%run [insert .py or .ipynb file here]
%run can execute python code from .py files - this is well-documented behavior. Lesser known is the fact that it can also execute other jupyter notebooks, which can quite useful.
%load [insert script file or URL here]
This will replace the contents of the cell with an external script. You can either use a file on your computer as a source, or alternatively a URL.
%%time
%%time will give you information about a single run of the code in your cell.
%%timeit
%%timeit uses the Python timeit module which runs a statement 100,000 times (by default) and then provides the mean of the fastest three times.
%%writefile
%pycat
Using the %%writefile magic saves the contents of that cell to an external file. %pycat does the opposite, and shows you (in a popup) the syntax highlighted contents of an external file.
%pdb
Jupyter has own interface for The Python Debugger (pdb). This makes it possible to go inside the function and investigate what happens there.
adding a ; to end of line
Sometimes it’s handy to suppress the output of the function on a final line, for instance when plotting. To do this, you just add a semicolon at the end.
It’s easy to execute a shell command from inside your notebook. You can use this to check what datasets are in available in your working folder…
!ls *.csv
It’s easy to execute a shell command from inside your notebook…like to check and manage packages
!pip install numpy
!pip list | grep pandas
Just use IPython Magics with the name of your kernel at the start of each cell that you want to use that Kernel for…
%%bash
%%HTML
%%python2
%%python3
%%ruby
%%perl
Running R and Python in the same notebook.
The best solution to this is to install rpy2 (requires a working version of R as well), which can be easily done with pip.
pip install rpy2
You can then use the two languages together, and even pass variables inbetween:
%load_ext rpy2.ipython
%R require(ggplot2)
array([1], dtype=int32)
import pandas as pd
df = pd.DataFrame({
‘Letter’: [‘a’, ‘a’, ‘a’, ‘b’, ‘b’, ‘b’, ‘c’, ‘c’, ‘c’],
‘X’: [4, 3, 5, 2, 1, 7, 7, 5, 9],
‘Y’: [0, 4, 3, 6, 7, 10, 11, 9, 13],
‘Z’: [1, 2, 3, 1, 2, 3, 1, 2, 3]
})
%%R -i df
ggplot(data = df) + geom_point(aes(x = X, y= Y, color = Letter, size = Z))