Wilson Mar bio photo

Wilson Mar

Hello!

Calendar YouTube Github

LinkedIn

“Notebooks” display HTML markup as comments between executable Python code (for Machine Learning and Data Science), for Style Transfer of images, etc.

US (English)   Norsk (Norwegian)   Español (Spanish)   Français (French)   Deutsch (German)   Italiano   Português   Estonian   اَلْعَرَبِيَّةُ (Egypt Arabic)   Napali   中文 (简体) Chinese (Simplified)   日本語 Japanese   한국어 Korean

Overview

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 Colab (Colaboratory)

Google provides a FREE on-line environment with zero configuration:

https://colab.research.google.com

python-colab-welcome-1436x1216

In the above example, data is received from the numpy (np) library’s random function, rounded down.

Photo Style Transfer

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:

  1. Click on this link to open the “TF-Hub: Fast Style Transfer for Arbitrary Styles.ipynb” within Colab:

    https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/tf2_arbitrary_image_stylization.ipynb

  2. 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.

  3. Press (command+down arrow) to scroll down to the bottom to see the default original, style image, and stylized image:

    python-style-transfer-1178x410

  4. 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.

  5. Click the content_name: sea_turtle to select another original image.
  6. Click the style_name: munch_scream to select another style image.

    Style Transfer your picture’s URL

  7. To run the code block:
    • click on the rounded arrow or
    • Press the Tab key then hold down Shift key to press Return.

  8. At the left menu, click “To Run: Load more images” to see where you can add a statement pointing to your own photo.

  9. Click on this URL, to go to many websites go to get a URL of a photo based on an email:

    en.gravatar.com

  10. Sign in using your WordPress email and password, or sign up and download your photo.
  11. Right-click on the picture you want to use and select Copy image address.
  12. Click on the “TF-Hub” tab to return to the Colab.

  13. Replace the “grace_hopper” entry with the URL to your picture, for example:

    grace_hopper='https://en.gravatar.com/userimage/10217204/6f0736298461c359b864b3939db37f8f.jpg',
    
  14. To Run all Notebook cells: on a Mac, hold down command + fn to press the “F9”.

  15. Select a different style_name and rerun to see a new stylized image.
  16. When you see a stylized image you like, right-click on the picture and Save Image As… download.jpg in your Downloads folder:

    jupyter-amadeo-2026x698

    Admittedly, the pictures are not high resolution.

  17. Now edit that picture and share it.

Sample Notebook files in Colab

Navigate to a URL containing Jupyter Notebook files:


Browser configuration on your laptop

For possible offline access (on a cruise ship, a railcar through the wilderness, at the beach, etc.):

  1. 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.

    Jupyter notebook and session files

  2. Make a container folder (directory) to hold Jupyter session files.

    This is done automatically by doing a git clone.

  3. Download a Jupyter Notebook file into your folder.

    You may have to move downloaded files from your Downloads folder.

    Ignore checkpoints on push

  4. Edit the .gitignore file to add .ipynb_checkpoints/ so that such folders created automatically are not uploaded into the GitHub repository.

  5. run a notebook.


Server Installation

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


Manual install

  1. 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.

  2. 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.

    After install: Add themes

    https://github.com/dunovank/jupyter-themes

    Use within VSCode

    Jupyter works great within VSCode.

    See https://code.visualstudio.com/docs/datascience/jupyter-notebooks

    Start Jupyter server

  3. 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.

    Shutdown jupyter server

    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

Run Notebook UI

https://jupyter.readthedocs.io/en/latest/running.html

  1. If Jupyter was started without specifying a Notebook file, you would see a tree listing.

  2. Click a link to a .ipynb (Notebook) file listed, to see it’s code.

  3. 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.

    Keyboard Command Palette of shortcuts

    NOTE: This does not work in Firefox and Internet Explorer, only in Chrome and Apple Safari.

  4. When in command mode, click the little keyboard icon, called the “command palette”.

    A panel appears with a search bar.

  5. Press down arrow to scroll down.

    Keyboard shortcuts are on the right side.

  6. Enter a command to search for. Helpful for speeding up your workflow instead of looking around in the menus with your mouse.

    See http://docutils.sourceforge.net/rst.html

  7. Press Esc key.

    Run All Cells

  8. Click “Cell” in the menu and select “Run All”:

    jupyter-cell-menu-408x584

    Kernel

    Alternately, select the Kernel menu “Restart and Run All”:

    jupyter-kernel-menu-179x232

    DEFINITION: A “kernel” is each web page (window).

  9. Create a kernel by clicking on the New drop-down and selecting one.

  10. Click on tab File to show all the files and folders in the current directory.

  11. Click Open.

  12. List all the currently running notebooks by clicking on the Running tab.

  13. Select “Python [Conda root]” for a new window (with tabs).

    jupyter types

    CAUTION: Just closing the browser leaves a kernel running.

  14. To close and halt the kernel, select File, then Close and Halt.

    Conda

  15. cd to the folder containing the Jupyter notebook above.

  16. 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
    
  17. Source activate the conda environment.

  18. Install packages from within the Conda environment:

    conda install jupyter notebook numpy matplotlib scikit-learn bokeh

    Editing

  19. Click on the Edit tab.

    NOTE: Cells are where code is written and run.

    Several of the selections have an icon equivalent.

  20. Press Esc key.

  21. 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.

  22. To exit from edit mode to display mode, hold down Shift and press Enter.

    Command mode

    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.

    Python Code

  23. Click on the “Code” drop-down, which specifies what is typed in the cell (input box).

  24. Select Markdown to format syntax for writing web content.

  25. 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.

  26. 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.

    Heading

  27. Click the up/down arrows to position the cell above or below existing runned lines.

  28. Select Heading and add a # to reduce the level before typing heading text.

    Open and Rename Notebook

  29. Click Files tab.
  30. Click Open.

    Select _____???

  31. Click the checkbox to the right of the file name.
  32. Click the Rename button.
  33. Remove the “.txt” and press OK.
  34. Click on the file name itself to open the file.

    A new tab should appear.

    Timings

    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.

    Debugging

    %pdb at the top of the cell turns debugging on.

    https://docs.python.org/3/library/pdb.html

    Visualization magic keywords

    %matplotlib inline

    See http://ipython.readthedocs.io/en/stable/interactive/magics.html for docs about magic commands.

    nbextensions

  35. Code to dynamically loaded:

    %%javascript
    IPython.load_extensions('custom_shortcuts');
    

    See https://carreau.gitbooks.io/jupyter-book/content/Jsextensions.html

    Raw NBConvert

    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

    Output as HTML

  36. 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

    GitHub

    https://github.com/blog/1995-github-jupyter-notebooks-3 GitHub renders Jupyter Notebooks with Git Large File Support.

    NBViewer

    http://nbviewer.jupyter.org renders notebooks from a GitHub repo or from notebooks stored elsewhere.

    Pandas Slideshows

  37. Click the View tab.

    See http://nbviewer.jupyter.org/format/slides/github/jorisvandenbossche/2015-PyDataParis/blob/master/pandas_introduction.ipynb#/

Turi (Dato) Python algorithms

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:

  • https://www.coursera.org/learn/ml-foundations
  • https://www.turi.com/learn/userguide/
  • https://www.turi.com/products/create/docs/
  • https://github.com/learnml/machine-learning-specialization
  • https://www.coursera.org/learn/ml-clustering-and-retrieval/supplement/iF7Ji/software-tools-you-ll-need-for-this-course

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.

References

This is based on tutorials from several sources:

Jupyter Notebook Tutorial: Introduction, Setup, and Walkthrough

Additional

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

More about Python

This is one of a series about Python:

  1. Python install on MacOS
  2. Python install on MacOS using Pyenv
  3. Python install on Raspberry Pi for IoT

  4. Python tutorials
  5. Python Examples
  6. Python coding notes
  7. Pulumi controls cloud using Python, etc.
  8. Jupyter Notebooks provide commentary to Python

  9. Python certifications

  10. Test Python using Pytest BDD Selenium framework
  11. Test Python using Robot testing framework
  12. Testing AI uses Python code

  13. Microsoft Azure Machine Learning makes use of Python

  14. Python REST API programming using the Flask library
  15. Python coding for AWS Lambda Serverless programming
  16. Streamlit visualization framework powered by Python
  17. Web scraping using Scrapy, powered by Python
  18. Neo4j graph databases accessed from Python