forked from plotly/plotly.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_renderers.py
380 lines (289 loc) · 11.1 KB
/
test_renderers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import json
import sys
import base64
import threading
import time
import pytest
import requests
import numpy as np
import webbrowser
import plotly.graph_objs as go
import plotly.io as pio
from plotly.offline import get_plotlyjs
from plotly.io._utils import plotly_cdn_url
if sys.version_info >= (3, 3):
import unittest.mock as mock
from unittest.mock import MagicMock
else:
import mock
from mock import MagicMock
# fixtures
# --------
@pytest.fixture
def fig1(request):
return go.Figure(
data=[
{
"type": "scatter",
"y": np.array([2, 1, 3, 2, 4, 2]),
"marker": {"color": "green"},
}
],
layout={"title": {"text": "Figure title"}},
)
# JSON
# ----
def test_json_renderer_mimetype(fig1):
pio.renderers.default = "json"
expected = {"application/json": json.loads(pio.to_json(fig1, remove_uids=False))}
pio.renderers.render_on_display = False
with mock.patch("IPython.display.display") as mock_display:
fig1._ipython_display_()
mock_display.assert_not_called()
pio.renderers.render_on_display = True
with mock.patch("IPython.display.display") as mock_display:
fig1._ipython_display_()
mock_display.assert_called_once_with(expected, raw=True)
def test_json_renderer_show(fig1):
pio.renderers.default = "json"
expected_bundle = {
"application/json": json.loads(pio.to_json(fig1, remove_uids=False))
}
with mock.patch("IPython.display.display") as mock_display:
pio.show(fig1)
mock_display.assert_called_once_with(expected_bundle, raw=True)
def test_json_renderer_show_override(fig1):
pio.renderers.default = "notebook"
expected_bundle = {
"application/json": json.loads(pio.to_json(fig1, remove_uids=False))
}
with mock.patch("IPython.display.display") as mock_display:
pio.show(fig1, renderer="json")
mock_display.assert_called_once_with(expected_bundle, raw=True)
# Plotly mimetype
# ---------------
plotly_mimetype = "application/vnd.plotly.v1+json"
plotly_mimetype_renderers = ["plotly_mimetype", "jupyterlab", "vscode", "nteract"]
@pytest.mark.parametrize("renderer", plotly_mimetype_renderers)
def test_plotly_mimetype_renderer_mimetype(fig1, renderer):
pio.renderers.default = renderer
expected = {plotly_mimetype: json.loads(pio.to_json(fig1, remove_uids=False))}
expected[plotly_mimetype]["config"] = {"plotlyServerURL": "https://plot.ly"}
pio.renderers.render_on_display = False
with mock.patch("IPython.display.display") as mock_display:
fig1._ipython_display_()
mock_display.assert_not_called()
pio.renderers.render_on_display = True
with mock.patch("IPython.display.display") as mock_display:
fig1._ipython_display_()
mock_display.assert_called_once_with(expected, raw=True)
@pytest.mark.parametrize("renderer", plotly_mimetype_renderers)
def test_plotly_mimetype_renderer_show(fig1, renderer):
pio.renderers.default = renderer
expected = {plotly_mimetype: json.loads(pio.to_json(fig1, remove_uids=False))}
expected[plotly_mimetype]["config"] = {"plotlyServerURL": "https://plot.ly"}
with mock.patch("IPython.display.display") as mock_display:
pio.show(fig1)
mock_display.assert_called_once_with(expected, raw=True)
# Static Image
# ------------
# See plotly/tests/test_orca/test_image_renderers.py
# HTML
# ----
def assert_full_html(html):
assert html.startswith("<html")
def assert_not_full_html(html):
assert not html.startswith("<html")
def assert_html_renderer_connected(html):
assert plotly_cdn_url().rstrip(".js") in html
def assert_offline(html):
assert get_plotlyjs() in html
def assert_requirejs(html):
assert 'require(["plotly"]' in html
def assert_not_requirejs(html):
assert 'require(["plotly"]' not in html
def test_colab_renderer_show(fig1):
pio.renderers.default = "colab"
with mock.patch("IPython.display.display") as mock_display:
pio.show(fig1)
# Get display call arguments
mock_call_args = mock_display.call_args
mock_arg1 = mock_call_args[0][0]
# Check for html bundle
assert list(mock_arg1) == ["text/html"]
# Check html contents
html = mock_arg1["text/html"]
assert_full_html(html)
assert_html_renderer_connected(html)
assert_not_requirejs(html)
# check kwargs
mock_kwargs = mock_call_args[1]
assert mock_kwargs == {"raw": True}
@pytest.mark.parametrize(
"name,connected",
[("notebook", False), ("notebook_connected", True), ("kaggle", True)],
)
def test_notebook_connected_show(fig1, name, connected):
# Set renderer
pio.renderers.default = name
# Show
with mock.patch("IPython.display.display_html") as mock_display_html:
with mock.patch("IPython.display.display") as mock_display:
pio.show(fig1)
# ### Check initialization ###
# Get display call arguments
mock_call_args_html = mock_display_html.call_args
mock_arg1_html = mock_call_args_html[0][0]
# Check init display contents
bundle_display_html = mock_arg1_html
if connected:
assert_html_renderer_connected(bundle_display_html)
else:
assert_offline(bundle_display_html)
# ### Check display call ###
# Get display call arguments
mock_call_args = mock_display.call_args
mock_arg1 = mock_call_args[0][0]
# Check for html bundle
assert list(mock_arg1) == ["text/html"]
# Check html display contents
bundle_html = mock_arg1["text/html"]
assert_not_full_html(bundle_html)
assert_requirejs(bundle_html)
# check kwargs
mock_kwargs = mock_call_args[1]
assert mock_kwargs == {"raw": True}
# Browser
# -------
@pytest.mark.parametrize("renderer", ["browser", "chrome", "firefox"])
def test_browser_renderer_show(fig1, renderer):
pio.renderers.default = renderer
renderer_obj = pio.renderers[renderer]
# scan through webbrowser._browsers.keys() and assign the browser name registered with os
renderer_obj.using = [i for i in webbrowser._browsers.keys() if renderer in i][0]
# Setup mocks
mock_get = MagicMock(name="test get")
mock_browser = MagicMock(name="test browser")
mock_get.return_value = mock_browser
request_responses = []
def perform_request(url):
request_responses.append(requests.get(url))
def open_url(url, new=0, autoraise=True):
print("open url")
# Perform request in thread so that we don't block
request_thread = threading.Thread(target=lambda: perform_request(url))
request_thread.daemon = True
request_thread.start()
mock_browser.open.side_effect = open_url
with mock.patch("webbrowser.get", mock_get):
pio.show(fig1)
# check get args
mock_get.assert_called_once_with(renderer_obj.using)
# check open args
mock_call_args = mock_browser.open.call_args
mock_arg1 = mock_call_args[0][0]
mock_arg1.startswith("http://127.0.0.1:")
mock_kwargs = mock_call_args[1]
assert mock_kwargs == dict(new=renderer_obj.new, autoraise=renderer_obj.autoraise)
# Give request content a little time to show up
tries = 0
while tries < 5 and not request_responses:
time.sleep(0.5)
# Check request content
assert len(request_responses) == 1
response = request_responses[0]
assert response.status_code == 200
html = response.content.decode("utf8")
assert_full_html(html)
assert_offline(html)
assert_not_requirejs(html)
# Validation
# ----------
@pytest.mark.parametrize("renderer", ["bogus", "json+bogus", "bogus+chrome"])
def test_reject_invalid_renderer(renderer):
with pytest.raises(ValueError) as e:
pio.renderers.default = renderer
e.match("Invalid named renderer")
@pytest.mark.parametrize(
"renderer", ["json", "json+firefox", "chrome+colab+notebook+vscode"]
)
def test_accept_valid_renderer(renderer):
pio.renderers.default = renderer
@pytest.mark.parametrize(
"renderer",
plotly_mimetype_renderers
+ ["notebook", "notebook_connected", "browser", "notebook+plotly_mimetype"],
)
def test_repr_html(renderer):
pio.renderers.default = renderer
fig = go.Figure()
fig.update_layout(template=None)
str_html = fig._repr_html_()
bundle = fig._repr_mimebundle_()
# id number of figure
id_html = str_html.split('document.getElementById("')[1].split('")')[0]
id_pattern = "cd462b94-79ce-42a2-887f-2650a761a144"
template = (
'<div> <script type="text/javascript">'
"window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n "
'<script src="' + plotly_cdn_url() + '"></script> '
'<div id="cd462b94-79ce-42a2-887f-2650a761a144" class="plotly-graph-div" '
'style="height:100%; width:100%;"></div> <script type="text/javascript">'
" window.PLOTLYENV=window.PLOTLYENV || {};"
' if (document.getElementById("cd462b94-79ce-42a2-887f-2650a761a144"))'
' { Plotly.newPlot( "cd462b94-79ce-42a2-887f-2650a761a144",'
' [], {"template":{}},'
' {"responsive": true} ) };'
" </script> </div>"
)
if "text/html" in bundle:
str_bundle = bundle["text/html"]
id_bundle = str_bundle.split('document.getElementById("')[1].split('")')[0]
assert str_html.replace(id_html, "") == str_bundle.replace(id_bundle, "")
else:
assert str_html.replace(id_html, "") == template.replace(id_pattern, "")
all_renderers_without_orca = [
"plotly_mimetype",
"jupyterlab",
"nteract",
"vscode",
"notebook",
"notebook_connected",
"kaggle",
"azure",
"colab",
"cocalc",
"databricks",
"json",
"browser",
"firefox",
"chrome",
"chromium",
"iframe",
"iframe_connected",
"sphinx_gallery",
]
@pytest.mark.parametrize("renderer_str", all_renderers_without_orca)
def test_repr_mimebundle(renderer_str):
pio.renderers.default = renderer_str
fig = go.Figure()
fig.update_layout(template=None)
bundle = fig._repr_mimebundle_()
renderer = pio.renderers[renderer_str]
from plotly.io._renderers import MimetypeRenderer
if isinstance(renderer, MimetypeRenderer):
ref_bundle = renderer.to_mimebundle(fig.to_dict())
for key in bundle:
if "getElementById" in bundle[key]:
id1 = bundle[key].split('document.getElementById("')[1].split('")')[0]
id2 = (
ref_bundle[key].split('document.getElementById("')[1].split('")')[0]
)
assert bundle[key].replace(id1, "") == ref_bundle[key].replace(id2, "")
else:
assert bundle == {}
def test_repr_mimebundle_mixed_renderer(fig1):
pio.renderers.default = "notebook+plotly_mimetype"
assert set(fig1._repr_mimebundle_().keys()) == set(
{"application/vnd.plotly.v1+json", "text/html"}
)