“Notebooks” display HTML markup as comments between executable Python code (for Machine Learning and Data Science), for Style Transfer of images, etc.
The name “Jupyter” comes from the combination of languages Julia, Python, and R (the statistics package).
NOTE: Content here are my personal opinions, and not intended to represent any employer (past or present). “PROTIP:” here highlight information I haven’t seen elsewhere on the internet because it is hard-won, little-know but significant facts based on my personal research and experience.
Jupyter is a web application server that runs several separate environments (one on each port). It’s open-sourced at:
Each Jupyter notebook contains explanatory text, math equations, code, and visualizations all in one easily sharable document.
Jupyter Notebooks are JSON files with the extension .ipynb. However, “.txt” is added for storage.
Notebooks used to be called “iPython” notebooks before languages other than Python were added.
Below is a guided learning experience. You perform actions in a planned sequence that takes you through various features. “PROTIP” tags commentary to highlight ideas probably unique to this website.ß
Google provides a FREE on-line environment with zero configuration:
https://colab.research.google.com
In the above example, data is received from the numpy (np) library’s random function, rounded down.
There are other tools that do this. But as a practical example of learning about Colab for programming Python Machine Learning, let’s do a “style transfer” on a photo file you provide:
Click on this link to open the “TF-Hub: Fast Style Transfer for Arbitrary Styles.ipynb” within Colab:
import tensorflow as tf
import tensorflow_hub as hub
import matplotlib.pyplot as plt
import numpy as np
import PIL.Image
def load_img(path_to_img):
max_dim = 512
img = tf.io.read_file(path_to_img)
img = tf.image.decode_image(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
shape = tf.cast(tf.shape(img)[:-1], tf.float32)
long_dim = max(shape)
scale = max_dim / long_dim
new_shape = tf.cast(shape * scale, tf.int32)
img = tf.image.resize(img, new_shape)
img = img[tf.newaxis, :]
return img
def imshow(image, title=None):
if len(image.shape) > 3:
image = tf.squeeze(image, axis=0)
plt.imshow(image)
if title:
plt.title(title)
def tensor_to_image(tensor):
tensor = tensor*255
tensor = np.array(tensor, dtype=np.uint8)
if np.ndim(tensor)>3:
assert tensor.shape[0] == 1
tensor = tensor[0]
return PIL.Image.fromarray(tensor)
To Run all Notebook cells: on a Mac, hold down command + fn to press the “F9”.
For a reminder of that keyboard sequence, click the “Runtime” menu.
Press (command+down arrow) to scroll down to the bottom to see the default original, style image, and stylized image:
Scroll down past the list of style images and click on each URL to view them.
The program creates a stylized image for each entry in the “content_url” dictionary.
Click the style_name: munch_scream to select another style image.
At the left menu, click “To Run: Load more images” to see where you can add a statement pointing to your own photo.
Click on this URL, to go to many websites go to get a URL of a photo based on an email:
Click on the “TF-Hub” tab to return to the Colab.
Replace the “grace_hopper” entry with the URL to your picture, for example:
grace_hopper='https://en.gravatar.com/userimage/10217204/6f0736298461c359b864b3939db37f8f.jpg',
To Run all Notebook cells: on a Mac, hold down command + fn to press the “F9”.
When you see a stylized image you like, right-click on the picture and Save Image As… download.jpg in your Downloads folder:
Admittedly, the pictures are not high resolution.
Navigate to a URL containing Jupyter Notebook files:
Sample Notebook code with “Open in Colab” are in Noah Gift’s “Minimal Python” book: https://github.com/paiml/minimal-python/blob/master/Chapter1_Minimal_Python.ipynb
analysis of gravitational waves from two colliding blackholes detected by the LIGO experiment.
Sentiment-network.zip from Andrew Trask contains notebooks for sentiment analysis.
For possible offline access (on a cruise ship, a railcar through the wilderness, at the beach, etc.):
PROTIP: Set Chrome as your default browser.
REMEMBER: Jupyter’s Command Palette does not work in Firefox and Internet Explorer, only in Chrome and Apple Safari.
Make a container folder (directory) to hold Jupyter session files.
This is done automatically by doing a git clone.
Download a Jupyter Notebook file into your folder.
You may have to move downloaded files from your Downloads folder.
Edit the .gitignore file to add .ipynb_checkpoints/ so that such folders created automatically are not uploaded into the GitHub repository.
VIDEO: Without Python Anacoda:
https://www.youtube.com/watch?v=z7zOkRubIrU
cd
mkdir ~/jupyter-env
cd venv
python3 -m venv ~/jupyter-env
cd jupyter-env
ls -ltr
File pyenv.cfg and directories lib, include, bin.
source ~/jupyter-env/bin/activate
python3 -m pip install --upgrade pip
pip install jupyter
python3 -m pip freeze
jupyter notebook
The canonical https://jupyter.org/install and Installation Guide says JupyterLab can be manually installed using conda or pip, described below.
However, you will find it easier to use the Docker image defined at https://github.com/jupyter/docker-stacks. See https://jupyter-docker-stacks.readthedocs.io/en/latest/using/selecting.html
Even better, the easiest approach is to use my Bash script at bash/bash-sample.sh
https://github.com/wilsonmar/DevSecOps
On a Mac: if you use pipenv, install using that. Otherwise:
pip3 install jupyterlab
This adds a bunch of “jupyter” files in the /bin folder.
Edit ~/.bash_profile to make sure the /bin folder is in the PATH.
export PATH="$PATH:/bin" # for /bin/jupyter
One may already be there for other apps.
https://github.com/dunovank/jupyter-themes
Jupyter works great within VSCode.
See https://code.visualstudio.com/docs/datascience/jupyter-notebooks
In a Terminal window, enter Jupyter (with & to run in background so you can keep working on the command line):
jupyter notebook --port 8888 notebook1.ipynb
A “&” is added to the end of the command above to run the server in background so that additional bash commands can execute (such as identifying the process number and cleaning up after shutdown).
The command above creates a (hidden) file .jupyter in your home folder, invokes a web server locally, and automatically pops up to your default browser a tab with url:
http://localhost:8888/tree
Alternately, to start without a web browser:
jupyter notebook --no-browser
The command response briefly shows a process number:
[1] 2809
If the command is run again on another Terminal, additional Notebook servers are invoked with the port number incremented from 8888.
If the server was started in background, you can’t shutdown the server from Terminal by pressing control + C twice. You would need to run (such as):
kill -9 2809
https://jupyter.readthedocs.io/en/latest/running.html
If Jupyter was started without specifying a Notebook file, you would see a tree listing.
Click a link to a .ipynb (Notebook) file listed, to see it’s code.
PROTIP: You can press the “Run” icon to run a single step. However, those familiar with the file can press the “»” button to run all cells (steps) in the file.
Each invocation opens a new browser window.
NOTE: This does not work in Firefox and Internet Explorer, only in Chrome and Apple Safari.
When in command mode, click the little keyboard icon, called the “command palette”.
A panel appears with a search bar.
Press down arrow to scroll down.
Keyboard shortcuts are on the right side.
Enter a command to search for. Helpful for speeding up your workflow instead of looking around in the menus with your mouse.
Press Esc key.
Click “Cell” in the menu and select “Run All”:
Alternately, select the Kernel menu “Restart and Run All”:
DEFINITION: A “kernel” is each web page (window).
Create a kernel by clicking on the New drop-down and selecting one.
Click on tab File to show all the files and folders in the current directory.
Click Open.
List all the currently running notebooks by clicking on the Running tab.
Select “Python [Conda root]” for a new window (with tabs).
CAUTION: Just closing the browser leaves a kernel running.
To close and halt the kernel, select File, then Close and Halt.
cd to the folder containing the Jupyter notebook above.
A) Create a conda environment based on the requirements file:
conda install --file requirements.txt
B) Create a stand-alone environment named PDSH with Python 3.5 and all the required package versions:
conda create -n PDSH python=3.5 --file requirements.txt
Source activate the conda environment.
Install packages from within the Conda environment:
conda install jupyter notebook numpy matplotlib scikit-learn bokeh
Click on the Edit tab.
NOTE: Cells are where code is written and run.
Several of the selections have an icon equivalent.
Press Esc key.
Each cell can be changed by just clicking on it.
When the thick left border on a box is colored green, the box is in edit mode.
If you don’t see a blinking cursor in the cell, click in the cell.
To exit from edit mode to display mode, hold down Shift and press Enter.
When a new cell is created, it is in command mode.
When the thick left border on a box is colored blue, the box is in command mode.
When the bar is blue, press H for a help screen about Keyboard shortcuts. It reads:
“Command mode binds the keyboard to notebook level actions.”
When the bar is blue, press A to create a new cell.
press B to create a cell below the currently selected cell.
Click on the “Code” drop-down, which specifies what is typed in the cell (input box).
Select Markdown to format syntax for writing web content.
Type in some markdown text. See
https://daringfireball.net/projects/markdown/basics
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
REMEMBER: Two dollar signs begin and end math entries.
To run the cell, click the >| icon (used to mean forward to the end) or press Shift + Enter or Control + Enter to run the Markdown cell.
Click the up/down arrows to position the cell above or below existing runned lines.
Select Heading and add a # to reduce the level before typing heading text.
Click Open.
Select _____???
Click on the file name itself to open the file.
A new tab should appear.
To get the number of milliseconds a function took to run,
put %timeit in front of commands to invoke.
To get the number of seconds it took for a cell to run,
put %%timeit at the top of the cell.
%pdb at the top of the cell turns debugging on.
https://docs.python.org/3/library/pdb.html
%matplotlib inline
See http://ipython.readthedocs.io/en/stable/interactive/magics.html for docs about magic commands.
Code to dynamically loaded:
%%javascript IPython.load_extensions('custom_shortcuts');
See https://carreau.gitbooks.io/jupyter-book/content/Jsextensions.html
NBConvert code modify the UI and behavior of Jupyter itself on the browser.
So Jupyter notebook extensions are written in javascript and CSS.
nbextensions are installed in the directory of the same name, either system wide or in your user profile. Their entry point is named extensionname.js
To share a Notebook with others who do not have Notebook installed, convert the Notebook to HTML or Markdown.
PROTIP: Some prefer receiving Markdown text so they paste in blog editing software which formats the Markdown to their own liking.
jupyter nbconvert --to html notebook.ipynb
See https://nbconvert.readthedocs.io/en/latest/usage.html
See https://ipython.org/ipython-doc/1/interactive/nbconvert.html#notebook-format
https://github.com/blog/1995-github-jupyter-notebooks-3 GitHub renders Jupyter Notebooks with Git Large File Support.
http://nbviewer.jupyter.org renders notebooks from a GitHub repo or from notebooks stored elsewhere.
Click the View tab.
See http://nbviewer.jupyter.org/format/slides/github/jorisvandenbossche/2015-PyDataParis/blob/master/pandas_introduction.ipynb#/
GraphLab Create from Dato is no longer available since Dato was sold and an unrelated entity now uses their domain name.
It provided scalable “pre-implemented” ML algorithms using Python installed using Anaconda. Entire courses on its use is at:
When the one-year free license is over, note scikit-learn also uses Python with Anaconda.
https://www.youtube.com/watch?v=GxZUdZMPGpQ Large-Scale Deep Learning with TensorFlow Turi, Inc.
This is based on tutorials from several sources:
Jupyter Notebook Tutorial: Introduction, Setup, and Walkthrough
Clusters is not longer used to create multiple kernels used in parallel computing.
https://ipyparallel.readthedocs.io/en/latest/intro.html
Introduction to Data Science at UC Berkeley
Python for Data Analysis (Safari books online) by Wes McKinney
https://medium.com/analytics-vidhya/how-to-do-neural-style-transfer-in-python-bee3f0d1008b
This is one of a series about Python: