Wilson Mar bio photo

Wilson Mar

Hello!

Email me Calendar Skype call

LinkedIn Twitter Gitter Instagram Youtube

Github Stackoverflow Pinterest

Notebooks that displays HTML markup as comments between executable Python code (for Machine Learning)

US (English)   Español (Spanish)   Français (French)   Deutsch (German)   Italiano   Português   Cyrillic Russian   中文 (简体) Chinese (Simplified)   日本語 Japanese   한국어 Korean

Overview

The name Jupyter comes from the combination of Julia, Python, and R (the statistics package).

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. Commentary along the way describes concepts to understand.

You will open a notebook, add a Markdown text and add a header while you try various keyboard shortcuts.


Browser configuration

  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.

  2. Navigate to a URL containing Jupyter Notebook files.

    https://github.com/jakevdp/PythonDataScienceHandbook

    https://www.eecis.udel.edu/~boncelet/ipython.html

    https://github.com/odewahn/ipynb-examples

    https://github.com/ogrisel/notebooks

    analysis of gravitational waves from two colliding blackholes detected by the LIGO experiment.

    Sentiment-network.zip from Andrew Trask contains notebooks for sentiment analysis.

## Jupyter notebook and session files

  1. In a Terminal …

  2. If you haven’t already, make a 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

    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 another company 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 using Pyenv
  2. Python install on MacOS
  3. Python install on Raspberry Pi for IoT

  4. Test Python using Pytest BDD Selenium framework
  5. Test Python using Robot testing framework

  6. Python certifications
  7. Python tutorials
  8. Python coding notes
  9. Jupyter Notebooks provide commentary to Python

  10. Pulumi controls cloud using Python, etc.
  11. Microsoft Azure Machine Learning makes use of Python
  12. Testing AI uses Python code

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