Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Jupyter notebooks features

$
0
0
Jupyter (Ipython) notebooks features

It is very flexible tool to create readable analyses, because one can keep code, images, comments, formula and plots together:


Jupyter notebooks features

Jupyter is quite extensible, supports many programming languages, easily hosted on almost any server ― you only need to have ssh or http access to a server. And it is completely free.

List of hotkeys is shown in Help > Keyboard Shortcuts (list is extended from time to time, so don't hesitate to look at it again).

This gives an idea of how you're expected to interact with notebook. If you're using notebook constantly, you'll of course learn most of the list. In particular:

Esc + F Find and replace to search only over the code, not outputs Esc + O Toggle cell output You can select several cells in a row and delete / copy / cut / paste them. This is helpful when you need to move parts of a notebook
Jupyter notebooks features
Sharing notebooks

Simplest way is to share notebook file (.ipynb), but not everyone is using notebooks, so the options are

convert notebooks to html file share it withgists, which are rendering the notebooks. See this example store your notebook e.g. in dropbox and put the link to nbviewer . nbviewer will render the notebook github renders notebooks (with some limitations, but in most cases it is ok), which makes it very useful to keep history of your research (if research is public) Plotting in notebooks

There are many plotting options:

matplotlib (de-facto standard), activated with %matplotlib inline %matplotlib notebook is interactive regime, but very slow, since rendering is done on server-side. mpld3 provides alternative renderer (using d3) for matplotlib code. Quite nice, though incomplete bokeh is a better option for building interactive plots plot.ly can generate nice plots, but those will cost you money
Jupyter notebooks features

Magics are turning simple python into magical python . Magics are the key to power of ipython.

In[1]:

# list available python magics %lsmagic

Out[1]:

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 %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 %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics.

You can manage environment variables of your notebook without restarting the jupyter server process. Some libraries (like theano) use environment variables to control behavior, %env is the most convenient way.

In[2]:

# %env - without arguments lists environmental variables %env OMP_NUM_THREADS=4

env: OMP_NUM_THREADS=4

Executing shell commands

You can call any shell command. This in particular useful to manage your virtual environment.

In[3]:

!pip install numpy !pip list | grep Theano

Requirement already satisfied (use --upgrade to upgrade): numpy in /Users/axelr/.venvs/rep/lib/python2.7/site-packages Theano (0.8.2)

Suppress output of last line

sometimes output isn't needed, so we can either use pass instruction on new line or semicolon at the end

In[4]:

%matplotlib inline from matplotlib import pyplot as plt import numpy

In[5]:

# if you don't put semicolon at the end, you'll have output of function printed plt.hist(numpy.linspace(0, 1, 1000)**1.5);


Jupyter notebooks features
See the source of python functions / classes / whatever with question mark (?, ??) In[6]:

from sklearn.cross_validation import train_test_split # show the sources of train_test_split function in the pop-up window train_test_split??

In[7]:

# you can use ? to get details about magics, for instance: %pycat?

will output in the pop-up window:

Show a syntax-highlighted file through a pager. This magic is similar to the cat utility, but it will assume the file to be Python source and will show it with syntax highlighting. This magic command can either take a local filename, an url, an history range (see %history) or a macro as argument :: %pycat myscript.py %pycat 7-27 %pycat myMacro %pycat http://www.example.com/myscript.py

%run to execute python code

%run can execute python code from .py files ― this is a well-documented behavior.

But it also can execute other jupyter notebooks! Sometimes it is quite useful.

NB. %run is not the same as importing python module.

In[8]:

# this will execute all the code cells from different notebooks %run ./2015-09-29-NumpyTipsAndTricks1.ipynb

[49 34 49 41 59 45 30 33 34 57] [172 177 209 197 171 176 209 208 166 151] [30 33 34 34 41 45 49 49 57 59] [209 208 177 166 197 176 172 209 151 171] [1 0 4 8 6 5 2 9 7 3] ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'] ['b' 'a' 'e' 'i' 'g' 'f' 'c' 'j' 'h' 'd'] ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'] [1 0 6 9 2 5 4 8 3 7] [1 0 6 9 2 5 4 8

Viewing all articles
Browse latest Browse all 9596

Trending Articles