Jupyter Notebook Hacks

Hello Everyone! I am documenting some of the useful shortcuts & commands I learnt. I will be adding more. Feel free to add anything important I have missed from class.

  • Shift+Enter (run cell, select below)

  • ALT+Enter (run cell, insert below)

  • o (to hide output of any cell)

  • SHIFT + TAB (documentation of python command)

  • shift + ctrl + minus(-) (split cell at cursor)

  • shift + M (merge selected cells)

  • 1 (to heading 1)

  • 2,3,4,5,6 (to heading 2,3,4,5,6)

  • ! ( to use terminal)

  • ! head {Python Object} (Using python objects within terminal commands)

16 Likes

Nice. Easier way to hide output: o

thanks! I will update

two that I particularly enjoy:

shift + ctrl + minus(-)
split cell at cursor

shift + M
merge selected cells

3 Likes

yeah! that’s cool

I’ve been using shortcuts listed below a lot recently :slightly_smiling_face:

esc then ‘A’ to add a new cell below
esc then ‘B’ to add a new cell below
esc then ‘D’ then ‘D’ to delete current cell

1 Like
  • x: cut
  • c: copy
  • v: paste
2 Likes

Did anyone find some way to swap code chunks? There have been a few times where I wanted to swap chunks, e.g. moving one chunk with import packages above other. I had to use copy-paste

m - convert to Markdown
y - convert to Code

2 Likes

I just hit x then v to move cells. Works pretty well. Use shift with arrow keys to select multiple cells.

2 Likes

Gang, note that keyboard shortcuts are case sensitive. So better to use lowercase letters when telling about keyboard shortcuts that don’t use the shift key, otherwise it can get confusing!

2 Likes

personally a fan of this site. covers most of what’s been mentioned here but could be useful later: https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

1 Like

To convert a jupyter notebook to a .py file.

jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb

3 Likes

Even easier: click “Download as” in the notebook “File” menu! :slight_smile:

5 Likes

Is it possible to access to two Jupiter notebooks in two AWS instances. I played around with port numbers. Didn’t work out. Please share if someone knows how to do it.

Each instance will have a different IP address. Are checking the notebooks are running at the separate IPs at port 8888?

You can use a terminal multiplexer like tmux to run two different notebook instances on the same server. Juypter Notebooks will put themselves on port 8888 and if that port is being used, it puts itself on port 8889 usually.

When you are connecting to AWS instance 1, use the following :
ssh -i .ssh/id_rsa -L8011:localhost:8011 user@ip.address

Open Jupyter by :
jupyter notebook --port 8011

Then on 2nd instance, use the following :
ssh -i .ssh/id_rsa -L8012:localhost:8012 user@ip.address

Here open jupyter notebook using :
jupyter notebook --port 8012

The point is to connect with ssh using different tunnels and open jupyter notebook on different ports.

2 Likes

Does anyone know how to hide a cell in the notebook? I’d like to prevent code, but still show the results in the output document.
I looked up solutions online but there is no simple way to do it. I thought there was something similar to echo = F in R.
Thanks!

Did you try using o key.?

o hides the output but not the code cell.