Skip to content

Commit c0cf315

Browse files
add tests
1 parent 465087b commit c0cf315

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# needs these two lines:
44
sudo: required
55
dist: trusty
6+
addons:
7+
chrome: stable
68

79
language: python
810
python:
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"application/vnd.jupyter.widget-view+json": {
11+
"model_id": "9aa0769fd66e42fcb83d86241d228062",
12+
"version_major": 2,
13+
"version_minor": 0
14+
},
15+
"text/plain": [
16+
"Image(value=b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x08\\x06\\x00\\x00\\x00\\x1f\\x15\\"
17+
]
18+
},
19+
"metadata": {},
20+
"output_type": "display_data"
21+
}
22+
],
23+
"source": [
24+
"# this generates a 1 pixel image with the rgba values (100, 200, 250, 255)\n",
25+
"# generated with the code in the next cells (kept for debugging purposes)\n",
26+
"import base64\n",
27+
"import ipywidgets\n",
28+
"image_data = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNIOfHrPwAG4AMmv//laQAAAABJRU5ErkJggg==')\n",
29+
"ipywidgets.Image(value=image_data)"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 2,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"# try:\n",
39+
"# from io import BytesIO as StringIO\n",
40+
"# except:\n",
41+
"# from cStringIO import StringIO\n",
42+
"# import PIL.Image\n"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 3,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"# f = StringIO(image_data)\n",
52+
"# image = PIL.Image.new('RGBA', (1,1))\n",
53+
"# image.putpixel((0, 0), (100, 200, 250, 255))\n",
54+
"# image.save(f, format='png')\n",
55+
"# base64.b64encode(f.getvalue())"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 4,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"# base64.b64encode(f.getvalue())"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 5,
70+
"metadata": {},
71+
"outputs": [],
72+
"source": [
73+
"# image = PIL.Image.open(f)\n",
74+
"# pixel = image.getpixel((0, 0))\n",
75+
"\n",
76+
"# #assert pixel == (100, 200, 250)\n",
77+
"\n",
78+
"# pixel"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {},
85+
"outputs": [],
86+
"source": []
87+
}
88+
],
89+
"metadata": {
90+
"kernelspec": {
91+
"display_name": "Python 3",
92+
"language": "python",
93+
"name": "python3"
94+
},
95+
"language_info": {
96+
"codemirror_mode": {
97+
"name": "ipython",
98+
"version": 3
99+
},
100+
"file_extension": ".py",
101+
"mimetype": "text/x-python",
102+
"name": "python",
103+
"nbconvert_exporter": "python",
104+
"pygments_lexer": "ipython3",
105+
"version": "3.6.4"
106+
}
107+
},
108+
"nbformat": 4,
109+
"nbformat_minor": 2
110+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import os
2+
3+
import nbformat
4+
from ..snapshot import SnapshotPreProcessor
5+
from ..execute import ExecutePreprocessor
6+
import PIL.Image
7+
try:
8+
from io import BytesIO as StringIO
9+
except:
10+
from cStringIO import StringIO
11+
import base64
12+
13+
current_dir = os.path.dirname(__file__)
14+
15+
def test_red_pixel():
16+
execute_preprocessor = ExecutePreprocessor(enabled=True)
17+
18+
input_file = os.path.join(current_dir, 'files', 'single-pixel.ipynb')
19+
spp = SnapshotPreProcessor(page_opener_class='headless')
20+
nb = nbformat.read(input_file, 4)
21+
resources = {}
22+
nb, resources = execute_preprocessor.preprocess(nb, resources)
23+
assert 'image/png' not in nb.cells[0].outputs[0].data
24+
nb, resources = spp.preprocess(nb, resources)
25+
assert 'image/png' in nb.cells[0].outputs[0].data
26+
# we cannot be sure the browser encodes it the same way
27+
# assert nb.cells[0].outputs[0].data['image/png'] == 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGNIOfHrPwAG4AMmv//laQAAAABJRU5ErkJggg=='
28+
# instead, we read the magic pixel values
29+
f = StringIO(base64.b64decode(nb.cells[0].outputs[0].data['image/png']))
30+
image = PIL.Image.open(f)
31+
pixel = image.getpixel((0, 0))
32+
assert pixel == (100, 200, 250, 255)

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ def run(self):
228228
jupyter_client_req = 'jupyter_client>=4.2'
229229

230230
extra_requirements = {
231-
'test': ['pytest', 'pytest-cov', 'ipykernel', 'jupyter_client>=4.2', 'ipywidgets>=7'],
231+
'test': ['pytest', 'pytest-cov', 'ipykernel', 'jupyter_client>=4.2', 'ipywidgets>=7', 'PyChromeDevTools', 'pillow'],
232232
'serve': ['tornado>=4.0'],
233+
'snapshot': ['PyChromeDevTools']
233234
'execute': [jupyter_client_req],
234235
'docs': ['sphinx>=1.5.1',
235236
'sphinx_rtd_theme',

0 commit comments

Comments
 (0)