Gangmax Blog

IPython, Jupyter & Miniconda

The content of this post is from here.

Miniconda

Download Miniconda package from here.

1
2
3
4
5
6
7
8
9
10
11
# 1. Install "miniconda".
bash ./Miniconda3-latest-MacOSX-x86_64.sh
# You can change the installation directory in the installation process.
# Note that the installation script will tell you it adds content to your
# "~/.bash_profile" file. If you are using zsh like me, remember copy the
# adding line to your "~/.zshrc" file. And if you are also "pyenv" user
# like me, do remember "conda" overrides your Python environment provided
# by "pyenv".
# 2. Install conda package.
conda install numpy
conda install jupyter

Document about conda can be found here.

Added on 2018-04-28: Here is a better way to install “miniconda” by “pyenv”.

  • Remove standalone “miniconda3”

From here.

1
2
rm -rf miniconda3
rm -rf ~/.condarc ~/.conda ~/.continuum
  • Install “miniconda3” by “pyenv”

From here.

1
2
3
4
5
6
pyenv install --list
pyenv install miniconda3-4.3.30
pyenv local miniconda3-4.3.30
which conda
conda install jupyter
jupyter notebook

Jupyter

After you install Jupyter via conda, you can run it by:

1
jupyter notebook

IPython Notebook

The command will start a web server listening on the “8888” port and you will be navigated to the URL “http://localhost:8888/“ automatically. In the web page, go to “New -> Notebook: Python 3”. A new browser tab is opened which is an IPython notebook and a corresponding “Untitled.ipynb” file is created to save the notebook. You can run Python code and shell commands there by typing the content and press “Shift + Enter” to execute.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
>>> 2 + 2
4


>>> _ * 3
12


# Run shell command.
>>> !ls
my_notebook.ipynb


# List IPython 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 %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 %%markdown %%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.


# Run magic command.
>>> %%writefile test.txt
Hello world!
Writing test.txt


# Run Python code to verify the execution result of the magic command above.
>>> # Let's check what this file contains.
with open('test.txt', 'r') as f:
print(f.read())
Hello world!


# Show the help information of a magic command.
>>> %run?
Docstring:
Run the named file inside IPython as a program.

Usage::

%run [-n -i -e -G]
[( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
( -m mod | file ) [args]


# Switch "Code cell" to "Markdown" cell before running the following
# code to render markdown content.
>>> $$\hat{f}(\xi) = \int_{-\infty}^{+\infty} f(x) \, \exp \left(-2i\pi x \xi \right) dx$$
>>> ![This is a image](http://jupyter.org/assets/nav_logo.svg)


### Run the following code to display rich web content.
>>>
from IPython.display import HTML, SVG, YouTubeVideo

HTML('''
<table style="border: 2px solid black;">
''' +
''.join(['<tr>' +
''.join([f'<td>{row},{col}</td>'
for col in range(5)]) +
'</tr>' for row in range(5)]) +
'''
</table>
''')

SVG('''<svg width="600" height="80">''' +
''.join([f'''<circle
cx="{(30 + 3*i) * (10 - i)}"
cy="30"
r="{3. * float(i)}"
fill="red"
stroke-width="2"
stroke="black">
</circle>''' for i in range(10)]) +
'''</svg>''')

YouTubeVideo('VQBZ2MqWBZI')

Comments