Notebook 3.1: Jupyter notebooks in the cloud

Learning objectives

This notebook is meant to provide an introduction to using jupyter notebooks. By the end of this notebook you should:

  1. Be able to recognize assigned questions or actions in notebooks.
  2. Know how to enter text (markdown) or code into notebooks.
  3. Know how to complete and save assigned notebooks.
Note: Questions that require you to respond using Markdown (text) or Actions that require you to edit and execute code to produce new results will be highlighted on a green background. You should always execute all code blocks in a notebook in order from the top to the bottom, which will not necessarily be highlighted in green, but if we ask you to edit a code block it will be highlighted.

Jupyter notebooks

The Jupyter Notebook is a powerful tool for interactively developing and presenting data science projects. It allows you to use a web browser (as opposed to a terminal or other software) to interact with a computer language interpreter (typically Python) to send commands and receive outputs. It then displays the outputs in HTML below the executed cells, and allows users to add rich text comments or annotations around code cells using markdown.

How we will use notebook

For this class, we will be using notebooks that run on a server in the cloud. This is convenient because you will never have to install software on your own computer and can just connect to your browser to complete assignments or exercises. It does not matter whether you are using Mac, Windows, Linux, Chrome, etc.

For your own future reference, you can install jupyter notebooks onto your own computer to use as a research tool. There are many resources online to learn more about this but it is outside the scope of this class.

Markdown cells

Double click on the text below that says "Hello world". When you do you should see the text open up into an interactive cell. This cell is set up to execute a type of code called Markdown, which is a language for formatting text, much like HTML (which is called a MarkUp language). Markdown is much easier to learn than HTML, and for that reason has become a very popular tool for simple formatting and styling of text. To transform the cell back into rendered text you must execute it, which can be done by pressing the 'Run' button in the notebook tool bar while the cell is selected, or, by holding down the control key and pressing enter. We recommend that you learn the hotkey commands, since it will make you a more efficient notebook user. You can click on the keyboard icon in the toolbar to find a list of hotkey commands, or find many resources online (like this one).

Hello World

Action: In the markdown cell below convert the top line of text into a level-3 header (large and bold text) by double clicking on the cell so that you can interact with the text, and then adding three hash marks (#) before the first line without any spaces separating the hash marks, and with one space between the hashes and the text (like in the Hello World cell above). Then hold control and press enter to execute the cell. You should see large bold formatted text on the first line, followed by normal formatted text for the second line. If not, try editing the text again, you may have included spaces, or other errors.

My first executed Markdown cell

The text on the line above this is a level-3 header.

Code cells

In the menu and tool bar you can find options to change the type of cells. The two main types we are interested in are Markdown cells, as you just saw above, and Code cells, which are used to execute code. Most of the time code cells will produce some kind of output which is returned when the code is executed. In jupyter notebooks the returned code will be saved in the output area below the executed cell.

As an example, let's execute the cell below which contains two Python commands, one storing an integer as a variable x, and a second command to print the variable's value. The code also includes lines that start with hash marks (#) which are called comment lines, these are not executed and are meant for leaving notes about the code.


In [1]:
# store value to the variable x
x = 3

# print the variable x
print(x)


3

The default language in Jupyter notebooks is Python, but we can execute some other languages as well. The bash language is one of several related languages used in a command line terminal to execute programs. We can use it in Jupyter notebooks by simply appending %%bash to the top of the cell. You can see that the syntax to perform the same task we did above in Python is similar but a little different in bash. Learning the idiosyncracies of different coding languages takes time, and it is easiest to learn by example.


In [2]:
%%bash

# store value to a variable x
x=3

# print the variable
echo $x


3

Coding in Evolutionary Biology

It is OK if you are unfamiliar with these coding languages. You will not need to write your own code for the purposes of this course, but simply to try to interpret or modify written code so that you can learn about how modern evolutionary biologists use code.

Action: The cell below is currently set to execute as a markdown cell, meaning the contents are formatted as text when it is executed. However, the content of the cell is actually proper Python code (which is why it looks quite jumbled as markdown). Therefore, to make it display properly you need to change the cell type so it will execute as a code cell instead of markdown. Convert the cell type to a Code cell and execute it. If done correctly it should return an output message telling you that you did it correctly. Understanding the actual Python code itself within the cell is not of particular concern for now.

from IPython.display import HTML def success_message(): msg = "Great job, you completed the tutorial" fmt = "

{}

" return HTML(fmt.format(msg))

success_message()

Submitting assignments

Jupyter notebooks auto-save every few minutes, which helps to keep you from losing your progress if you disconnect from the notebook, or accidentally delete something important. You can always restart a notebook from the toolbar on top if you somehow crash it. When you reach the end of a notebook you should look back over the entire document to make sure you have answered all of the required questions or actions. If you have, then save the document one last time by clicking on Save and Checkpoint in the File Menu (or click on the disk in the upper left).

When you are finished with a notebook and ready to submit it we ask that you download an HTML version of the notebook to submit. To do this, go to the 'File Menu', then 'Download as', and select HTML. Upload the HTML notebook to Courseworks to submit your assignment.