nbinteract

nbinteract is a Python package that creates interactive webpages from Jupyter notebooks. nbinteract has built-in support for interactive plotting. These interactions are driven by data, not callbacks, allowing authors to focus on the logic of their programs.

nbinteract is most useful for:

  • Data scientists that want to create simple interactive blog posts without having to know / work with Javascript.
  • Instructors that want to include interactive examples in their textbooks.
  • Students that want to publish data analysis that contains interactive demos.

Examples

Most plotting functions from other libraries (e.g. matplotlib) take data as input. nbinteract's plotting functions take functions as input.

import numpy as np
import nbinteract as nbi

def normal(mean, sd):
    '''Returns 1000 points drawn at random fron N(mean, sd)'''
    return np.random.normal(mean, sd, 1000)

normal(10, 1.0)
array([ 11.10032294,   8.01737258,   8.84975049, ...,   9.86721442,
        11.06511688,  10.88371858])
# Plot aesthetics
options = {
    'xlim': (-2, 12),
    'ylim': (0, 0.7),
    'bins': 20
}

# Pass in the `normal` function and let user change mean and sd.
# Whenever the user interacts with the sliders, the `normal` function
# is called and the returned data are plotted.
nbi.hist(normal, mean=(0, 10), sd=(0, 2.0), options=options)

Simulations are easy to create using nbinteract. In this simulation, we roll a die and plot the running average of the rolls. We can see that with more rolls, the average gets closer to the expected value: 3.5.

rolls = np.random.choice([1, 2, 3, 4, 5, 6], size=300)
averages = np.cumsum(rolls) / np.arange(1, 301)

def x_vals(num_rolls):
    return range(num_rolls)

# The function to generate y-values gets called with the
# x-values as its first argument.
def y_vals(xs):
    return averages[:len(xs)]
nbi.line(x_vals, y_vals, num_rolls=(1, 300))

Publishing

From a notebook cell:

# Run in a notebook cell to convert the notebook into a
# publishable HTML page
nbi.publish('landing_page.ipynb')

From the command line:

# Run on the command line to convert the notebook into a
# publishable HTML page.
nbinteract landing_page.ipynb

For more information on publishing, see the tutorial which has a complete walkthrough on publishing a notebook to the web.

Installation

Using pip:

pip install nbinteract

# The next two lines can be skipped for notebook version 5.3 and above
jupyter nbextension enable --py --sys-prefix widgetsnbextension
jupyter nbextension enable --py --sys-prefix bqplot

You may now import the nbinteract package in Python code and use the nbinteract CLI command to convert notebooks to HTML pages.

Documentation

Access the tutorials, examples, and documentation for nbinteract using the links in the sidebar.

results matching ""

    No results matching ""