PSY 381D Brain Connectivity, Spring 2019
Jupyter notebook
January 28, 2019
A Jupyter notebook is an interactive document that lets you run Python right there in your document. It is an ideal tool for learning how to code and demonstrating outputs and plots generated by a code. You can view Jupyter notebook documents (with .ipynb
extension) on GitHub with your web browser. However, in order to run code snippets on a Jupyter notebook document, you need to open the .ipynb
document on your computer (or your Docker container).
If you are running a Docker image for this course, then you can start Jupyter on your container with jnb
command, and follow the instructions given in the Docker documentation to open the Notes folder on your web browser (on your local computer).
From the Anaconda Navigator, you can launch Jupyter notebook.
If you are using any other distribution of Python, you must open a Terminal (Mac) or Command Prompt (Windows) app. Then type in the command:
jupyter notebook
As starting up, Jupyter should look something like this.
Using the file browser within your web browser (not the browser's menu), find a Jupyter notebook document. It should look something like this:
Within a notebook document, there are some codes that can be executed on the spot. You simply find the code snippet, a box with In [ ]:
on the left margin.
You can click on this cell to select. If you happen to click on the text area, then you will see the cell selected with a green border.
Or if you happen to click anywhere else on the cell, the you will see the cell with a blue border.
Once the cell is selected (blue or green), you can run that code by pressing the run button
within the browser window on the top. Then the code is executed, and the output is produced. You can see the output immediately below the code cell.
print("Hello World!")
yourname = 'Satoru'
print("Hello, " + yourname + "!!")
You can edit and re-run the code within the notebook as well. Just click on the text of the code cell (so that it's green) and make an edit.
Here, I am changing the variable yourname
from Satoru
to Hayasaka
. Then I can just re-run the code by clicking on the run button again.
print("Hello World!")
yourname = 'Hayasaka'
print("Hello, " + yourname + "!!")
Jupyter notebook by default auto-saves any edits frequently. You can also click on the Save button to save any changes made to your Jupyter notebook.
When you are done with a Jupyter notebook, there are multiple ways to close it. This is the way I do it:
y
.Jupyter notebook documents are great for demonstrating and teaching Python coding, but not an ideal platform for sharing actual Python codes. Typically sharing and exchanging of Python codes are done by exchanging source codes, as opposed to exchanging Jupyter notebook documents. A source code is a file consisting of lines of Python codes that is able to call libraries / functions, and execute a set of instructions. As long as people have the right version of Python and required set of libraries, such source codes can be executed on any computers.
To save a source code from a cell, you need to write a line
%%writefile MyProgram.py
For example,
%%writefile MyProgram.py
print('Hello world!')
print('Hello Python!')
Then the content of the code cell is saved in a file called MyProgram.py
. If you want to append any additional lines to this program, then you can use the -a
option.
%%writefile -a MyProgram.py
print('Additional line 1')
print('Additional line 2')
To load an existing source code into a Jupyter notebook document, you can use
%load MyProgram.py
in a code cell. Then the content of the source code is loaded to that cell.
# %load MyProgram.py
print('Hello world!')
print('Hello Python!')
print('Additional line 1')
print('Additional line 2')
If you run this cell, then %load MyProgram.py
is ignored and you can run the content of the loaded source code.