Notebook Programming#
Table of Contents#
Jupyter#
[ d ] Jupyter Notebooks in VS Code
-
[ d ] Kernels for Conda environments
nbformat
[ d ] The Jupyter Notebook Format
[ h ] Binder
[ d ] ipyparallel
[ d ] ipywidgets
[ d ] Voila
Online
[ o ] StackExchange. “How to delete all markdown cells in jupyter notebook?”.
https://www.geeksforgeeks.org/how-to-optimize-jupyter-notebook-performance/
Config#
macOS
~/.ipython~/.jupyter
Jupyter Notebook Shortcuts in Visual Studio Code#
Open the command pallette (Cmd-Shift-P (macoS) or Ctrl-Shift-P (Windows)) and search for Open Keyboard Shortcuts.
Shortcut |
Symbol |
Action |
|---|---|---|
Alt-Down |
⎇↓ |
Move the current cell below the cell below (i.e., swap them) (Windows) |
Alt-Enter |
⎇↵ |
Run the current cell and create a new cell below (Windows) |
Alt-Up |
⎇↑ |
Move the current cell above the cell above (i.e., swap them) (Windows) |
Cmd-F |
⌘F |
Filter (macOS) |
Ctrl-Enter |
⌃↵ |
Run the current cell |
Ctrl-F |
⌃F |
Filter (Windows) |
Ctrl-S |
⌃S |
Save the notebook |
Down |
↓ |
Select the cell below the current cell |
Enter |
↵ |
Switch to Edit Mode |
Esc |
Switch to Command Mode |
|
Opt-Down |
⌥↓ |
Move the current cell below the cell below (i.e., swap them) (macOS) |
Opt-Enter |
⌥↵ |
Run the current cell and create a new cell below (macOS) |
Opt-Up |
⌥↑ |
Move the current cell above the cell above (i.e., swap them) (macOS) |
Shift-Enter |
⇧↵ |
Run the current cell and advance to the next cell |
Shift-L |
⇧L |
Toggle line numbering for the entire notebook (when in command mode) |
Shift-V |
⇧V |
paste above ??? |
Shift-LeftClick |
Select consecutive cells |
|
Up |
↑ |
Select the cell above the current cell |
Command Mode
acreate a new cell above the current cellbcreate a new cell below the current cellccopy the current celljselect the cell below the current cellkselect the cell above the current celllenable/disable line numbering in a code cellmconvert the current cell to a markdown cellvpaste the previously copied cell below the current cellyconvert the current cell to a code cellxordddelete the current cellzundo previous cell operation
Managing Jupyter Kernels#
List Jupyter kernels.
jupyter kernelspec list
1 - Activate the Conda environment that you want to make a Jupyter kernel.
conda activate my_conda_env
2 - Notice how the default Jupyter kernel path changes to the activated Conda environment. This is why the default Jupyter kernel “Python 3 (IPyKernel)” depends on the underlying Conda environment.
jupyter kernelspec list
3 - Make your Conda environment a new Jupyter kernel.
python -m ipykernel install --user --name my_conda_env
4 - Verify the successful creation of the new Jupyter kernel.
jupyter kernelspec list
Delete a Jupyter kernel.
jupyter kernelspec remove my_conda_env
Command Reference#
jupyter --version
jupyter lab --version
jupyter notebook --version
jupyter lab
jupyter lab -hHow do I use commandjupyter lab?jupyter lab --versionWhat is the Jupyter Lab version?jupyter labLaunch Jupyter Lab.jupyter lab listWhich Jupyter Lab servers are currently running?jupyter lab pathorjupyter lab pathsWhere are the Jupyter Lab configuration directories located in the filesystem?jupyter lab build -hHow do I use commandjupyter lab build?jupyter lab clean -hHow do I use commandjupyter lab clean?jupyter lab --notebook-dir=.
jupyter lab --ContentsManager.allow_hidden=True &>> .jupyter.log &
jupyter labextension
jupyter labextension -hHow do I use commandjupyter labextension?jupyter labextension --versionWhat is the version?jupyter labextension listWhich Jupyter Lab extensions are currently configured?
jupyter notebook
jupyter notebook -hHow do I use commandjupyter notebook?jupyter notebook --versionWhat is the Jupyter Notebook version?jupyter notebookLaunch Jupyter Notebook.jupyter notebook listWhich Jupyter Notebook servers are currently running?jupyter notebook --notebook-dir=.
jupyter nbextension
jupyter nbextension -hHow do I use commandjupyter nbextension?jupyter nbextension --versionWhat is the version?jupyter nbextension listWhich Jupyter Notebook extensions are currently configured?
jupyter
jupyter -hHow do I use commandjupyter?jupyter --versionWhat is the Jupyter version?jupyter --config-dirWhere is the Jupyter configuration directory located in the filesystem?jupyter --data-dirWhere is the Jupyter data directory located in the filesystem?jupyter --runtime-dirWhere is the Jupyter runtime directory located in the filesystem?
jupyter kernelspec
jupyter kernelspec -hHow do I use commandjupyter kernelspec?jupyter kernelspec --versionWhat is the version?jupyter kernelspec listWhich kernels are currently available?
jupyter server
jupyter server -hHow do I use commandjupyter server?jupyter server --versionWhat is the Jupyter Server version?jupyter server listWhich Jupyter servers are currently running?jupyter server extension listorjupyter lab extension listWhich Jupyter Server extensions are currently configured?
ipython
ipython -hHow do I use commandipython?ipython -VWhat is the IPython version?ipython locatePrint the path to the IPython directory.
Exporting Jupyter notebook files as python files#
[ d ] Visual Studio Code’s Export your Jupyter Notebook: Export
Jupyter Lab: File -> Export Notebook As... -> Export Notebook to Executable Script.
Jupyter Notebook: File -> Download as -> Python (.py).
Google Colab#
Resources#
[ d ] Overview of Colaboratory Features
[ d ] Markdown Guide
[ d ] Charts in Colaboratory
[ d ] External data: Local Files, Drive, Sheets, and Cloud Storage
[ d ] Getting started with BigQuery
[ d ] Forms
[ d ] Data Table
[ d ] Accessing files
[ d ] Advanced Outputs
[ d ] Altair
[ d ] BigQuery
[ d ] Google Drive
[ d ] Saving Data to Google Cloud Storage
[ d ] Forms snippets
[ d ] Importing libraries
[ d ] Google Sheets
Apache Spark, PostgreSQL, & Google Colab#
#####
#
# APACHE SPARK + POSTGRES + GOOGLE COLAB
#
# Find the latest versions of
# Spark & Hadoop: https://spark.apache.org/downloads.html (https://www.apache.org/dist/spark/)
# Postgres driver: https://jdbc.postgresql.org/
#####
#
# CONFIGURE VERSIONS
#
import os
os.environ['HADOOP_VERSION'] = hadoop_version = 'hadoop3'
os.environ['SPARK_VERSION'] = spark_version = 'spark-3.5.0'
os.environ['POSTGRES_VERSION'] = postgres_version = 'postgresql-42.6.0'
#
# it should not be necessary to modify code below this point
#
####################
# Install Java
! apt install openjdk-11-jdk-headless > /dev/null
os.environ['JAVA_HOME'] = '/usr/lib/jvm/java-11-openjdk-amd64'
# Install Spark
! wget https://www.apache.org/dist/spark/$SPARK_VERSION/$SPARK_VERSION-bin-$HADOOP_VERSION.tgz
! tar xf $SPARK_VERSION-bin-$HADOOP_VERSION.tgz
os.environ['SPARK_HOME'] = f'/content/{spark_version}-bin-{hadoop_version}'
! pip install findspark
# Install Postgres driver
! wget https://jdbc.postgresql.org/download/$POSTGRES_VERSION.jar
# Install AWS's Boto3
! pip install boto3
import boto3
import findspark
findspark.init()
from getpass import getpass
from pyspark.sql import SparkSession
spark = SparkSession.builder \
.appName('CustomerReviews') \
.config('spark.driver.extraClassPath', f'/content/{postgres_version}.jar') \
.getOrCreate()
spark
Terms#
[ w ] Google Colab
[ w ] Interactive Computing
[ w ] Jupyter
[ w ] Literate Programming
[ w ] Notebook Interface