Skip to content

Jupyter Notebooks

BK Jackson edited this page Dec 23, 2024 · 24 revisions

Subpages:

Articles

Writing Clean Jupyter Notebooks

View a YouTube video in a notebook

#@title
from IPython.display import HTML

HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/tiZFewofSLM?rel=0&amp;controls=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>')  

view a powerpoint file in a notebook

%%HTML

<div align="center"><iframe src="<YOUR_FILENAME>.pptx" width="800px" height="500px" frameborder="0"></iframe></div>  

view a README.md file in a notebook

%load README.md  

Also

from IPython.display import display, Markdown

with open('README.md', 'r') as fh:
    content = fh.read()

display(Markdown(content))

Cool trick for appending a notebook function to a .py file

%%file -a ../src/utils.py
def my_appended_function(x):
    print(x)

Note: %%file ../src/utils.py will create a new file or overwrite an existing file.

NBextensions

View installed nbextensions

At the command line:

$ jupyter nbextension list  

ipywidgets

Interactive Controls in Jupyter Notebooks - Covers ipywidgets

install with pip

pip install ipywidgets  

activate widgets for Jupyter Notebooks

jupyter nbextension enable --py widgetsnbextension  

use with JupyterLab

jupyter labextension install @jupyter-widgets/jupyterlab-manager  

Import the ipywidgets library

import ipywidgets as widgets
from ipywidgets import interact, interact_manual  

Applying the interact widget

Create drop-down menu for columns and a slider for variable x

@interact
def show_articles_more_than(column='claps', x=5000):  
    return df.loc[df[column] > x]  

Create drop-down menu for quickly looking at images in a directory

import os
from IPython.display import Image
@interact
def show_images(file=os.listdir('images/')):
    display(Image(fdir+file))  

Do some math between two columns selected from drop-down menus

Calculate correlation:

@interact
def correlations(column1=list(df.select_dtypes('number').columns),
                 column2=list(df.select_dtypes('number').columns)):
    print(f"Correlation: {df[column1].corr(df[column2])}")  

RISE - Reveal.js - Jupyter/IPython Slideshow Extension

RISE Docs - With RISE, a Jupyter notebook extension, you can instantly turn your jupyter notebook into a live reveal.js-based presentation.

Cool notebook resources

Awesome Jupyter - A curated list of awesome Jupyter projects, libraries and resources.
QGrid - An interactive grid for sorting, filtering, and editing DataFrames in Jupyter notebooks

Clone this wiki locally