Jupyter Tips

feature image

Basic Keyboard Shortcuts

  • Shift + Enter to run cell and move to next cell
  • Ctrl + Enter to run cell but stay in the same cell
  • Escape to leave cell
  • Enter to enter cell
  • a to add a cell above
  • b to add a cell below
  • dd to delete a cell
  • m to change cell to Markdown (after pressing escape)
  • y to change cell to Code (after pressing escape)
  • Arrow keys move cells (after pressing escape)

Tab Completion

listing available modules on import

  • import <tab>
  • from numpy import <tab>

listing available modules after import

  • np.<tab>

function completion

  • np.ar<tab>
  • sor<tab>([2, 3, 1])

variable completion

  • myvar_1 = 5
  • myvar_2 = 6
  • my<tab>

listing relative path directory contents

  • ../<tab>
  • (then press enter on a folder and tab again to show its contents)

Jupyter Magic Functions

List of the available magic commands

%lsmagic

Available line magics:
%alias  %alias_magic  %autocall  %automagic  %autosave  %bookmark  %cat  %cd  %clear  %colors  %config  %connect_info  %cp  %debug  %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %gui  %hist  %history  %killbgscripts  %ldir  %less  %lf  %lk  %ll  %load  %load_ext  %loadpy  %logoff  %logon  %logstart  %logstate  %logstop  %ls  %lsmagic  %lx  %macro  %magic  %man  %matplotlib  %mkdir  %more  %mv  %notebook  %page  %pastebin  %pdb  %pdef  %pdoc  %pfile  %pinfo  %pinfo2  %pip  %popd  %pprint  %precision  %profile  %prun  %psearch  %psource  %pushd  %pwd  %pycat  %pylab  %qtconsole  %quickref  %recall  %rehashx  %reload_ext  %rep  %rerun  %reset  %reset_selective  %rm  %rmdir  %run  %save  %sc  %set_env  %shell  %store  %sx  %system  %tb  %tensorflow_version  %time  %timeit  %unalias  %unload_ext  %who  %who_ls  %whos  %xdel  %xmode


Jupyter Magic Functions

Available cell magics:

%%!  %%HTML  %%SVG  %%bash  %%bigquery  %%capture  %%debug  %%file  %%html  %%javascript  %%js  %%latex  %%perl  %%prun  %%pypy  %%python  %%python2  %%python3  %%ruby  %%script  %%sh  %%shell  %%svg  %%sx  %%system  %%time  %%timeit  %%writefile

Automagic is ON, % prefix IS NOT needed for line magics.

Display plots inline

Run this before you plot the chart (especially for matplotlib) so the chart is display in the browser without refresh.

%matplotlib inline

Timer

Time execution of a Python statement or expression.

%time
CPU times: user 2 µs, sys: 1 µs, total: 3 µs
Wall time: 5.01 µs

Shell

Run one bash command in jupyter by adding “!” in the front.

!ls
!touch test.txt

Bash

Run full bash script in Jupyter.

%%bash

ls -al
rm test.txt
echo"hello jupyter" > test.txt
open test.txt

Load SQL

SQL is not loaded in Jupyter by default.

%%sql sqlite://

Output:

UsageError: Cell magic `%%sql` not found.

SQL does not come with Jupyter. To install SQLite.

!pip install ipython-sql

Output:

Collecting ipython-sql
  Downloading ipython_sql-0.4.1-py3-none-any.whl (21 kB)
[…]

Load SQL in Jupyter.

%load_ext sql

Output:

UsageError: Cell magic `%%sql` not found.

Run SQL

Run a simple SQL statement.

%%sql sqlite://

SELECT * FROM (
    SELECT 'Hello' AS msg1
) A JOIN (
    SELECT 'Jupyter' AS msg2
) B;

## Install numpy, pandas, matplotlib

Jupyter is target for Machine Learning programmers. So it comes with numpy, pandas, matplotlib 3 libraries. You do not need to install them unless you want to update the libraries.

To learn more, please check out the documentation.

,