Jupyter

From FarmShare

(Difference between revisions)
Jump to: navigation, search
(colors and details)
(trimming of technical details to separate section.)
Line 1: Line 1:
== Introduction ==
== Introduction ==
 +
[https://jupyter.org Project Jupyter] evolved out of the [https://ipython.org IPython] project (specifically the IPython notebook) with the goal to provide an interactive, web-browser driven, language-independent programming environment. Jupyter notebooks can be deployed on the FarmShare servers to enable an accessible, powerful, and persistent computational platform.
[https://jupyter.org Project Jupyter] evolved out of the [https://ipython.org IPython] project (specifically the IPython notebook) with the goal to provide an interactive, web-browser driven, language-independent programming environment. Jupyter notebooks can be deployed on the FarmShare servers to enable an accessible, powerful, and persistent computational platform.
Line 15: Line 16:
== Overview ==
== Overview ==
-
The guide consists of three sections: installation, Jupyter notebook server management, and client connection. In typical usage (i.e., once installation is complete and the Jupyter notebook server is running), only the client connection steps are necessary at every use.
+
The guide consists of three sections: [[Jupyter#Installation |installation]], [[Jupyter#Jupyter_server |Jupyter server]], and [[Jupyter#client_connection|client connection]].
-
 
+
* Installation is only performed once per user.
-
== Installation ==
+
* The Jupyter server needs to be started once per FarmShare machine used.
-
 
+
* Client connection must happen every time a client computer reconnects to the Internet (e.g., a laptop wakes from sleep).
-
Jupyter setup is best performed via the [https://en.wikipedia.org/wiki/Linux Linux] console. This guide will be mostly step-by-step, but general familiarity with Linux is helpful.
+
Commands to be typed in are in '''bold''' and text to substitute is in <font color=red>'''red'''</font>.
Commands to be typed in are in '''bold''' and text to substitute is in <font color=red>'''red'''</font>.
-
Useful tutorials are available as a [http://linuxcommand.org/tlcl.php free book], [http://linuxcommand.org/lc3_learning_the_shell.php online tutorial], or [https://www.youtube.com/playlist?list=PLII6oL6B7q78PKy6_R6JTkkYjVXZBZcVq video series].
+
== Installation ==
=== SSH into FarmShare ===
=== SSH into FarmShare ===
Line 33: Line 33:
substituting sunetid for <font color=red>'''jane'''</font>.
substituting sunetid for <font color=red>'''jane'''</font>.
-
[https://en.wikipedia.org/wiki/Secure_Shell SSH] provides an encrypted remote [https://en.wikipedia.org/wiki/Shell_(computing) shell] into another system and is the primary way that Jupyter will be installed and accessed.
+
A [https://en.wikipedia.org/wiki/Terminal_emulator |terminal application] and [https://en.wikipedia.org/wiki/Secure_Shell SSH] client are shipped with Mac and Linux systems. Windows does not come with an SSH client, but [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html putty] is a a free and lightweight SSH client for Windows.
-
 
+
-
A terminal program and SSH client is shipped with Mac and Linux systems. Windows does not come with an SSH client, but [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html putty] is a a free and lightweight SSH client for Windows.
+
=== Bind to Jupyter virtual environment ===
=== Bind to Jupyter virtual environment ===
Line 42: Line 40:
  jane@corn99:~$ '''source /afs/ir.stanford.edu/group/bil/env/jupyter/bin/activate'''
  jane@corn99:~$ '''source /afs/ir.stanford.edu/group/bil/env/jupyter/bin/activate'''
  (jupyter)jane@corn99:~$
  (jupyter)jane@corn99:~$
-
 
-
These two commands switch the shell to [https://en.wikipedia.org/wiki/Bash_(Unix_shell) bash] and then update the [http://tldp.org/LDP/Bash-Beginners-Guide/html/chap_03.html environment] to use a pre-built Jupyter installation. The prompt should have the <code>(jupyter)</code> prefix if the environment change is successful.
 
-
 
-
If of interest, the pip packages installed for the Jupyter environment are stored in a <code>requirements.txt</code> file at <code>/afs/ir.stanford.edu/group/bil/env/jupyter/requirements.txt</code>. Additional packages can be installed upon request.
 
=== Setup encryption and default configuration ===
=== Setup encryption and default configuration ===
Line 51: Line 45:
  (jupyter)jane@corn99:~$ '''/afs/ir.stanford.edu/group/bil/env/scripts/jupyter_config_wrapper'''
  (jupyter)jane@corn99:~$ '''/afs/ir.stanford.edu/group/bil/env/scripts/jupyter_config_wrapper'''
-
This command calls a script that will create encryption keys for the Jupyter notebook and configure the jupyter config file to use these newly created keys. Note, this will modify existing Jupyter notebook config files (if they exist), but will not overwrite any parameters that have changed from their default values.
 
-
Installation is complete after this step is successful. Proceed to [[Jupyter#Create_virtual_terminal|creating the virtual terminal]].
+
== Jupyter server ==
-
== Jupyter notebook server ==
+
This only needs to be performed ‘’once’’ per FarmShare system (and after every server reboot).
-
 
+
-
Jupyter notebook will be run within a persistent [https://tmux.github.io virtual terminal] so it can stay running even after the user has logged out of FarmShare.
+
-
 
+
-
=== Create virtual terminal ===
+
-
 
+
-
This only needs to be performed once per FarmShare system (and after every server reboot).
+
Make a note of exactly which corn server is being used (e.g., corn14, corn22, etc).
Make a note of exactly which corn server is being used (e.g., corn14, corn22, etc).
Line 92: Line 79:
         https://localhost:9876/?token=ba682763f27d8e2d59862badef28b0eaecb552529933176e
         https://localhost:9876/?token=ba682763f27d8e2d59862badef28b0eaecb552529933176e
-
The notebook will ''not work'' until a [[Jupyter#Client_connection |client connection]] is established. Copy the <code>https://localhos...</code> URL and save it for subsequent use. Also note the [https://en.wikipedia.org/wiki/Port_(computer_networking) TCP port] that the Jupyter notebook is listening on. Detach from tmux and logout of FarmShare.
+
The notebook will ''not work'' until a [[Jupyter#Client_connection_via_SSH_tunnel |client connection]] is established. Copy the <code>https://localhos...</code> URL and save it for subsequent use. Also note the [https://en.wikipedia.org/wiki/Port_(computer_networking) TCP port] that the Jupyter notebook is listening on. Detach from tmux and logout of FarmShare.
  '''Ctrl+b''' then '''d'''
  '''Ctrl+b''' then '''d'''
Line 101: Line 88:
Note: the <code>'''jupyter_start'''</code> script accepts an optional port argument, specifying which port to connect to (e.g., <code>'''jupyter_start <font color=red>9876</font>'''</code>). This is useful in case a randomized port is not desired.
Note: the <code>'''jupyter_start'''</code> script accepts an optional port argument, specifying which port to connect to (e.g., <code>'''jupyter_start <font color=red>9876</font>'''</code>). This is useful in case a randomized port is not desired.
-
== Client connection ==
+
== Client connection via SSH tunnel ==
-
=== Create SSH tunnel ===
+
This is the only step that needs to be performed every time (e.g., a client laptop wakes from sleep).
-
 
+
-
Once the Jupyter notebook server is running, it is ready to accept client connections. For security, it only accepts connections from [https://en.wikipedia.org/wiki/Localhost localhost] (i.e., connections originating from the FarmShare system itself). Local connections can be created remotely through [https://en.wikipedia.org/wiki/Tunneling_protocol#Secure_Shell_tunneling SSH tunnels]. SSH into the '''same''' FarmShare system that the Jupyter notebook server virtual terminal was created on (e.g., corn14, corn22, etc) by using the server's [https://en.wikipedia.org/wiki/Fully_qualified_domain_name FQDN] (e.g., corn14.stanford.edu) with a [http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html local port tunnel] to the TCP port that the server is listening on.
+
In Mac/Linux, setup the SSH tunnel in a terminal.
In Mac/Linux, setup the SSH tunnel in a terminal.
Line 113: Line 98:
substituting in the appropriate sunetid for <code><font color=red>'''jane'''</font></code>, the appropriate FarmShare server hostname for <code>'''corn<font color=red>99'''</font></code>, and the appropriate TCP port for <code><font color=red>'''9999'''</font></code>. Windows users can setup an SSH local tunnel using [https://www.electrictoolbox.com/putty-create-ssh-port-tunnel menu options] in [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html putty]
substituting in the appropriate sunetid for <code><font color=red>'''jane'''</font></code>, the appropriate FarmShare server hostname for <code>'''corn<font color=red>99'''</font></code>, and the appropriate TCP port for <code><font color=red>'''9999'''</font></code>. Windows users can setup an SSH local tunnel using [https://www.electrictoolbox.com/putty-create-ssh-port-tunnel menu options] in [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html putty]
-
Once the ssh tunnel has been established, paste the <code>https://localhos...</code> URL provided from the <code>jupyter_start</code> script into a web browser to connect to the notebook home page. If this URL is misplaced, use <code>tmux attach -d</code> to bring up the virtual terminal and copy the URL again. Since the encryption keys were [https://en.wikipedia.org/wiki/Self-signed_certificate self-signed], the browser will warn about an insecure connection, but disregard these and continue/proceed past them.
+
Once the ssh tunnel has been established, paste the <code>https://localhos...</code> URL provided from the <code>jupyter_start</code> script into a web browser to connect to the Jupyter notebook. Ignore any browser security warnings.
== Renewing virtual terminal ==
== Renewing virtual terminal ==
-
This only needs to be done a week after creating the virtual terminal when the kerberos credentials expire.
+
This only needs to be done when the kerberos credentials expire after a week. Renewal can take place at any time after a week when use is desired.
The virtual terminal created by the above instructions will persist on the FarmShare server for one week, after which its Kerberos tickets will expire and the Jupyter notebook will no longer function. To restore the notebook after this time, SSH into the '''same''' FarmShare system used to create the virtual terminal.
The virtual terminal created by the above instructions will persist on the FarmShare server for one week, after which its Kerberos tickets will expire and the Jupyter notebook will no longer function. To restore the notebook after this time, SSH into the '''same''' FarmShare system used to create the virtual terminal.
Line 202: Line 187:
[[File:jupyter_julia_demo.png]]
[[File:jupyter_julia_demo.png]]
 +
 +
== Technical Details ==
 +
 +
Jupyter setup is best performed via the [https://en.wikipedia.org/wiki/Linux Linux] console. This guide will be mostly step-by-step, but general familiarity with Linux is helpful.
 +
 +
Useful tutorials are available as a [http://linuxcommand.org/tlcl.php free book], [http://linuxcommand.org/lc3_learning_the_shell.php online tutorial], or [https://www.youtube.com/playlist?list=PLII6oL6B7q78PKy6_R6JTkkYjVXZBZcVq video series].
 +
 +
[https://en.wikipedia.org/wiki/Secure_Shell SSH] provides an encrypted remote [https://en.wikipedia.org/wiki/Shell_(computing) shell] into another system and is the primary way that Jupyter will be installed and accessed.
 +
 +
The first two commands using <code>bash</code> and <code>source</code> commands switch the shell to [https://en.wikipedia.org/wiki/Bash_(Unix_shell) bash] and then update the [http://tldp.org/LDP/Bash-Beginners-Guide/html/chap_03.html environment] to use a pre-built Jupyter installation. The prompt changes to have <code>(jupyter)</code> prefix when the environment change is successful.
 +
 +
If of interest, the pip packages installed for the Jupyter environment are stored in a <code>requirements.txt</code> file at <code>/afs/ir.stanford.edu/group/bil/env/jupyter/requirements.txt</code>. Additional packages can be installed upon request.
 +
 +
The <code>jupyter_config_wrapper</code> command calls a script that will create encryption keys for the Jupyter notebook and configure the jupyter config file to use these newly created keys. Note, this will modify existing Jupyter notebook config files (if they exist), but will not overwrite any parameters that have changed from their default values.
 +
 +
Jupyter notebook will be run within a persistent [https://tmux.github.io virtual terminal] so it will stay running even after the user has logged out of FarmShare.
 +
 +
Once the Jupyter notebook server is running, it is ready to accept client connections. For security, it only accepts connections from [https://en.wikipedia.org/wiki/Localhost localhost] (i.e., connections originating from the FarmShare system itself). Local connections can be created remotely through [https://en.wikipedia.org/wiki/Tunneling_protocol#Secure_Shell_tunneling SSH tunnels]. SSH into the '''same''' FarmShare system that the Jupyter notebook server virtual terminal was created on (e.g., corn14, corn22, etc) by using the server's [https://en.wikipedia.org/wiki/Fully_qualified_domain_name FQDN] (e.g., corn14.stanford.edu) with a [http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html local port tunnel] to the TCP port that the server is listening on.
 +
 +
If this notebook URL is misplaced, <code>tmux attach -d</code> will bring up the virtual terminal to view the URL again. Since the encryption keys were [https://en.wikipedia.org/wiki/Self-signed_certificate self-signed], the browser will warn about an insecure connection, but this warning can be disregarded.

Revision as of 23:45, 5 January 2017

Contents

Introduction

Project Jupyter evolved out of the IPython project (specifically the IPython notebook) with the goal to provide an interactive, web-browser driven, language-independent programming environment. Jupyter notebooks can be deployed on the FarmShare servers to enable an accessible, powerful, and persistent computational platform.

Features

At the end of this guide, the resulting Jupyter notebook will support:

  • Python, MATLAB, R, SAS, and Julia programming languages
  • an encrypted, token protected, and web-browser enabled programming environment
  • indefinite persistence of the Jupyter notebook server environment with simple weekly renewals (the maximum duration of Stanford Kerberos tickets)
  • file/data storage on the Stanford AFS servers (5GB user quota, Stanford-wide, automatic backups)
  • shared file/data storage to Class Disk AFS Space
  • easy deployment on any of the Stanford FarmShare systems

Overview

The guide consists of three sections: installation, Jupyter server, and client connection.

  • Installation is only performed once per user.
  • The Jupyter server needs to be started once per FarmShare machine used.
  • Client connection must happen every time a client computer reconnects to the Internet (e.g., a laptop wakes from sleep).

Commands to be typed in are in bold and text to substitute is in red.

Installation

SSH into FarmShare

In a terminal, ssh into a FarmShare computer.

$ ssh jane@corn.stanford.edu

substituting sunetid for jane.

A |terminal application and SSH client are shipped with Mac and Linux systems. Windows does not come with an SSH client, but putty is a a free and lightweight SSH client for Windows.

Bind to Jupyter virtual environment

corn99:~> bash
jane@corn99:~$ source /afs/ir.stanford.edu/group/bil/env/jupyter/bin/activate
(jupyter)jane@corn99:~$

Setup encryption and default configuration

(jupyter)jane@corn99:~$ /afs/ir.stanford.edu/group/bil/env/scripts/jupyter_config_wrapper


Jupyter server

This only needs to be performed ‘’once’’ per FarmShare system (and after every server reboot).

Make a note of exactly which corn server is being used (e.g., corn14, corn22, etc).

corn99:~$ pagsh
sh-4.3$ kinit; aklog
Password for jane@stanford.edu:
sh-4.3$ tmux
(new blank terminal appears)
corn99:~$ bash
jane@corn99:~$ /afs/ir.stanford.edu/group/bil/env/scripts/keep_kerberos_afs
Run: export KRB5CCNAME=FILE:/tmp/.krb5_jane.tgt
jane@corn99:~$ export KRB5CCNAME=FILE:/tmp/.krb5_jane.tgt
jane@corn99:~$ source /afs/ir.stanford.edu/group/bil/env/jupyter/bin/activate
(jupyter)jane@corn99:~$ /afs/ir.stanford.edu/group/bil/env/scripts/jupyter_start

Substitute sunetid for jane in the export line (as instructed by the keep_kerberos_afs script). If done correctly, the output of this command will be

IMPORTANT: This Jupyter notebook is listening on TCP port 9876

[I 17:23:59.661 NotebookApp] Loading IPython parallel extension
[I 17:23:59.668 NotebookApp] Serving notebooks from local directory: /afs/ir.stanford.edu/users/j/a/jane
[I 17:23:59.668 NotebookApp] 0 active kernels
[I 17:23:59.668 NotebookApp] The Jupyter Notebook is running at: https://localhost:9876/?token=ba682763f27d8e2d59862badef28b0eaecb552529933176e
[I 17:23:59.668 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 17:23:59.671 NotebookApp]

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        https://localhost:9876/?token=ba682763f27d8e2d59862badef28b0eaecb552529933176e

The notebook will not work until a client connection is established. Copy the https://localhos... URL and save it for subsequent use. Also note the TCP port that the Jupyter notebook is listening on. Detach from tmux and logout of FarmShare.

Ctrl+b then d
(detaches tmux)
sh-4.3$ exit
corn99:~> logout

Note: the jupyter_start script accepts an optional port argument, specifying which port to connect to (e.g., jupyter_start 9876). This is useful in case a randomized port is not desired.

Client connection via SSH tunnel

This is the only step that needs to be performed every time (e.g., a client laptop wakes from sleep).

In Mac/Linux, setup the SSH tunnel in a terminal.

 $ ssh jane@corn99.stanford.edu -L 9999:localhost:9999

substituting in the appropriate sunetid for jane, the appropriate FarmShare server hostname for corn99, and the appropriate TCP port for 9999. Windows users can setup an SSH local tunnel using menu options in putty

Once the ssh tunnel has been established, paste the https://localhos... URL provided from the jupyter_start script into a web browser to connect to the Jupyter notebook. Ignore any browser security warnings.

Renewing virtual terminal

This only needs to be done when the kerberos credentials expire after a week. Renewal can take place at any time after a week when use is desired.

The virtual terminal created by the above instructions will persist on the FarmShare server for one week, after which its Kerberos tickets will expire and the Jupyter notebook will no longer function. To restore the notebook after this time, SSH into the same FarmShare system used to create the virtual terminal.

$ ssh jane@corn99.stanford.edu

substituting sunetid for jane and the appropriate FarmShare server for corn99.

corn99:~$ tmux attach -d
Ctrl+b then c
(creates a new window in tmux)
jane@corn99:~$ kinit; aklog
Password for jane@stanford.edu:
Ctrl+b then 0
(returns tmux to the original window)

This will extend the terminal's credentials for another week. The same notebook URL can be used without interruption. Note, for subsequent renewals, use Ctrl+b then 1 to switch to window 1 instead of creating a new window. It is not necessary to create a new tmux window every time.

Examples

Python 3

import numpy as np
import matplotlib.pyplot as plt

x = np.r_[ -np.pi : np.pi : 0.1 ]
plt.plot(x, np.sin(x), linewidth=3)
plt.plot(x, np.cos(x), linewidth=3, color='r')
plt.show()

Jupyter python demo.png

MATLAB

x = -pi : 0.1 : pi;
hold on; box on;
plot(x, sin(x), 'lineWidth', 2);
plot(x, cos(x), 'lineWidth', 2, 'color', 'r');

Jupyter matlab demo.png

R

x = seq(-pi, pi, 0.1)
plot(x,  sin(x), type='l', lwd=4, col='green', ann=FALSE)
lines(x, cos(x), type='l', lwd=4, col='red')

Jupyter r demo.png

SAS

data curves;
  do x = -constant("pi") to constant("pi") by 0.1;
    y = sin(x);
    z = cos(x);
    output;
  end;
run;

symbol1 interpol=join color=red width=5;
symbol2 interpol=join color=red  width=5;
axis1 label=none minor=none;

proc gplot data=curves;
  plot y*x=1 z*x=2 / overlay vaxis=axis1 haxis=axis1;
run;

Jupyter sas demo.png

Julia

using Gadfly
plot([sin, cos], -pi, pi, Theme(line_width=4pt, guides=nothing))

Jupyter julia demo.png

Technical Details

Jupyter setup is best performed via the Linux console. This guide will be mostly step-by-step, but general familiarity with Linux is helpful.

Useful tutorials are available as a free book, online tutorial, or video series.

SSH provides an encrypted remote shell into another system and is the primary way that Jupyter will be installed and accessed.

The first two commands using bash and source commands switch the shell to bash and then update the environment to use a pre-built Jupyter installation. The prompt changes to have (jupyter) prefix when the environment change is successful.

If of interest, the pip packages installed for the Jupyter environment are stored in a requirements.txt file at /afs/ir.stanford.edu/group/bil/env/jupyter/requirements.txt. Additional packages can be installed upon request.

The jupyter_config_wrapper command calls a script that will create encryption keys for the Jupyter notebook and configure the jupyter config file to use these newly created keys. Note, this will modify existing Jupyter notebook config files (if they exist), but will not overwrite any parameters that have changed from their default values.

Jupyter notebook will be run within a persistent virtual terminal so it will stay running even after the user has logged out of FarmShare.

Once the Jupyter notebook server is running, it is ready to accept client connections. For security, it only accepts connections from localhost (i.e., connections originating from the FarmShare system itself). Local connections can be created remotely through SSH tunnels. SSH into the same FarmShare system that the Jupyter notebook server virtual terminal was created on (e.g., corn14, corn22, etc) by using the server's FQDN (e.g., corn14.stanford.edu) with a local port tunnel to the TCP port that the server is listening on.

If this notebook URL is misplaced, tmux attach -d will bring up the virtual terminal to view the URL again. Since the encryption keys were self-signed, the browser will warn about an insecure connection, but this warning can be disregarded.

Personal tools
Toolbox
LANGUAGES