Hands-On Jupyter Notebook Hacks. Hacks, tips and shortcuts you should be... | by Shinichi Okada | Towards Data Science

Table of ContentsIntroduction1. Gist2. display() over print() for dataFrame3. Output without print()4. Bash commands without !5. Other useful shortcuts6. Nbextensions7. Move Selected Cells8. Tabnine9. Autopep8
In this short article, I am going to write Jupyter Notebook tips, extensions and hacks that I found useful when working with Jupyter Notebook. For writing tips with Jupyter Notebook, please read this article.
The gist is a simple way to share snippets hosted by Github. This hack allows you to create a gist from your codes in a Juptyer notebook cell.
Assuming you have a Github account, first, you need to install gist. If you are using brew, please run the following in your terminal.
brew updatebrew outdatedbrew upgradebrew cleanupbrew install gist
Now you need to login to Github.
gist --loginObtaining OAuth2 access_token from GitHub.GitHub username: your-github-nameGitHub password: your-github-pw2-factor auth code:Success! https://github.com/settings/tokens
gist will allow you to upload the content from a file or the clipboard, adding a filename and description.
Put the following code in one of the cells in your Jupyter Notebook. You need to change myfile.py and my description .
%%bashgist -P -f myfile.py -d "my description"
Copy codes from a cell you want to send to Gist to your clipboard, CTRL+C/⌘C, then running the above cell will send your clipboard to Gist.
It returns your gist URL.
https://gist.github.com/b9e4b509cb6ed80631b617b53a65f0b9
When you want to update your gist, you need to use your local file name. You can add the following in a cell and run it to update the gist.
%%bashgist -u your-gist-id your-filename-with-extension
towardsdatascience.com
towardsdatascience.com
IPython.display is already installed in Jupyter Notebook. If you are printing one item, you don't need print() method. If you are printing out more than one item, display() will display nicer outputs.
import pandas as pddf = pd.DataFrame(    [        [48,22,33,47],        [35,36,42,27]    ],    index=["Male","Female"],    columns=["Black","White","Red","Blue"])print(df)display(df)df
If you feel too lazy to type print() for all outputs, then this is for you.
For Conda users, you need to add the following code in a cell.
from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity = "all"
Let’s test it.
testvar=2testvartestvar
This will return,
22
If you installed Jupyter Notebook with PIP, you can make it as the default without the above code. Please open ~/.ipython/profile_default/ipython_config.py with an editor and paste the following.
c = get_config()c.InteractiveShell.ast_node_interactivity = "all"
You need to restart Jupyter Notebook to make it work.
towardsdatascience.com
You can use bash commands without !. Try them out. Some examples you can use are:
ls -alpwdcat Readme.mdman lsmkdir newfolder
Please install Nbextensions before using the following extensions. The jupyter_contrib_nbextensions package contains a collection of community-contributed unofficial extensions that add functionality to the Jupyter notebook.
How to install jupyter_contrib_nbextensions
I use quite a few Nbextensions. Equation Auto Numbering, Select CodeMirror Keymap, Table of Contents (2), RISE. I’d like to show you three other extensions that are very useful to have.
towardsdatascience.com
Please install Move Selected Cells by enabling it from the Nbextensions tab.
This is super useful when you organize your cells. You can move a selected cell or cells with Option/Alt + Up/Down.
jupyter-tabnine enables the use of coding auto-completion based on Deep Learning.
Let’s install it.
pip install jupyter-tabninejupyter nbextension install --py jupyter_tabninejupyter nbextension enable --py jupyter_tabninejupyter serverextension enable --py jupyter_tabnine
You might restart your Jupyter Notebook a couple of times to work. You can see Tabnine in action in the following gif images.
The keyboard shortcuts for indentation are `CMD+]` for right indentation and `CMD+[` for left indentation.
The Nbextensions Autopep8 formats code in a code cell with one click.
You can install it using pip:
pip install autopep8
Then enable it in Nbextsions.
What is your favorite? Do you have anything else to share?
Get full access to every story on Medium by becoming a member.