Power-Ups for Jupyter Notebooks

Power-Ups for Jupyter NotebooksShyam ChoudharyBlockedUnblockFollowFollowingFeb 7In my last article, I discussed some of the Jupyter notebooks magic functions, extensions and a few other cool features.

In this one, we are gonna take a look at some quite useful add-ons on top of the ones discussed earlier.

These have been accumulated from various other sources I have been going through.

Jupyter LogoMore ExtensionsNotebook extensions let you move beyond the general vanilla way of using the Jupyter Notebooks.

Notebook extensions (or nbextensions) are JavaScript modules that you can load on most of the views in your Notebook’s frontend.

These extensions modify the user experience and interface.

Let us discuss some of the useful extensions that have been missed out in the last article.

1.

HinterlandHinterland enables code autocompletion menu for every keypress in a code cell, instead of only calling it with the tab.

This makes Jupyter notebook’s autocompletion behave like other popular IDEs such as PyCharm and VCS.

AWESOME!2.

Split Cells NotebookThis extension splits the cells of the notebook and places then adjacent to each other.

These will be of great use for comparing things for a presentation.

3.

SnippetsWith this you get a drop-down menu to the Notebook toolbar that allows easy insertion of code snippet cells from many popular libraries into the current notebook.

4.

Collapsible HeadingsCollapsible Headings allows the notebook to have collapsible sections, separated by headings.

So in case you have a lot of dirty code in your notebook, you can simply collapse it to avoid scrolling it again and again.

DOPE!!SlideshowNotebooks are an effective tool for teaching and writing explainable codes.

Jupyter Notebooks can be easily converted to slides and we can easily choose what to show and what to hide from the notebooks.

NO MORE POWERPOINT!!This can be done in two ways:1.

Jupyter’s inbuilt SlideIn your notebook navigate to View → Cell Toolbar → Slideshow.

A light grey bar appears on top of each cell, and you can customize the slides.

Now go to the directory where the notebook is present and enter the following code:jupyter nbconvert *.

ipynb –to slides –post serve# insert your notebook name instead of *.

ipynbThe slides get displayed at port 8000.

Also, a .

html file will be generated in the directory, and you can also access the slides from there.

Here, you can see the code but cannot edit it.

RISE plugin offers a solution.

2.

RISE pluginThis I have already mentioned in my last article.

RISE is an acronym for Reveal.

js — Jupyter/IPython Slideshow Extension.

It utilized the reveal.

js to run the slideshow.

This is super useful since it also gives the ability to run the code without having to exit the slideshow.

Install with:#Using condaconda install -c conda-forge rise#Using pippip install RISEInstall the JS and CSS in the proper places:jupyter-nbextension install rise –py –sys-prefix#enable the nbextension:jupyter-nbextension enable rise –py –sys-prefixNow we notice a new extension that says “Enter/Exit RISE Slideshow.

”Click on it.

You get your interactive slides.

Awesome!Jupyter WidgetsJupyter Widgets are eventful python objects that have a representation in the browser, often as a control like a slider, textbox, etc.

Widgets can be used to build interactive GUIs for the notebooks.

Install them with:# pippip install ipywidgetsjupyter nbextension enable –py widgetsnbextension# Condaconda install -c conda-forge ipywidgets#Installing ipywidgets with conda automatically enables the extensionFor complete details, you can visit their Github repository.

For now, let’s have a look at some of the widgets.

InteractIt is the easiest way to get started using IPython’s widgets.

The interact function (ipywidgets.

interact) automatically creates a user interface (UI) controls for exploring code and data interactively.

There are few actions less efficient in data exploration than re-running the same cell over and over again, each time slightly changing the input parameters.

The ideal solution to this issue would be interactive controls to change inputs without needing to rewrite or rerun code.

We’ll see how to get started with IPython widgets ( ipywidgets), interactive controls you can build with one line of code.

This library allows us to turn Jupyter Notebooks from static documents into interactive dashboards, perfect for exploring and visualizing data.

IPython widgets, unfortunately, do not render on GitHub or nbviewer but you can still access the notebook and run locally.

# Start with some imports!from ipywidgets import interact, interact_manualimport ipywidgets as widgetsdef f(x): return x# Generate a slider interact(f, x=10,);# Booleans generate check-boxesinteract(f, x=True);# Strings generate text areasinteract(f, x='Hi there!');We can use this @interact decorator to quickly turn any ordinary function into an interactive widget.

@interactdef show_articles_more_than(column='claps', x=5000): return df.

loc[df[column] > x]Source: https://towardsdatascience.

com/interactive-controls-for-jupyter-notebooks-f5c94829aee6import osfrom IPython.

display import Image@interactdef show_images(file=os.

listdir('images/')): display(Image(fdir+file))Source: https://towardsdatascience.

com/interactive-controls-for-jupyter-notebooks-f5c94829aee6Now we can quickly cycle through all the images without re-running the cell.

This might actually be useful if you were building a convolutional neural network and wanted to examine the images your network had missclassified.

The uses of widgets for data exploration are boundless.

Another simple example is finding correlations between two columns:Widget for correlation between two columns.

There are numerous helpful examples on the ipywidgets GitHub.

2.

Play WidgetThe Play widget is useful to perform animations by iterating on a sequence of integers at a certain speed.

The value of the slider below is linked to the player.

play = widgets.

Play( # interval=10, value=50, min=0, max=100, step=1, description="Press play", disabled=False)slider = widgets.

IntSlider()widgets.

jslink((play, 'value'), (slider, 'value'))widgets.

HBox([play, slider])3.

Date pickerThe date picker widget works in Chrome and IE Edge but does not currently work in Firefox or Safari because they do not support the HTML date input field.

widgets.

DatePicker( description='Pick a Date', disabled=False)4.

Color pickerwidgets.

ColorPicker( concise=False, description='Pick a color', value='blue', disabled=False)5.

Tabstab_contents = ['P0', 'P1', 'P2', 'P3', 'P4']children = [widgets.

Text(description=name) for name in tab_contents]tab = widgets.

Tab()tab.

children = childrenfor i in range(len(children)): tab.

set_title(i, str(i))tab6.

Widgets for PlotsInteractive widgets are especially helpful for selecting data to plot.

We can use the same @interact decorator with functions that visualize our data:import cufflinks as cf @interactdef scatter_plot(x=list(df.

select_dtypes('number').

columns), y=list(df.

select_dtypes('number').

columns)[1:], theme=list(cf.

themes.

THEMES.

keys()), colorscale=list(cf.

colors.

_scales_names.

keys())): df.

iplot(kind='scatter', x=x, y=y, mode='markers', xTitle=x.

title(), yTitle=y.

title(), text='title', title=f'{y.

title()} vs {x.

title()}', theme=theme, colorscale=colorscale)Source: https://towardsdatascience.

com/interactive-controls-for-jupyter-notebooks-f5c94829aee6Here, cufflinks+plotly combination is used to make an interactive plot with interactive IPython widget controls.

If the plot was a little slow to update, we can use @interact_manual which requires a button for updating.

7.

QgridQgrid is a Jupyter notebook widget but mainly focussed at dataframes.

It uses SlickGrid to render pandas DataFrames within a Jupyter notebook.

This allows you to explore your DataFrames with intuitive scrolling, sorting and filtering controls, as well as edit your DataFrames by double-clicking cells.

The Github Repository contains more details and examples.

Install:#with pippip install qgridjupyter nbextension enable –py –sys-prefix qgrid# only required if you have not enabled the ipywidgets nbextension yetjupyter nbextension enable –py –sys-prefix widgetsnbextension#with conda# only required if you have not added conda-forge to your channels yetconda config –add channels conda-forgeconda install qgridQgridEmbedding URL/ PDF/YoutubeUsing IPython’s display module, you can easily embed an URLs, PDFs, and videos into your Jupyter Notebooks.

URLs#Note that http urls will not be displayed.

Only https are allowed inside the Iframefrom IPython.

display import IFrameIFrame('https://en.

wikipedia.

org/wiki/HTTPS', width=800, height=450)PDFsfrom IPython.

display import IFrameIFrame('https://arxiv.

org/pdf/1406.

2661.

pdf', width=800, height=450)Youtube Videosfrom IPython.

display import YouTubeVideoYouTubeVideo('9SA7FaKxZVI', width=800, height=300)Jupyter ThemesJust like your IDE’s schema or theme setting, Jupyter also have themes which can get the same thematic feeling.

These themes also enhance the presentation of the code.

For installation:pip install jupyterthemesList out the available themes with:jt -lThough not high in numbers, these are still pretty good and more are gonna come in future.

Choose your favourite with:jt -t <name of the theme>Reset to original with:jt -rChesterish and solarizedlWrapping Things UpIf you haven’t read my last one, I urge you to read it as this one is adding more content to the last one.

Install these Jupyter Notebook add-ons, spend some time figuring out which ones are useful to you, and improve your productivity.

While none of these is life-changing, they all add just enough benefit to be worthwhile, cumulatively saving you hours of valuable development time.

If you wanna know why so much content on notebooks, take a look at this.

TAh TAh!!.

. More details

Leave a Reply