27 Jupyter Notebook tips, tricks and shortcuts
This post originally appeared on Alex Rogozhnikov’s blog, ‘Brilliantly Wrong’ . Alex has graciously let us republish his post here.Jupyter Notebook
Jupyter notebook, formerly known as the Ipython notebook, is a flexible tool that helps you create readable analyses, as you can keep code, images, comments, formulae and plots together.
Jupyter is quite extensible, supports many programming languages and is easily hosted on your computer or on almost any server ― you only need to have ssh or http access. Best of all, it’s completely free.
The Jupyter interface.
By default, Jupyter works with Python kernels - hence its historical name as the IPython notebook. Jupyter notebook is produced by the Jupyter project - the name Jupyter is an indirect acronyum of the three core languages it was designed for: JU lia, PYT hon, and R and is inspired by the planet Jupiter. We’re going to show you 27 tips and tricks to make your life working with Jupyter easier.1. Keyboard Shortcuts
As any power user knows, keyboard shortcuts will save you lots of time. Jupyter stores a list of keybord shortcuts under the menu at the top: Help > Keyboard Shortcuts . It’s worth checking this each time you update Jupyter, as more shortcuts are added all the time.
Another way to access keyboard shortcuts, and a handy way to learn them is to use the command palette: Cmd + Shift + P (or Ctrl + Shift + P on linux and windows). This dialog box helps you run any command by name - useful if you don’t know the keyboard shortcut for an action or if what you want to do does not have a keyboard shortcut. The functionality is similar to Spotlight search on a Mac, and once you start using it you’ll wonder how you lived without it!
The command palette.
Some of my favorites:Esc + F Find and replace on your code but not the outputs. Esc + O Toggle cell output. Select Multiple Cells:
- 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. You can also use Shift + M to merge multiple cells.
Merging multiple cells.2. Pretty Display of Variables
The first part of this is pretty widely known. By finishing a Jupyter cell with the name of a variable or unassigned output of a statement, Jupyter will display that variable without the need for a print statement. This is especially useful when dealing with Pandas DataFrames, as the output is neatly formatted into a table.
What is known less, is that you can alter a modify the ast_note_interactivity kernel option to make jupyter do this for any variable or statement on it’s own line, so you can see the value of multiple statements at once.In:
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = "all"In:
from pydataset import data quakes = data('quakes') quakes.head() quakes.tail()Out: lat long depth mag stations 1 -20.42 181.62 562 4.8 41 2 -20.62 181.03 650 4.2 15 3 -26.00 184.10 42 5.4 43 4 -17.97 181.66 626 4.1 19 5 -20.42 181.96 649 4.0 11 Out: lat long depth mag stations 996 -25.93 179.54 470 4.4 22 997 -12.28 167.06 248 4.7 35 998 -20.13 184.20 244 4.5 34 999 -17.40 187.80 40 4.5 14 1000 -21.59 170.56 165 6.0 119
If you want to set this behaviour for all instances of Jupyter (Notebook and Console), simply create a file ~/.ipython/profile_default/ipython_config.py with the lines below.c = get_config() # Run all nodes interactively c.InteractiveShell.ast_node_interactivity = "all" 3. Easy links to documentation
Inside the Help menu you’ll find handy links to the online documentation for common libraries including NumPy, Pandas, SciPy and Matplotlib.
Don’t forget also that by prepending a library, method or variable with ? , you can access the Docstring for quick reference on syntax.In:
?str.replace()Docstring: S.replace(old, new[, count]) -> str Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. Type: method_descriptor 4. Plotting in notebooks
There are many options for generating plots in your notebooks.- matplotlib (the de-facto standard), activate
本文开发（python）相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程