Skip to content

Commit 1b03f27

Browse files
committed
adding a binder example
1 parent 7a02a56 commit 1b03f27

File tree

5 files changed

+230
-0
lines changed

5 files changed

+230
-0
lines changed

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+
scrapbook
4+
nbformat
5+
./

binder/run_nbclient.ipynb

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
"# The Executor class handles execution of the notebook\n",
49+
"executor = nbclient.execute.Executor(nb)\n",
50+
"\n",
51+
"# Update our in-memory notebook, which will now have outputs\n",
52+
"nb = executor.execute()"
53+
]
54+
},
55+
{
56+
"cell_type": "markdown",
57+
"metadata": {},
58+
"source": [
59+
"# Inspect the new notebook for its contents"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"# First we'll convert our nbformat NotebokNote into a *scrapbook* NotebookNode\n",
69+
"nb = sb.read_notebook(nb)"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": null,
75+
"metadata": {},
76+
"outputs": [],
77+
"source": [
78+
"# We can access the dataframe that was created and glued into the empty notebook\n",
79+
"pd.DataFrame.from_dict(nb.scraps.get('dataframe').data).head()"
80+
]
81+
},
82+
{
83+
"cell_type": "code",
84+
"execution_count": null,
85+
"metadata": {},
86+
"outputs": [],
87+
"source": [
88+
"# We can also access the generated plot by \"re-gluing\" the notebook here\n",
89+
"nb.reglue('plot')"
90+
]
91+
}
92+
],
93+
"metadata": {
94+
"kernelspec": {
95+
"display_name": "Python 3",
96+
"language": "python",
97+
"name": "python3"
98+
},
99+
"language_info": {
100+
"codemirror_mode": {
101+
"name": "ipython",
102+
"version": 3
103+
},
104+
"file_extension": ".py",
105+
"mimetype": "text/x-python",
106+
"name": "python",
107+
"nbconvert_exporter": "python",
108+
"pygments_lexer": "ipython3",
109+
"version": "3.7.3"
110+
},
111+
"widgets": {
112+
"application/vnd.jupyter.widget-state+json": {
113+
"state": {},
114+
"version_major": 2,
115+
"version_minor": 0
116+
}
117+
}
118+
},
119+
"nbformat": 4,
120+
"nbformat_minor": 4
121+
}

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

0 commit comments

Comments
 (0)