Jupyter notebook INTRODUCTION

Introduction to GIS scripting
May, 2017

© 2017, Stijn Van Hoey (mailto:stijnvanhoey@gmail.com). Licensed under CC BY 4.0 Creative Commons



In [1]:
from IPython.display import Image
Image(url='http://python.org/images/python-logo.gif')


Out[1]:

To run a cell: push the start triangle in the menu or type SHIFT + ENTER/RETURN

Notebook cell types

We will work in Jupyter notebooks during this course. A notebook is a collection of cells, that can contain different content:

Code


In [19]:
# Code cell, then we are using python
print('Hello DS')


Hello DS

In [20]:
DS = 10
print(DS + 5) # Yes, we advise to use Python 3 (!)


15

Writing code is what you will do most during this course!

Markdown

Text cells, using Markdown syntax. With the syntax, you can make text bold or italic, amongst many other things...

  • list
  • with
  • items

Link to interesting resources or images:

Blockquotes if you like them This line is part of the same blockquote.

Mathematical formulas can also be incorporated (LaTeX it is...) $$\frac{dBZV}{dt}=BZV_{in} - k_1 .BZV$$ $$\frac{dOZ}{dt}=k_2 .(OZ_{sat}-OZ) - k_1 .BZV$$

Or tables:

course points
Math 8
Chemistry 4

or tables with Latex..

Symbool verklaring
$BZV_{(t=0)}$ initiële biochemische zuurstofvraag (7.33 mg.l-1)
$OZ_{(t=0)}$ initiële opgeloste zuurstof (8.5 mg.l-1)
$BZV_{in}$ input BZV(1 mg.l-1.min-1)
$OZ_{sat}$ saturatieconcentratie opgeloste zuurstof (11 mg.l-1)
$k_1$ bacteriële degradatiesnelheid (0.3 min-1)
$k_2$ reäeratieconstante (0.4 min-1)

Code can also be incorporated, but than just to illustrate:

BOT = 12
print(BOT)

In other words, it is markdown, just as you've written in Rmarkdown (!)

See also: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet

HTML

You can also use HTML commands, just check this cell:

html-adapted titel with <h3>

Bold text <b> of or italic <i>

Headings of different sizes: section

subsection

subsubsection

Raw Text

Cfr. any text editor

Notebook handling ESSENTIALS

Completion: TAB

  • The TAB button is essential: It provides you all possible actions you can do after loading in a library AND it is used for automatic autocompletion:

In [21]:
import os
os.mkdir


Out[21]:
<function posix.mkdir>

In [22]:
my_very_long_variable_name = 3
my_ + TAB

Help: SHIFT + TAB

  • The SHIFT-TAB combination is ultra essential to get information/help about the current operation

In [23]:
round(3.2)


Out[23]:
3

In [24]:
os.mkdir


Out[24]:
<function posix.mkdir>

In [25]:
# An alternative is to put a question mark behind the command
os.mkdir?
EXERCISE:
  • What happens if you put two question marks behind the command?

In [26]:
import glob
glob.glob??

edit mode to command mode

  • edit mode means you're editing a cell, i.e. with your cursor inside a cell to type content --> green colored side
  • command mode means you're NOT editing(!), i.e. NOT with your cursor inside a cell to type content --> blue colored side

To start editing, click inside a cell or

To s stop edting,

new cell A-bove

Create a new cell above with the key A... when in command mode

new cell B-elow

Create a new cell below with the key B... when in command mode

CTRL + SHIFT + P

Just do it!

Trouble...

NOTE: When you're stuck, or things do crash:
  • first try **Kernel** > **Interrupt** -> you're cell should stop running
  • if no succes -> **Kernel** > **Restart** -> restart you're notebook

Overload?!?



No stress, just go to

`Help` > `Keyboard shortcuts`

  • Google search is with you!

REMEMBER: To run a cell: push the start triangle in the menu or type SHIFT + ENTER

some MAGIC...

%psearch


In [11]:
%psearch os.*dir

%%timeit


In [12]:
%%timeit

mylist = range(1000)
for i in mylist:
    i = i**2


306 µs ± 2.62 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [13]:
import numpy as np

In [14]:
%%timeit

np.arange(1000)**2


2.87 µs ± 73.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%lsmagic


In [15]:
%lsmagic


Out[15]:
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.

Let's get started!


In [16]:
from IPython.display import FileLink, FileLinks

In [17]:
FileLinks('.', recursive=False)




For now, we will work on the 01-python-introduction.ipynb notebook, which is a short introduction to some essential basic Python concepts required to start programming in Python.

Additional material is available here (largely adopted from the scientific python notes, which you can explore on your own to get more background on the Python syntax if specific elements would not be clear.


In [ ]: