Skip to content

Commit 96148fd

Browse files
authored
Merge pull request #7 from choldgraf/binder
adding a binder example
2 parents 7a02a56 + 3c4763c commit 96148fd

File tree

8 files changed

+241
-1
lines changed

8 files changed

+241
-1
lines changed

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ global-exclude *.pyc
3232
global-exclude *.pyo
3333
global-exclude .git
3434
global-exclude .ipynb_checkpoints
35+
36+
# Binder files to be excluded
37+
exclude binder
38+
recursive-exclude binder *.ipynb
39+
recursive-exclude binder *.txt

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/jupyter/nbclient/master?filepath=binder%2Frun_nbclient.ipynb)
12
[![Travis Build Status](https://travis-ci.org/jupyter/nbclient.svg?branch=master)](https://travis-ci.org/jupyter/nbclient)
23
[![image](https://codecov.io/github/jupyter/nbclient/coverage.svg?branch=master)](https://codecov.io/github/jupyter/nbclient?branch=master)
34
[![Python 3.5](https://img.shields.io/badge/python-3.5-blue.svg)](https://www.python.org/downloads/release/python-350/)

binder/empty_notebook.ipynb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Show a pandas dataframe"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"import pandas as pd\n",
17+
"import numpy as np\n",
18+
"import scrapbook as sb"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"data = pd.DataFrame(np.random.randn(20, 2), columns=['a', 'b'])\n",
28+
"data"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"# Use scrapbook to store this data in the notebook\n",
38+
"sb.glue('dataframe', data.to_dict())"
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {},
44+
"source": [
45+
"# Make a matplotlib plot"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"import matplotlib.pyplot as plt"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"# Make and display a plot\n",
64+
"fig, ax = plt.subplots()\n",
65+
"ax.scatter(data['a'], data['b'])\n",
66+
"sb.glue('plot', fig, 'display')"
67+
]
68+
}
69+
],
70+
"metadata": {
71+
"kernelspec": {
72+
"display_name": "Python 3",
73+
"language": "python",
74+
"name": "python3"
75+
},
76+
"language_info": {
77+
"codemirror_mode": {
78+
"name": "ipython",
79+
"version": 3
80+
},
81+
"file_extension": ".py",
82+
"mimetype": "text/x-python",
83+
"name": "python",
84+
"nbconvert_exporter": "python",
85+
"pygments_lexer": "ipython3",
86+
"version": "3.7.3"
87+
},
88+
"widgets": {
89+
"application/vnd.jupyter.widget-state+json": {
90+
"state": {},
91+
"version_major": 2,
92+
"version_minor": 0
93+
}
94+
}
95+
},
96+
"nbformat": 4,
97+
"nbformat_minor": 4
98+
}

binder/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
numpy
2+
pandas
3+
matplotlib
4+
nteract-scrapbook
5+
nbformat

binder/run_nbclient.ipynb

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import nbclient\n",
10+
"import nbformat as nbf\n",
11+
"import pandas as pd\n",
12+
"import scrapbook as sb"
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"metadata": {},
18+
"source": [
19+
"# Background\n",
20+
"\n",
21+
"This notebook uses `nbclient` to read and execute an *empty* notebook.\n",
22+
"The empty notebook generates some fake data, makes a plot, and stores\n",
23+
"both the data and the plot inside the notebook using the\n",
24+
"[scrapbook package](https://github.com/nteract/scrapbook). We will\n",
25+
"then be able to access the generated contents of the notebook here.\n",
26+
"\n",
27+
"You can see the empty notebook by clicking this button:\n",
28+
"\n",
29+
"<a href=\"empty_notebook.ipynb\"><button>Empty notebook</button></a>"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"# Read and execute the empty notebook"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"# We use nbformat to represent our empty notebook in-memory\n",
46+
"nb = nbf.read('./empty_notebook.ipynb', nbf.NO_CONVERT)\n",
47+
"\n",
48+
"# Execute our in-memory notebook, which will now have outputs\n",
49+
"nb = nbclient.execute(nb)"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"# Inspect the new notebook for its contents"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"# First we'll convert our nbformat NotebokNote into a *scrapbook* NotebookNode\n",
66+
"nb = sb.read_notebook(nb)"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"# We can access the dataframe that was created and glued into the empty notebook\n",
76+
"pd.DataFrame.from_dict(nb.scraps.get('dataframe').data).head()"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": null,
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"# We can also access the generated plot by \"re-gluing\" the notebook here\n",
86+
"nb.reglue('plot')"
87+
]
88+
}
89+
],
90+
"metadata": {
91+
"kernelspec": {
92+
"display_name": "Python 3",
93+
"language": "python",
94+
"name": "python3"
95+
},
96+
"language_info": {
97+
"codemirror_mode": {
98+
"name": "ipython",
99+
"version": 3
100+
},
101+
"file_extension": ".py",
102+
"mimetype": "text/x-python",
103+
"name": "python",
104+
"nbconvert_exporter": "python",
105+
"pygments_lexer": "ipython3",
106+
"version": "3.7.3"
107+
},
108+
"widgets": {
109+
"application/vnd.jupyter.widget-state+json": {
110+
"state": {},
111+
"version_major": 2,
112+
"version_minor": 0
113+
}
114+
}
115+
},
116+
"nbformat": 4,
117+
"nbformat_minor": 4
118+
}

docs/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ NBClient lets you:
1010
Similar in nature to jupyter_client, as the jupyter_client is to the jupyter
1111
protocol nbclient is to notebooks allowing for execution contexts to be run.
1212

13+
To demo **NBClient** interactively, click the Binder link below:
14+
15+
.. image:: https://mybinder.org/badge_logo.svg
16+
:target: https://mybinder.org/v2/gh/jupyter/nbclient/master?filepath=binder%2Frun_nbclient.ipynb
17+
1318
Origins
1419
-------
1520

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def read_reqs(fname):
3737

3838
long_description = read(os.path.join(os.path.dirname(__file__), "README.md"))
3939
requirements = read(os.path.join(os.path.dirname(__file__), "requirements.txt"))
40-
dev_reqs = read_reqs('requirements-dev.txt')
40+
dev_reqs = read_reqs(os.path.join(os.path.dirname(__file__), 'requirements-dev.txt'))
4141
extras_require = {"test": dev_reqs, "dev": dev_reqs}
4242

4343
setup(

tox.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,16 @@ basepython =
4646
py38: python3.8
4747
flake8: python3.8
4848
manifest: python3.8
49+
binder: python3.8
4950
dist: python3.8
5051
docs: python3.8
5152
deps = .[dev]
5253
commands =
5354
pytest -vv --maxfail=2 --cov=nbclient -W always {posargs}
55+
56+
# Binder
57+
[testenv:binder]
58+
description = ensure /binder/*ipynb are runnable
59+
deps =
60+
-r binder/requirements.txt
61+
commands = python -c "from glob import glob; from nbclient import execute; import nbformat as nbf; [execute(nbf.read(input, nbf.NO_CONVERT), cwd='./binder') for input in glob('binder/**/*.ipynb')]"

0 commit comments

Comments
 (0)