As mentioned in the instructions, all materials can be open in Colab as Jupyter notebooks. In this way users can run the code in the cloud. It is highly recommanded to follow the tutorials in the right order.


What is Jupyter?

The Jupyter Project is an open source effort that evolved from the IPython project to support interactive data science and computing. Besides Python, it also supports many different programming languages including R and Julia.

Components of Jupyter Notebook

  • Jupyter Notebook IDE: The application that launches in a web browser like Firefox or Safari and is the environment where you write and run your code.

  • Jupyter Notebook Files(.ipynb): The file format that you can use to store code and markdown text for individual projects and workflows.

  • Kernels: A kernel runs your code in a specific programming language. In this tutorial, Python kernel is used within the Jupyter Notebook IDE.

Jupyter Notebook User Interface

After you create a new notebook file (.ipynb), you will be presented with notebook name, menu bar, tool bar and a code cell as default starting cell.

  • Notebook Name: if you click at the notebook name, you could rename the file.

  • Menu Bar: presents all functions and settings of the notebook file.

  • Tool Bar: presents the most used tools as icons.

  • Code Cell: it is the default type of cell when you create a new cell; if you want to transfer it to a markdown cell, you could use the drop down box in tool bar or a keyboard shortcut.



1. Headers

Note: The more # the title holds, the smaller the title will be.

# This is a Title in Markdown

## This is a Subtitle in Markdown

### This is a smaller subtitle in Markdown


will be rendered as:

This is a Title in Markdown

This is a Subtitle in Markdown

This is a smaller subtitle in Markdown



2. Lists

* This is a bullet list

* This is a bullet list

* This is a bullet list


1. This is a numbered list

2. This is a numbered list

3. This is a numbered list


will be rendered as:

  • This is a bullet list
  • This is a bullet list
  • This is a bullet list
  1. This is a numbered list
  2. This is a numbered list
  3. This is a numbered list

Tip: To render list, you should leave a blank space between 1. and the following texts.



3. Bold and Italic

*These are italic words.*

**These are bold words.**

***These are bold AND italic words.***


will be rendered as


These are italic words.

These are bold words.

These are bold AND italic words.




4. Highlight Code

If you want to highlight a funciton or some code in a plain text, you add one backtick on each side of the text (`).

Here is some highlighted text!




5. Horizontal Lines

You can also create a horizontal line to highlight a block of markdown syntax.


***

Here is some important text!

***


will be rendered as:



Here is some important text!



Tip: The *s on both side of the texts should be at the line above and the line below the texts.



You can use HTML in Markdown cells to create hyperlinks redirecting to other websites. For example, the following syntax.

More infos about our data cube program can be found at <a href="https://datacube.remote-sensing.org/">this website</a>


will be rendered as:

More infos about our data cube program can be found at this website



7. Images

You can also render images in Markdown cells using the following syntax:

![alternative text here](url-to-image-here)


For example,

![Fotograph of Philip is here](https://i.imgur.com/VGPeJ6s.jpg)


will be redered like:

Fotograph of Philip is here

Or if the image need to be resize:

<div>

<img src=https://i.imgur.com/VGPeJ6s.jpg width="200">

</div>


will be rendered as:

Note: The texts in the rectangle brackets (e.g. "Fotograph of Philip is here") will appear when the image fails to load.




8. LaTex

Jupyter notebook markdown cell also supports LaTex. So that the markdown cells interpret your texts as LaTex, surround your input texts with $ signs.

For instance, $c=a+b$ will be rendered as

$c=a+b$

If you want your texts be centered in the cell, surround your input texts with two $ signs.

For instance, $$C_{g}=\frac{H}{\frac{\pi}{2}*Cl_{p}}$$ will be rendered as

$$C_{g}=\frac{H}{\frac{\pi}{2}*Cl_{p}}$$


and


|Uppercase| LaTeX |Lowercase| LaTeX |

|---------|-------|---------|-------|

|$\Delta$ |\\Delta|$\delta$ |\\delta|

|$\Omega$ |\\Omega|$\omega$ |\\omega|


will be rendered as:


Uppercase LaTeX Lowercase LaTeX
$\Delta$ \Delta $\delta$ \delta
$\Omega$ \Omega $\omega$ \omega

9. Table

A table can be constructed using | (pipe symbol) and — (dash) to mark columns and rows. The first row of the table defines the headers, and the next row defines the alignment of each column.


For instance,

| Stretch/Untouched | ProbDistribution | Accuracy |

| --- | --- | --- |

| Stretched | Gaussian | .843 |


will be rendered as:

Stretch/Untouched ProbDistribution Accuracy
Stretched Gaussian .843

The widths of the columns can also be changed by adding an empty row at the end with defined width.


| Stretch/Untouched | ProbDistribution | Accuracy |

| --- | --- | --- |

| Stretched | Gaussian | .843 |

|<img width=200/>|<img width=200/>|<img width=200/>|


will be rendered as:


Stretch/Untouched ProbDistribution Accuracy
Stretched Gaussian .843

What is Colab?

So now we learnt about Jupyter Notebook, then what is Colab? Colab notebooks are Jupyter notebooks that are hosted by Colab. Therefore, the operations are highly similar and it allows you to write and execute Python in your browser, with

  • Zero configuration required
  • Free access to GPUs
  • Easy sharing

There are limits in the available memory but they are generally sufficient if you are not performing particularly demanding tasks. One of the benefits using Colab is that you do not need to worried about dependencies as much for downloading Python tools and libraries. Many of the libraries are available in the Colab environment by default, eg. Pandas.

To import a library that's not in Colaboratory by default, you can also use !pip install or !apt-get install.

For example,

!pip install cartopy
import cartopy
Collecting cartopy
  Downloading Cartopy-0.20.1.tar.gz (10.8 MB)
     |████████████████████████████████| 10.8 MB 5.1 MB/s 
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
WARNING: Discarding https://files.pythonhosted.org/packages/fc/59/aa52698e3838f4cd0e7eaa75bd86837e9e0b05041dbdaee3cda2fffced06/Cartopy-0.20.1.tar.gz#sha256=91f87b130e2574547a20cd634498df97d797abd12dcfd0235bc0cdbcec8b05e3 (from https://pypi.org/simple/cartopy/) (requires-python:>=3.7). Command errored out with exit status 1: /usr/bin/python3 /usr/local/lib/python3.7/dist-packages/pip/_vendor/pep517/in_process/_in_process.py get_requires_for_build_wheel /tmp/tmpvn613tgl Check the logs for full command output.
  Downloading Cartopy-0.20.0.tar.gz (10.8 MB)
     |████████████████████████████████| 10.8 MB 20.5 MB/s 
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
WARNING: Discarding https://files.pythonhosted.org/packages/0f/c0/58453b036e79046d211f083880d58dcce787e7e07647ac25dc46c6555099/Cartopy-0.20.0.tar.gz#sha256=eae58aff26806e63cf115b2bce9477cedc4aa9f578c5e477b2c25cfa404f2b7a (from https://pypi.org/simple/cartopy/) (requires-python:>=3.7). Command errored out with exit status 1: /usr/bin/python3 /usr/local/lib/python3.7/dist-packages/pip/_vendor/pep517/in_process/_in_process.py get_requires_for_build_wheel /tmp/tmpcxegil_v Check the logs for full command output.
  Downloading Cartopy-0.19.0.post1.tar.gz (12.1 MB)
     |████████████████████████████████| 12.1 MB 19.6 MB/s 
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
    Preparing wheel metadata ... done
Requirement already satisfied: shapely>=1.5.6 in /usr/local/lib/python3.7/dist-packages (from cartopy) (1.8.0)
Collecting pyshp>=2
  Downloading pyshp-2.1.3.tar.gz (219 kB)
     |████████████████████████████████| 219 kB 48.7 MB/s 
Requirement already satisfied: numpy>=1.13.3 in /usr/local/lib/python3.7/dist-packages (from cartopy) (1.19.5)
Building wheels for collected packages: cartopy, pyshp
  Building wheel for cartopy (PEP 517) ... done
  Created wheel for cartopy: filename=Cartopy-0.19.0.post1-cp37-cp37m-linux_x86_64.whl size=12516269 sha256=983da772a11f3cba2d78a180f9a2d0136b8d43ba98219da421e264ca5b01d995
  Stored in directory: /root/.cache/pip/wheels/98/01/f7/bd10aeb96fe4b518cde5f7c4f5e12c7202f85b7353a5017847
  Building wheel for pyshp (setup.py) ... done
  Created wheel for pyshp: filename=pyshp-2.1.3-py3-none-any.whl size=37325 sha256=1c8e9a249f1f0ed81fac1f63ab6c703038812764fe6dd1b67b62e911ebaab64e
  Stored in directory: /root/.cache/pip/wheels/43/f8/87/53c8cd41545ba20e536ea29a8fcb5431b5f477ca50d5dffbbe
Successfully built cartopy pyshp
Installing collected packages: pyshp, cartopy
Successfully installed cartopy-0.19.0.post1 pyshp-2.1.3

Also, because Colab is a cloud environment, you cannot directly assess files in your local environment. If you want to import files, there are two options:

Data Import


Option 1: Upload Files


Remarks: The file name in line pd.read_csv() need to be exactly the same as the name of the file you uploaded to not run into errors.

from google.colab import files # use the google.colab library
uploaded = files.upload() # upload it, click choose file after running this cell and pick the file

# read your file depends on the file format
# Let's say you have a csv, then you can read them using Pandas (you will learn more about Pandas later)
import io
import pandas as pd

df = pd.read_csv(io.BytesIO(uploaded['yourfile.csv']))
df # now your data is stored in this data frame

Option 2: Access Files from Google Drive

One of the cons using upload option is that you can only upload one file at a time and it might take sometimes if your files are large. Let's say your colleague share you a file from Google Drive and you have a copy of it in the Drive. It is probably not the best way to download them to your computer and upload them in Colab again.

What you can do is to directly use files on the drive.

After running the following code, you still need to sign in to your Google Account, and click Allow to permit access to your own Google Drive.

from google.colab import drive
drive.mount('/content/drive/')
Mounted at /content/drive/

After successful connection, you can see "Mounted at / content/drive/" under the cell.

Then, you can check your directory.

!ls
drive  sample_data

Where to search for your files depends on where do you store them. Let's say you stored your file inside Google Drive with a folder call yourfolder. You can check if the files are there by:


Remark: Be carefule there is no space between My and Drive

print(os.popen('ls /content/drive/MyDrive/yourfolder').read())

Your files should appear under the cell after run.

Then you can open your file using the path. You can specify your path in a raw string r"path".

Then you can open them using different methods depending on the data format. The following example is a Geotiff file and it can be opened using gdal (You do not need to know much about it for now).

path = r"/content/drive/MyDrive/yourfolder/boundary.tif"

# import library
import gdal

# open file
ds = gdal.Open(path, gdal.GA_ReadOnly)
# import library
import pandas as pd

# define your path
path = r"/content/drive/MyDrive/yourfolder/city.csv"

# open file
df = pd.read_csv(path)
df # now your data is stored in this data frame

Data Export


It is basically very similar when you need to output a file.

Option: Directly Download Files


Remarks: The file name in the second and third line need to be exactly the same to not run into errors.

from google.colab import files # import library; only need to be done once for the whole notebook 

df.to_csv('temple_location.csv', encoding='utf_8_sig', index=False) # suppose df is the dataframe you want to export
files.download('temple_location.csv') # run this code and the file will be directly downloaded

What you can also do it to run only:

from google.colab import files # import library; only need to be done once for the whole notebook 

df.to_csv('temple_location.csv', encoding='utf_8_sig', index=False) # suppose df is the dataframe you want to export

# by default they will be in /content

and then click the bar on the left side with the folder icon (Files). Then you can also search for your files in the directory and click on the three dots and then click download.

It is generally recommanded to modify a file already existed in the Google Drive but not to create new files in Google Drive.




Previous Lesson: Python Research Q&A Basics

Next Lesson: Python Introduction Basics


Additional information

This notebook is provided for educational purpose and feel free to report any issue on GitHub.


Author: Ka Hei, Chow

License: The code in this notebook is licensed under the Creative Commons by Attribution 4.0 license.

Last modified: December 2021




References:

https://colab.research.google.com/?utm_source=scs-index