-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathpresentation_objs.py
1177 lines (1048 loc) · 41.9 KB
/
presentation_objs.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
dashboard_objs
==========
A module for creating and manipulating spectacle-presentation dashboards.
"""
import copy
import random
import re
import string
import warnings
import _plotly_utils.exceptions
from chart_studio import exceptions
from chart_studio.config import get_config
HEIGHT = 700.0
WIDTH = 1000.0
CODEPANE_THEMES = ['tomorrow', 'tomorrowNight']
VALID_LANGUAGES = ['cpp', 'cs', 'css', 'fsharp', 'go', 'haskell', 'java',
'javascript', 'jsx', 'julia', 'xml', 'matlab', 'php',
'python', 'r', 'ruby', 'scala', 'sql', 'yaml']
VALID_TRANSITIONS = ['slide', 'zoom', 'fade', 'spin']
PRES_THEMES = ['moods', 'martik']
VALID_GROUPTYPES = [
'leftgroup_v', 'rightgroup_v', 'middle', 'checkerboard_topleft',
'checkerboard_topright'
]
fontWeight_dict = {
'Thin': {'fontWeight': 100},
'Thin Italic': {'fontWeight': 100, 'fontStyle': 'italic'},
'Light': {'fontWeight': 300},
'Light Italic': {'fontWeight': 300, 'fontStyle': 'italic'},
'Regular': {'fontWeight': 400},
'Regular Italic': {'fontWeight': 400, 'fontStyle': 'italic'},
'Medium': {'fontWeight': 500},
'Medium Italic': {'fontWeight': 500, 'fontStyle': 'italic'},
'Bold': {'fontWeight': 700},
'Bold Italic': {'fontWeight': 700, 'fontStyle': 'italic'},
'Black': {'fontWeight': 900},
'Black Italic': {'fontWeight': 900, 'fontStyle': 'italic'},
}
def list_of_options(iterable, conj='and', period=True):
"""
Returns an English listing of objects seperated by commas ','
For example, ['foo', 'bar', 'baz'] becomes 'foo, bar and baz'
if the conjunction 'and' is selected.
"""
if len(iterable) < 2:
raise _plotly_utils.exceptions.PlotlyError(
'Your list or tuple must contain at least 2 items.'
)
template = (len(iterable) - 2)*'{}, ' + '{} ' + conj + ' {}' + period*'.'
return template.format(*iterable)
# Error Messages
STYLE_ERROR = "Your presentation style must be {}".format(
list_of_options(PRES_THEMES, conj='or', period=True)
)
CODE_ENV_ERROR = (
"If you are putting a block of code into your markdown "
"presentation, make sure your denote the start and end "
"of the code environment with the '```' characters. For "
"example, your markdown string would include something "
"like:\n\n```python\nx = 2\ny = 1\nprint x\n```\n\n"
"Notice how the language that you want the code to be "
"displayed in is immediately to the right of first "
"entering '```', i.e. '```python'."
)
LANG_ERROR = (
"The language of your code block should be "
"clearly indicated after the first ``` that "
"begins the code block. The valid languages to "
"choose from are" + list_of_options(
VALID_LANGUAGES
)
)
def _generate_id(size):
letters_and_numbers = string.ascii_letters
for num in range(10):
letters_and_numbers += str(num)
letters_and_numbers += str(num)
id_str = ''
for _ in range(size):
id_str += random.choice(list(letters_and_numbers))
return id_str
paragraph_styles = {
'Body': {
'color': '#3d3d3d',
'fontFamily': 'Open Sans',
'fontSize': 11,
'fontStyle': 'normal',
'fontWeight': 400,
'lineHeight': 'normal',
'minWidth': 20,
'opacity': 1,
'textAlign': 'center',
'textDecoration': 'none',
'wordBreak': 'break-word'
},
'Body Small': {
'color': '#3d3d3d',
'fontFamily': 'Open Sans',
'fontSize': 10,
'fontStyle': 'normal',
'fontWeight': 400,
'lineHeight': 'normal',
'minWidth': 20,
'opacity': 1,
'textAlign': 'center',
'textDecoration': 'none'
},
'Caption': {
'color': '#3d3d3d',
'fontFamily': 'Open Sans',
'fontSize': 11,
'fontStyle': 'italic',
'fontWeight': 400,
'lineHeight': 'normal',
'minWidth': 20,
'opacity': 1,
'textAlign': 'center',
'textDecoration': 'none'
},
'Heading 1': {
'color': '#3d3d3d',
'fontFamily': 'Open Sans',
'fontSize': 26,
'fontStyle': 'normal',
'fontWeight': 400,
'lineHeight': 'normal',
'minWidth': 20,
'opacity': 1,
'textAlign': 'center',
'textDecoration': 'none',
},
'Heading 2': {
'color': '#3d3d3d',
'fontFamily': 'Open Sans',
'fontSize': 20,
'fontStyle': 'normal',
'fontWeight': 400,
'lineHeight': 'normal',
'minWidth': 20,
'opacity': 1,
'textAlign': 'center',
'textDecoration': 'none'
},
'Heading 3': {
'color': '#3d3d3d',
'fontFamily': 'Open Sans',
'fontSize': 11,
'fontStyle': 'normal',
'fontWeight': 700,
'lineHeight': 'normal',
'minWidth': 20,
'opacity': 1,
'textAlign': 'center',
'textDecoration': 'none'
}
}
def _empty_slide(transition, id):
empty_slide = {'children': [],
'id': id,
'props': {'style': {}, 'transition': transition}}
return empty_slide
def _box(boxtype, text_or_url, left, top, height, width, id, props_attr,
style_attr, paragraphStyle):
children_list = []
fontFamily = "Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace"
if boxtype == 'Text':
children_list = text_or_url.split('\n')
props = {
'isQuote': False,
'listType': None,
'paragraphStyle': paragraphStyle,
'size': 4,
'style': copy.deepcopy(paragraph_styles[paragraphStyle])
}
props['style'].update(
{'height': height,
'left': left,
'top': top,
'width': width,
'position': 'absolute'}
)
elif boxtype == 'Image':
# height, width are set to default 512
# as set by the Presentation Editor
props = {
'height': 512,
'imageName': None,
'src': text_or_url,
'style': {'height': height,
'left': left,
'opacity': 1,
'position': 'absolute',
'top': top,
'width': width},
'width': 512
}
elif boxtype == 'Plotly':
if '?share_key' in text_or_url:
src = text_or_url
else:
src = text_or_url + '.embed?link=false'
props = {
'frameBorder': 0,
'scrolling': 'no',
'src': src,
'style': {'height': height,
'left': left,
'position': 'absolute',
'top': top,
'width': width}
}
elif boxtype == 'CodePane':
props = {
'language': 'python',
'source': text_or_url,
'style': {'fontFamily': fontFamily,
'fontSize': 13,
'height': height,
'left': left,
'margin': 0,
'position': 'absolute',
'textAlign': 'left',
'top': top,
'width': width},
'theme': 'tomorrowNight'
}
# update props and style attributes
for item in props_attr.items():
props[item[0]] = item[1]
for item in style_attr.items():
props['style'][item[0]] = item[1]
child = {
'children': children_list,
'id': id,
'props': props,
'type': boxtype
}
if boxtype == 'Text':
child['defaultHeight'] = 36
child['defaultWidth'] = 52
child['resizeVertical'] = False
if boxtype == 'CodePane':
child['defaultText'] = 'Code'
return child
def _percentage_to_pixel(value, side):
if side == 'left':
return WIDTH * (0.01 * value)
elif side == 'top':
return HEIGHT * (0.01 * value)
elif side == 'height':
return HEIGHT * (0.01 * value)
elif side == 'width':
return WIDTH * (0.01 * value)
def _return_box_position(left, top, height, width):
values_dict = {
'left': left,
'top': top,
'height': height,
'width': width,
}
for key in iter(values_dict):
if isinstance(values_dict[key], str):
var = float(values_dict[key][: -2])
else:
var = _percentage_to_pixel(values_dict[key], key)
values_dict[key] = var
return (values_dict['left'], values_dict['top'],
values_dict['height'], values_dict['width'])
def _remove_extra_whitespace_from_line(line):
line = line.lstrip()
line = line.rstrip()
return line
def _list_of_slides(markdown_string):
if not markdown_string.endswith('\n---\n'):
markdown_string += '\n---\n'
text_blocks = re.split('\n-{2,}\n', markdown_string)
list_of_slides = []
for text in text_blocks:
if not all(char in ['\n', '-', ' '] for char in text):
list_of_slides.append(text)
if '\n-\n' in markdown_string:
msg = ("You have at least one '-' by itself on its own line in your "
"markdown string. If you are trying to denote a new slide, "
"make sure that the line has 3 '-'s like this: \n\n---\n\n"
"A new slide will NOT be created here.")
warnings.warn(msg)
return list_of_slides
def _top_spec_for_text_at_bottom(text_block, width_per, per_from_bottom=0,
min_top=30):
# This function ensures that if there is a large block of
# text in your slide it will not overflow off the bottom
# of the slide.
# The input for this function are a block of text and the
# params that define where it will be placed in the slide.
# The function makes some calculations and will output a
# 'top' value (i.e. the left, top, height, width css params)
# so that the text block will come down to some specified
# distance from the bottom of the page.
# TODO: customize this function for different fonts/sizes
max_lines = 37
one_char_percent_width = 0.764
chars_in_full_line = width_per / one_char_percent_width
num_of_lines = 0
char_group = 0
for char in text_block:
if char == '\n':
num_of_lines += 1
char_group = 0
else:
if char_group >= chars_in_full_line:
char_group = 0
num_of_lines += 1
else:
char_group += 1
num_of_lines += 1
top_frac = (max_lines - num_of_lines) / float(max_lines)
top = top_frac * 100 - per_from_bottom
# to be safe
return max(top, min_top)
def _box_specs_gen(num_of_boxes, grouptype='leftgroup_v', width_range=50,
height_range=50, margin=2, betw_boxes=4, middle_center=50):
# the (left, top, width, height) specs
# are added to specs_for_boxes
specs_for_boxes = []
if num_of_boxes == 1 and grouptype in ['leftgroup_v', 'rightgroup_v']:
if grouptype == 'rightgroup_v':
left_shift = (100 - width_range)
else:
left_shift = 0
box_spec = (
left_shift + (margin / WIDTH) * 100,
(margin / HEIGHT) * 100,
100 - (2 * margin / HEIGHT * 100),
width_range - (2 * margin / WIDTH) * 100
)
specs_for_boxes.append(box_spec)
elif num_of_boxes > 1 and grouptype in ['leftgroup_v', 'rightgroup_v']:
if grouptype == 'rightgroup_v':
left_shift = (100 - width_range)
else:
left_shift = 0
if num_of_boxes % 2 == 0:
box_width_px = 0.5 * (
(float(width_range)/100) * WIDTH - 2 * margin - betw_boxes
)
box_width = (box_width_px / WIDTH) * 100
height = (200.0 / (num_of_boxes * HEIGHT)) * (
HEIGHT - (num_of_boxes / 2 - 1) * betw_boxes - 2 * margin
)
left1 = left_shift + (margin / WIDTH) * 100
left2 = left_shift + (
((margin + betw_boxes) / WIDTH) * 100 + box_width
)
for left in [left1, left2]:
for j in range(int(num_of_boxes / 2)):
top = (margin * 100 / HEIGHT) + j * (
height + (betw_boxes * 100 / HEIGHT)
)
specs = (
left,
top,
height,
box_width
)
specs_for_boxes.append(specs)
if num_of_boxes % 2 == 1:
width = width_range - (200 * margin) / WIDTH
height = (100.0 / (num_of_boxes * HEIGHT)) * (
HEIGHT - (num_of_boxes - 1) * betw_boxes - 2 * margin
)
left = left_shift + (margin / WIDTH) * 100
for j in range(num_of_boxes):
top = (margin / HEIGHT) * 100 + j * (
height + (betw_boxes / HEIGHT) * 100
)
specs = (
left,
top,
height,
width
)
specs_for_boxes.append(specs)
elif grouptype == 'middle':
top = float(middle_center - (height_range / 2))
height = height_range
width = (1 / float(num_of_boxes)) * (
width_range - (num_of_boxes - 1) * (100*betw_boxes/WIDTH)
)
for j in range(num_of_boxes):
left = ((100 - float(width_range)) / 2) + j * (
width + (betw_boxes / WIDTH) * 100
)
specs = (left, top, height, width)
specs_for_boxes.append(specs)
elif 'checkerboard' in grouptype and num_of_boxes == 2:
if grouptype == 'checkerboard_topleft':
for j in range(2):
left = j * 50
top = j * 50
height = 50
width = 50
specs = (
left,
top,
height,
width
)
specs_for_boxes.append(specs)
else:
for j in range(2):
left = 50 * (1 - j)
top = j * 50
height = 50
width = 50
specs = (
left,
top,
height,
width
)
specs_for_boxes.append(specs)
return specs_for_boxes
def _return_layout_specs(num_of_boxes, url_lines, title_lines, text_block,
code_blocks, slide_num, style):
# returns specs of the form (left, top, height, width)
code_theme = 'tomorrowNight'
if style == 'martik':
specs_for_boxes = []
margin = 18 # in pxs
# set Headings styles
paragraph_styles['Heading 1'].update(
{'color': '#0D0A1E',
'fontFamily': 'Raleway',
'fontSize': 55,
'fontWeight': fontWeight_dict['Bold']['fontWeight']}
)
paragraph_styles['Heading 2'] = copy.deepcopy(
paragraph_styles['Heading 1']
)
paragraph_styles['Heading 2'].update({'fontSize': 36})
paragraph_styles['Heading 3'] = copy.deepcopy(
paragraph_styles['Heading 1']
)
paragraph_styles['Heading 3'].update({'fontSize': 30})
# set Body style
paragraph_styles['Body'].update(
{'color': '#96969C',
'fontFamily': 'Roboto',
'fontSize': 16,
'fontWeight': fontWeight_dict['Regular']['fontWeight']}
)
bkgd_color = '#F4FAFB'
title_font_color = '#0D0A1E'
text_font_color = '#96969C'
if num_of_boxes == 0 and slide_num == 0:
text_textAlign = 'center'
else:
text_textAlign = 'left'
if num_of_boxes == 0:
specs_for_title = (0, 50, 20, 100)
specs_for_text = (15, 60, 50, 70)
bkgd_color = '#0D0A1E'
title_font_color = '#F4FAFB'
text_font_color = '#F4FAFB'
elif num_of_boxes == 1:
if code_blocks != [] or (url_lines != [] and
get_config()['plotly_domain'] in
url_lines[0]):
if code_blocks != []:
w_range = 40
else:
w_range = 60
text_top = _top_spec_for_text_at_bottom(
text_block, 80,
per_from_bottom=(margin / HEIGHT) * 100
)
specs_for_title = (0, 3, 20, 100)
specs_for_text = (10, text_top, 30, 80)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='middle', width_range=w_range,
height_range=60, margin=margin, betw_boxes=4
)
bkgd_color = '#0D0A1E'
title_font_color = '#F4FAFB'
text_font_color = '#F4FAFB'
code_theme = 'tomorrow'
elif title_lines == [] and text_block == '':
specs_for_title = (0, 50, 20, 100)
specs_for_text = (15, 60, 50, 70)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='middle', width_range=50,
height_range=80, margin=0, betw_boxes=0
)
else:
title_text_width = 40 - (margin / WIDTH) * 100
text_top = _top_spec_for_text_at_bottom(
text_block, title_text_width,
per_from_bottom=(margin / HEIGHT) * 100
)
specs_for_title = (60, 3, 20, 40)
specs_for_text = (60, text_top, 1, title_text_width)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='leftgroup_v', width_range=60,
margin=margin, betw_boxes=4
)
bkgd_color = '#0D0A1E'
title_font_color = '#F4FAFB'
text_font_color = '#F4FAFB'
elif num_of_boxes == 2 and url_lines != []:
text_top = _top_spec_for_text_at_bottom(
text_block, 46, per_from_bottom=(margin / HEIGHT) * 100,
min_top=50
)
specs_for_title = (0, 3, 20, 50)
specs_for_text = (52, text_top, 40, 46)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='checkerboard_topright'
)
elif num_of_boxes >= 2 and url_lines == []:
text_top = _top_spec_for_text_at_bottom(
text_block, 92, per_from_bottom=(margin / HEIGHT) * 100,
min_top=15
)
if num_of_boxes == 2:
betw_boxes = 90
else:
betw_boxes = 10
specs_for_title = (0, 3, 20, 100)
specs_for_text = (4, text_top, 1, 92)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='middle', width_range=92,
height_range=60, margin=margin, betw_boxes=betw_boxes
)
code_theme = 'tomorrow'
else:
text_top = _top_spec_for_text_at_bottom(
text_block, 40 - (margin / WIDTH) * 100,
per_from_bottom=(margin / HEIGHT) * 100
)
specs_for_title = (0, 3, 20, 40 - (margin / WIDTH) * 100)
specs_for_text = (
(margin / WIDTH) * 100, text_top, 50,
40 - (margin / WIDTH) * 100
)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='rightgroup_v', width_range=60,
margin=margin, betw_boxes=4
)
elif style == 'moods':
specs_for_boxes = []
margin = 18
code_theme = 'tomorrowNight'
# set Headings styles
paragraph_styles['Heading 1'].update(
{'color': '#000016',
'fontFamily': 'Roboto',
'fontSize': 55,
'fontWeight': fontWeight_dict['Black']['fontWeight']}
)
paragraph_styles['Heading 2'] = copy.deepcopy(
paragraph_styles['Heading 1']
)
paragraph_styles['Heading 2'].update({'fontSize': 36})
paragraph_styles['Heading 3'] = copy.deepcopy(
paragraph_styles['Heading 1']
)
paragraph_styles['Heading 3'].update({'fontSize': 30})
# set Body style
paragraph_styles['Body'].update(
{'color': '#000016',
'fontFamily': 'Roboto',
'fontSize': 16,
'fontWeight': fontWeight_dict['Thin']['fontWeight']}
)
bkgd_color = '#FFFFFF'
title_font_color = None
text_font_color = None
if num_of_boxes == 0 and slide_num == 0:
text_textAlign = 'center'
else:
text_textAlign = 'left'
if num_of_boxes == 0:
if slide_num == 0 or text_block == '':
bkgd_color = '#F7F7F7'
specs_for_title = (0, 50, 20, 100)
specs_for_text = (15, 60, 50, 70)
else:
bkgd_color = '#F7F7F7'
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=90,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=20
)
specs_for_title = (0, 2, 20, 100)
specs_for_text = (5, text_top, 50, 90)
elif num_of_boxes == 1:
if code_blocks != []:
# code
if text_block == '':
margin = 5
specs_for_title = (0, 3, 20, 100)
specs_for_text = (0, 0, 0, 0)
top = 12
specs_for_boxes = [
(margin, top, 100 - top - margin, 100 - 2 * margin)
]
elif slide_num % 2 == 0:
# middle center
width_per = 90
height_range = 60
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=width_per,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=100 - height_range / 2.
)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='middle',
width_range=50, height_range=60, margin=margin,
)
specs_for_title = (0, 3, 20, 100)
specs_for_text = (
5, text_top, 2, width_per
)
else:
# right
width_per = 50
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=width_per,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=30
)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='rightgroup_v',
width_range=50, margin=40,
)
specs_for_title = (0, 3, 20, 50)
specs_for_text = (
2, text_top, 2, width_per - 2
)
elif (url_lines != [] and
get_config()['plotly_domain'] in url_lines[0]):
# url
if slide_num % 2 == 0:
# top half
width_per = 95
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=width_per,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=60
)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='middle',
width_range=100, height_range=60,
middle_center=30
)
specs_for_title = (0, 60, 20, 100)
specs_for_text = (
2.5, text_top, 2, width_per
)
else:
# middle across
width_per = 95
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=width_per,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=60
)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='middle',
width_range=100, height_range=60
)
specs_for_title = (0, 3, 20, 100)
specs_for_text = (
2.5, text_top, 2, width_per
)
else:
# image
if slide_num % 2 == 0:
# right
width_per = 50
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=width_per,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=30
)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='rightgroup_v',
width_range=50, margin=0,
)
specs_for_title = (0, 3, 20, 50)
specs_for_text = (
2, text_top, 2, width_per - 2
)
else:
# left
width_per = 50
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=width_per,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=30
)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='leftgroup_v',
width_range=50, margin=0,
)
specs_for_title = (50, 3, 20, 50)
specs_for_text = (
52, text_top, 2, width_per - 2
)
elif num_of_boxes == 2:
# right stack
width_per = 50
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=width_per,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=30
)
specs_for_boxes = [(50, 0, 50, 50), (50, 50, 50, 50)]
specs_for_title = (0, 3, 20, 50)
specs_for_text = (
2, text_top, 2, width_per - 2
)
elif num_of_boxes == 3:
# middle top
width_per = 95
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=width_per,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=40
)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='middle',
width_range=100, height_range=40, middle_center=30
)
specs_for_title = (0, 0, 20, 100)
specs_for_text = (
2.5, text_top, 2, width_per
)
else:
# right stack
width_per = 40
text_top = _top_spec_for_text_at_bottom(
text_block, width_per=width_per,
per_from_bottom=(margin / HEIGHT) * 100,
min_top=30
)
specs_for_boxes = _box_specs_gen(
num_of_boxes, grouptype='rightgroup_v',
width_range=60, margin=0,
)
specs_for_title = (0, 3, 20, 40)
specs_for_text = (
2, text_top, 2, width_per - 2
)
# set text style attributes
title_style_attr = {}
text_style_attr = {'textAlign': text_textAlign}
if text_font_color:
text_style_attr['color'] = text_font_color
if title_font_color:
title_style_attr['color'] = title_font_color
return (specs_for_boxes, specs_for_title, specs_for_text, bkgd_color,
title_style_attr, text_style_attr, code_theme)
def _url_parens_contained(url_name, line):
return line.startswith(url_name + '(') and line.endswith(')')
class Presentation(dict):
"""
The Presentation class for creating spectacle-presentations.
The Presentations API is a means for creating JSON blobs which are then
converted Spectacle Presentations. To use the API you only need to define
a block string and define your slides using markdown. Then you can upload
your presentation to the Plotly Server.
Rules for your presentation string:
- use '---' to denote a slide break.
- headers work as per usual, where if '#' is used before a line of text
then it is interpretted as a header. Only the first header in a slide is
displayed on the slide. There are only 3 heading sizes: #, ## and ###.
4 or more hashes will be interpretted as ###.
- you can set the type of slide transition you want by writing a line that
starts with 'transition: ' before your first header line in the slide,
and write the types of transition you want after. Your transition to
choose from are 'slide', 'zoom', 'fade' and 'spin'.
- to insert a Plotly chart into your slide, write a line that has the form
Plotly(url) with your url pointing to your chart. Note that it is
STRONGLY advised that your chart has fig['layout']['autosize'] = True.
- to insert an image from the web, write a line with the form Image(url)
- to insert a block of text, begin with a line that denotes the code
envoronment '```lang' where lang is a valid programming language. To find
the valid languages run:\n
'plotly.presentation_objs.presentation_objs.VALID_LANGUAGES'\n
To end the code block environment,
write a single '```' line. All Plotly(url) and Image(url) lines will NOT
be interpretted as a Plotly or Image url if they are in the code block.
:param (str) markdown_string: the block string that denotes the slides,
slide properties, and images to be placed in the presentation. If
'markdown_string' is set to 'None', the JSON for a presentation with
one empty slide will be created.
:param (str) style: the theme that the presentation will take on. The
themes that are available now are 'martik' and 'moods'.
Default = 'moods'.
:param (bool) imgStretch: if set to False, all images in the presentation
will not have heights and widths that will not exceed the parent
container they belong to. In other words, images will keep their
original aspect ratios.
Default = True.
For examples see the documentation:\n
https://plot.ly/python/presentations-api/
"""
def __init__(self, markdown_string=None, style='moods', imgStretch=True):
self['presentation'] = {
'slides': [],
'slidePreviews': [None for _ in range(496)],
'version': '0.1.3',
'paragraphStyles': paragraph_styles
}
if markdown_string:
if style not in PRES_THEMES:
raise _plotly_utils.exceptions.PlotlyError(
"Your presentation style must be {}".format(
list_of_options(PRES_THEMES, conj='or', period=True)
)
)
self._markdown_to_presentation(markdown_string, style, imgStretch)
else:
self._add_empty_slide()
def _markdown_to_presentation(self, markdown_string, style, imgStretch):
list_of_slides = _list_of_slides(markdown_string)
for slide_num, slide in enumerate(list_of_slides):
lines_in_slide = slide.split('\n')
title_lines = []
# validate blocks of code
if slide.count('```') % 2 != 0:
raise _plotly_utils.exceptions.PlotlyError(CODE_ENV_ERROR)
# find code blocks
code_indices = []
code_blocks = []
wdw_size = len('```')
for j in range(len(slide)):
if slide[j:j+wdw_size] == '```':
code_indices.append(j)
for k in range(int(len(code_indices) / 2)):
code_blocks.append(
slide[code_indices[2 * k]:code_indices[(2 * k) + 1]]
)
lang_and_code_tuples = []
for code_block in code_blocks:
# validate code blocks
code_by_lines = code_block.split('\n')
language = _remove_extra_whitespace_from_line(
code_by_lines[0][3:]
).lower()
if language == '' or language not in VALID_LANGUAGES:
raise _plotly_utils.exceptions.PlotlyError(
"The language of your code block should be "
"clearly indicated after the first ``` that "
"begins the code block. The valid languages to "
"choose from are" + list_of_options(
VALID_LANGUAGES
)
)
lang_and_code_tuples.append(
(language, '\n'.join(code_by_lines[1:]))
)
# collect text, code and urls
title_lines = []
url_lines = []
text_lines = []
inCode = False
for line in lines_in_slide:
# inCode handling
if line[:3] == '```' and len(line) > 3:
inCode = True
if line == '```':
inCode = False
if not inCode and line != '```':
if len(line) > 0 and line[0] == '#':
title_lines.append(line)
elif (_url_parens_contained('Plotly', line) or
_url_parens_contained('Image', line)):
if (line.startswith('Plotly(') and
get_config()['plotly_domain'] not in line):
raise _plotly_utils.exceptions.PlotlyError(
"You are attempting to insert a Plotly Chart "
"in your slide but your url does not have "
"your plotly domain '{}' in it.".format(
get_config()['plotly_domain']
)
)
url_lines.append(line)
else:
# find and set transition properties
trans = 'transition:'
if line.startswith(trans) and title_lines == []:
slide_trans = line[len(trans):]
slide_trans = _remove_extra_whitespace_from_line(
slide_trans
)
slide_transition_list = []
for key in VALID_TRANSITIONS:
if key in slide_trans:
slide_transition_list.append(key)