5
5
A module for creating and manipulating dashboard content. You can create
6
6
a Dashboard object, insert boxes, swap boxes, remove a box and get an HTML
7
7
preview of the Dashboard.
8
+
9
+ The current workflow for making and manipulating dashboard follows:
10
+ 1) Create a Dashboard
11
+ 2) Modify the Dashboard (insert, swap, remove, etc)
12
+ 3) Preview the Dashboard (run `.get_preview()`)
13
+ 4) Repeat steps 2) and 3) as long as desired
14
+
15
+ The basic box object that your insert into a dashboard is just a `dict`.
16
+ The minimal dict for a box is:
17
+
18
+ ```
19
+ {
20
+ 'type': 'box',
21
+ 'boxType': 'plot'
22
+ }
23
+ ```
24
+
25
+ where 'fileId' can be set to the 'username:#' of your plot. The other
26
+ parameters
27
+ a box takes are `shareKey` (default is None) and `title` (default is '').
28
+
29
+ You will need to use the `.get_preview()` method quite regularly as this will
30
+ return an HTML representation of the dashboard in which the boxes in the HTML
31
+ are labelled with on-the-fly-generated numbers which change after each
32
+ modification to the dashboard.
33
+
34
+ Example: Create a simple Dashboard object
35
+ ```
36
+ import plotly.dashboard_objs as dashboard
37
+
38
+ box_1 = {
39
+ 'type': 'box',
40
+ 'boxType': 'plot',
41
+ 'fileId': 'username:some#',
42
+ 'title': 'box 1'
43
+ }
44
+
45
+ box_2 = {
46
+ 'type': 'box',
47
+ 'boxType': 'plot',
48
+ 'fileId': 'username:some#',
49
+ 'title': 'box 2'
50
+ }
51
+
52
+ box_3 = {
53
+ 'type': 'box',
54
+ 'boxType': 'plot',
55
+ 'fileId': 'username:some#',
56
+ 'title': 'box 3'
57
+ }
58
+
59
+ my_dboard = dashboard.Dashboard()
60
+ my_dboard.insert(box_1)
61
+ # my_dboard.get_preview()
62
+ my_dboard.insert(box_2, 'above', 1)
63
+ # my_dboard.get_preview()
64
+ my_dboard.insert(box_3, 'left', 2)
65
+ # my_dboard.get_preview()
66
+ my_dboard.swap(1, 2)
67
+ # my_dboard.get_preview()
68
+ my_dboard.remove(1)
69
+ # my_dboard.get_preview()
70
+ ```
8
71
"""
9
72
10
73
import pprint
@@ -134,6 +197,70 @@ def _add_html_text(dashboard_html, text, top_left_x, top_left_y, box_w, box_h):
134
197
135
198
136
199
class Dashboard (dict ):
200
+ """
201
+ Dashboard class for creating interactive dashboard objects.
202
+
203
+ Dashboards are dicts that contain boxes which hold plot information.
204
+ These boxes can be arranged in various ways. The most basic form of
205
+ a box is:
206
+
207
+ ```
208
+ {
209
+ 'type': 'box',
210
+ 'boxType': 'plot'
211
+ }
212
+ ```
213
+
214
+ where 'fileId' can be set to the 'username:#' of your plot. The other
215
+ parameters a box takes are `shareKey` (default is None) and `title`
216
+ (default is '').
217
+
218
+ `.get_preview()` should be called quite regularly to get an HTML
219
+ representation of the dashboard in which the boxes in the HTML
220
+ are labelled with on-the-fly-generated numbers or box ids which
221
+ change after each modification to the dashboard.
222
+
223
+ `.get_box()` returns the box located in the dashboard by calling
224
+ its box id as displayed via `.get_preview()`.
225
+
226
+ Example: Create a simple Dashboard object
227
+ ```
228
+ import plotly.dashboard_objs as dashboard
229
+
230
+ box_1 = {
231
+ 'type': 'box',
232
+ 'boxType': 'plot',
233
+ 'fileId': 'username:some#',
234
+ 'title': 'box 1'
235
+ }
236
+
237
+ box_2 = {
238
+ 'type': 'box',
239
+ 'boxType': 'plot',
240
+ 'fileId': 'username:some#',
241
+ 'title': 'box 2'
242
+ }
243
+
244
+ box_3 = {
245
+ 'type': 'box',
246
+ 'boxType': 'plot',
247
+ 'fileId': 'username:some#',
248
+ 'title': 'box 3'
249
+ }
250
+
251
+ my_dboard = dashboard.Dashboard()
252
+ my_dboard.insert(box_1)
253
+ # my_dboard.get_preview()
254
+ my_dboard.insert(box_2, 'above', 1)
255
+ # my_dboard.get_preview()
256
+ my_dboard.insert(box_3, 'left', 2)
257
+ # my_dboard.get_preview()
258
+ my_dboard.swap(1, 2)
259
+ # my_dboard.get_preview()
260
+ my_dboard.remove(1)
261
+ # my_dboard.get_preview()
262
+ ```
263
+ """
137
264
def __init__ (self , content = None ):
138
265
if content is None :
139
266
content = {}
@@ -160,7 +287,7 @@ def _compute_box_ids(self):
160
287
max_id = max (box_ids_to_path .keys ())
161
288
except ValueError :
162
289
max_id = 0
163
- box_ids_to_path [max_id + 1 ] = node [1 ] # list(...)
290
+ box_ids_to_path [max_id + 1 ] = node [1 ]
164
291
165
292
return box_ids_to_path
166
293
@@ -355,6 +482,27 @@ def insert(self, box, side='above', box_id=None):
355
482
'left', and 'right'.
356
483
:param (int) box_id: the box id which is used as the reference box for
357
484
the insertion of the box.
485
+
486
+ Example:
487
+ ```
488
+ import plotly.dashboard_objs as dashboard
489
+
490
+ box_1 = {
491
+ 'type': 'box',
492
+ 'boxType': 'plot',
493
+ 'fileId': 'username:some#',
494
+ 'title': 'box 1'
495
+ }
496
+
497
+ my_dboard = dashboard.Dashboard()
498
+ my_dboard.insert(box_1)
499
+ my_dboard.insert(box_1, 'left', 1)
500
+ my_dboard.insert(box_1, 'below', 2)
501
+ my_dboard.insert(box_1, 'right', 3)
502
+ my_dboard.insert(box_1, 'above', 4)
503
+
504
+ my_dboard.get_preview()
505
+ ```
358
506
"""
359
507
box_ids_to_path = self ._compute_box_ids ()
360
508
@@ -403,7 +551,27 @@ def insert(self, box, side='above', box_id=None):
403
551
self ._set_container_sizes ()
404
552
405
553
def remove (self , box_id ):
406
- """Remove a box from the dashboard by its box_id."""
554
+ """
555
+ Remove a box from the dashboard by its box_id.
556
+
557
+ Example:
558
+ ```
559
+ import plotly.dashboard_objs as dashboard
560
+
561
+ box_1 = {
562
+ 'type': 'box',
563
+ 'boxType': 'plot',
564
+ 'fileId': 'username:some#',
565
+ 'title': 'box 1'
566
+ }
567
+
568
+ my_dboard = dashboard.Dashboard()
569
+ my_dboard.insert(box_1)
570
+ my_dboard.remove(1)
571
+
572
+ my_dboard.get_preview()
573
+ ```
574
+ """
407
575
box_ids_to_path = self ._compute_box_ids ()
408
576
if box_id not in box_ids_to_path :
409
577
raise exceptions .PlotlyError (ID_NOT_VALID_MESSAGE )
@@ -424,7 +592,41 @@ def remove(self, box_id):
424
592
self ._set_container_sizes ()
425
593
426
594
def swap (self , box_id_1 , box_id_2 ):
427
- """Swap two boxes with their specified ids."""
595
+ """
596
+ Swap two boxes with their specified ids.
597
+
598
+ Example:
599
+ ```
600
+ import plotly.dashboard_objs as dashboard
601
+
602
+ box_1 = {
603
+ 'type': 'box',
604
+ 'boxType': 'plot',
605
+ 'fileId': 'username:first#',
606
+ 'title': 'first box'
607
+ }
608
+
609
+ box_2 = {
610
+ 'type': 'box',
611
+ 'boxType': 'plot',
612
+ 'fileId': 'username:second#',
613
+ 'title': 'second box'
614
+ }
615
+
616
+ my_dboard = dashboard.Dashboard()
617
+ my_dboard.insert(box_1)
618
+ my_dboard.insert(box_2, 'above', 1)
619
+
620
+ # check box at box id 1
621
+ box_at_1 = my_dboard.get_box(1)
622
+ print(box_at_1)
623
+
624
+ my_dboard.swap(1, 2)
625
+
626
+ box_after_swap = my_dboard.get_box(1)
627
+ print(box_after_swap)
628
+ ```
629
+ """
428
630
box_ids_to_path = self ._compute_box_ids ()
429
631
box_1 = self .get_box (box_id_1 )
430
632
box_2 = self .get_box (box_id_2 )
0 commit comments