Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c84e12

Browse files
committed
added tests and fixed size algo
1 parent cf2f74a commit 0c84e12

File tree

5 files changed

+191
-27
lines changed

5 files changed

+191
-27
lines changed

plotly/api/v2/dashboards.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def retrieve(fid):
3232
def update(fid, content):
3333
"""Completely update the writable."""
3434
url = build_url(RESOURCE, id=fid)
35-
return request('put', url)
35+
print url
36+
return request('put', url, json=content)
3637

3738

3839
def schema():

plotly/dashboard_objs/dashboard_objs.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
IPython = optional_imports.get_module('IPython')
1616

17-
# default variables
17+
# default HTML parameters
1818
MASTER_WIDTH = 400
1919
MASTER_HEIGHT = 400
2020
FONT_SIZE = 10
21+
2122
ID_NOT_VALID_MESSAGE = (
22-
"Your box_id must a number in your dashboard. To view a "
23-
"representation of your dashboard run 'get_preview()'."
23+
"Your box_id must be a number in your dashboard. To view a "
24+
"representation of your dashboard run get_preview()."
2425
)
2526

2627

@@ -103,11 +104,6 @@ def _draw_line_through_box(dashboard_html, top_left_x, top_left_y, box_w,
103104
new_box_w = box_w
104105
new_box_h = 1
105106

106-
#new_top_left_x = top_left_x + is_horizontal * 0.5 * box_w
107-
#new_top_left_y = top_left_y + (not is_horizontal) * 0.5 * box_h
108-
#new_box_w = (not is_horizontal) * box_w + is_horizontal
109-
#new_box_h = (not is_horizontal) + is_horizontal * box_h
110-
111107
html_box = """<!-- Draw some lines in -->
112108
context.beginPath();
113109
context.rect({top_left_x}, {top_left_y}, {box_w}, {box_h});
@@ -169,7 +165,8 @@ def _compute_box_ids(self):
169165
return box_ids_to_path
170166

171167
def _insert(self, box_or_container, path):
172-
if any(first_second not in ['first', 'second'] for first_second in path):
168+
if any(first_second not in ['first', 'second']
169+
for first_second in path):
173170
raise exceptions.PlotlyError(
174171
"Invalid path. Your 'path' list must only contain "
175172
"the strings 'first' and 'second'."
@@ -195,16 +192,17 @@ def _set_container_sizes(self):
195192
if ['second'] in all_paths:
196193
all_paths.remove(['second'])
197194

195+
# set dashboard_height proportional to max_path_len
198196
max_path_len = max(len(path) for path in all_paths)
199-
for path_len in range(1, max_path_len + 1):
200-
for path in [path for path in all_paths if len(path) == path_len]:
201-
if self._path_to_box(path)['type'] == 'split':
202-
if self._path_to_box(path)['direction'] == 'horizontal':
203-
new_size = MASTER_WIDTH / (2**path_len)
204-
elif self._path_to_box(path)['direction'] == 'vertical':
205-
new_size = MASTER_HEIGHT / (2**path_len)
197+
dashboard_height = 500 + 250 * max_path_len
198+
self['layout']['size'] = dashboard_height
199+
self['layout']['sizeUnit'] = 'px'
206200

207-
self._path_to_box(path)['size'] = new_size
201+
for path in all_paths:
202+
if len(path) != 0:
203+
if self._path_to_box(path)['type'] == 'split':
204+
self._path_to_box(path)['size'] = 50
205+
self._path_to_box(path)['sizeUnit'] = '%'
208206

209207
def _path_to_box(self, path):
210208
loc_in_dashboard = self['layout']
@@ -364,8 +362,7 @@ def insert(self, box, side='above', box_id=None):
364362

365363
# doesn't need box_id or side specified for first box
366364
if 'first' not in self['layout']:
367-
self._insert(_container(), [])
368-
self._insert(box, ['first'])
365+
self._insert(_container(box, _empty_box()), [])
369366
else:
370367
if box_id is None:
371368
raise exceptions.PlotlyError(

plotly/plotly/plotly.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,6 @@ def upload(cls, dashboard, filename, sharing='public', auto_open=True):
13761376
'world_readable': world_readable
13771377
}
13781378

1379-
#res = v2.dashboards.update(data)
13801379
res = v2.dashboards.create(data)
13811380
res.raise_for_status()
13821381

@@ -1392,7 +1391,6 @@ def upload(cls, dashboard, filename, sharing='public', auto_open=True):
13921391

13931392
@classmethod
13941393
def _get_all_dashboards(cls):
1395-
"""Grab a list of all users' dashboards."""
13961394
dashboards = []
13971395
res = v2.dashboards.list().json()
13981396

@@ -1422,16 +1420,13 @@ def _get_dashboard_json(cls, dashboard_name):
14221420

14231421
@classmethod
14241422
def get_dashboard(cls, dashboard_name):
1425-
"""
1426-
BETA pass of getting a dashboard from Plotly.
1427-
1428-
May need to put in dashboard_ops.
1429-
"""
1423+
"""Returns a Dashboard object from a dashboard name."""
14301424
dashboard_json = cls._get_dashboard_json(dashboard_name)
14311425
return dashboard.Dashboard(dashboard_json)
14321426

14331427
@classmethod
14341428
def get_dashboard_names(cls):
1429+
"""Return list of all active dashboard names from users' account."""
14351430
dashboards = cls._get_all_dashboards()
14361431
return [str(dboard['filename']) for dboard in dashboards]
14371432

plotly/tests/test_core/test_dashboard/__init__.py

Whitespace-only changes.
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"""
2+
test_dashboard:
3+
==========
4+
5+
A module intended for use with Nose.
6+
7+
"""
8+
from __future__ import absolute_import
9+
10+
from unittest import TestCase
11+
from plotly.exceptions import PlotlyError
12+
import plotly.dashboard_objs.dashboard_objs as dashboard
13+
14+
15+
class TestDashboard(TestCase):
16+
17+
def test_invalid_path(self):
18+
19+
my_box = {
20+
'type': 'box',
21+
'boxType': 'plot',
22+
'fileId': 'AdamKulidjian:327',
23+
'shareKey': None,
24+
'title': 'box 1'
25+
}
26+
dash = dashboard.Dashboard()
27+
28+
message = (
29+
"Invalid path. Your 'path' list must only contain "
30+
"the strings 'first' and 'second'."
31+
)
32+
33+
self.assertRaisesRegexp(PlotlyError, message,
34+
dash._insert, my_box, 'third')
35+
36+
def test_box_id_none(self):
37+
38+
my_box = {
39+
'type': 'box',
40+
'boxType': 'plot',
41+
'fileId': 'AdamKulidjian:327',
42+
'shareKey': None,
43+
'title': 'box 1'
44+
}
45+
46+
dash = dashboard.Dashboard()
47+
dash.insert(my_box, 'above', None)
48+
49+
message = (
50+
"Make sure the box_id is specfied if there is at least "
51+
"one box in your dashboard."
52+
)
53+
54+
self.assertRaisesRegexp(PlotlyError, message, dash.insert,
55+
my_box, 'above', None)
56+
57+
def test_id_not_valid(self):
58+
my_box = {
59+
'type': 'box',
60+
'boxType': 'plot',
61+
'fileId': 'AdamKulidjian:327',
62+
'shareKey': None,
63+
'title': 'box 1'
64+
}
65+
66+
message = (
67+
"Your box_id must be a number in your dashboard. To view a "
68+
"representation of your dashboard run get_preview()."
69+
)
70+
71+
dash = dashboard.Dashboard()
72+
dash.insert(my_box, 'above', 1)
73+
74+
# insert box
75+
self.assertRaisesRegexp(PlotlyError, message, dash.insert, my_box,
76+
'above', 0)
77+
# get box by id
78+
self.assertRaisesRegexp(PlotlyError, message, dash.get_box, 0)
79+
80+
# remove box
81+
self.assertRaisesRegexp(PlotlyError, message, dash.remove, 0)
82+
83+
def test_invalid_side(self):
84+
my_box = {
85+
'type': 'box',
86+
'boxType': 'plot',
87+
'fileId': 'AdamKulidjian:327',
88+
'shareKey': None,
89+
'title': 'box 1'
90+
}
91+
92+
message = (
93+
"If there is at least one box in your dashboard, you "
94+
"must specify a valid side value. You must choose from "
95+
"'above', 'below', 'left', and 'right'."
96+
)
97+
98+
dash = dashboard.Dashboard()
99+
dash.insert(my_box, 'above', 0)
100+
101+
self.assertRaisesRegexp(PlotlyError, message, dash.insert,
102+
my_box, 'somewhere', 1)
103+
104+
def test_dashboard_dict(self):
105+
my_box = {
106+
'type': 'box',
107+
'boxType': 'plot',
108+
'fileId': 'AdamKulidjian:327',
109+
'shareKey': None,
110+
'title': 'box 1'
111+
}
112+
113+
dash = dashboard.Dashboard()
114+
dash.insert(my_box, '', 0)
115+
dash.insert(my_box, 'above', 1)
116+
dash.insert(my_box, 'left', 2)
117+
dash.insert(my_box, 'right', 2)
118+
dash.insert(my_box, 'below', 4)
119+
120+
expected_dashboard = {
121+
'layout': {'direction': 'vertical',
122+
'first': {'direction': 'vertical',
123+
'first': {'direction': 'horizontal',
124+
'first': {'direction': 'vertical',
125+
'first': {'boxType': 'plot',
126+
'fileId': 'AdamKulidjian:327',
127+
'shareKey': None,
128+
'title': 'box 1',
129+
'type': 'box'},
130+
'second': {'boxType': 'plot',
131+
'fileId': 'AdamKulidjian:327',
132+
'shareKey': None,
133+
'title': 'box 1',
134+
'type': 'box'},
135+
'size': 50,
136+
'sizeUnit': '%',
137+
'type': 'split'},
138+
'second': {'direction': 'horizontal',
139+
'first': {'boxType': 'plot',
140+
'fileId': 'AdamKulidjian:327',
141+
'shareKey': None,
142+
'title': 'box 1',
143+
'type': 'box'},
144+
'second': {'boxType': 'plot',
145+
'fileId': 'AdamKulidjian:327',
146+
'shareKey': None,
147+
'title': 'box 1',
148+
'type': 'box'},
149+
'size': 50,
150+
'sizeUnit': '%',
151+
'type': 'split'},
152+
'size': 50,
153+
'sizeUnit': '%',
154+
'type': 'split'},
155+
'second': {'boxType': 'plot',
156+
'fileId': 'AdamKulidjian:327',
157+
'shareKey': None,
158+
'title': 'box 1',
159+
'type': 'box'},
160+
'size': 50,
161+
'sizeUnit': '%',
162+
'type': 'split'},
163+
'second': {'boxType': 'empty', 'type': 'box'},
164+
'size': 1500,
165+
'sizeUnit': 'px',
166+
'type': 'split'},
167+
'settings': {},
168+
'version': 2
169+
}
170+
171+
self.assertEqual(dash, expected_dashboard)

0 commit comments

Comments
 (0)