12
12
import warnings
13
13
14
14
from plotly import exceptions
15
+ from plotly .config import get_config
15
16
16
17
HEIGHT = 700.0
17
18
WIDTH = 1000.0
47
48
}
48
49
49
50
51
+
50
52
def list_of_options (iterable , conj = 'and' , period = True ):
51
53
"""
52
54
Returns an English listing of objects seperated by commas ','
@@ -61,6 +63,30 @@ def list_of_options(iterable, conj='and', period=True):
61
63
template = (len (iterable ) - 2 )* '{}, ' + '{} ' + conj + ' {}' + period * '.'
62
64
return template .format (* iterable )
63
65
66
+ # Error Messages
67
+ STYLE_ERROR = "Your presentation style must be {}" .format (
68
+ list_of_options (PRES_THEMES , conj = 'or' , period = True )
69
+ )
70
+
71
+ CODE_ENV_ERROR = (
72
+ "If you are putting a block of code into your markdown "
73
+ "presentation, make sure your denote the start and end "
74
+ "of the code environment with the '```' characters. For "
75
+ "example, your markdown string would include something "
76
+ "like:\n \n ```python\n x = 2\n y = 1\n print x\n ```\n \n "
77
+ "Notice how the language that you want the code to be "
78
+ "displayed in is immediately to the right of first "
79
+ "entering '```', i.e. '```python'."
80
+ )
81
+
82
+ LANG_ERROR = (
83
+ "The language of your code block should be "
84
+ "clearly indicated after the first ``` that "
85
+ "begins the code block. The valid languages to "
86
+ "choose from are" + list_of_options (
87
+ VALID_LANGUAGES
88
+ )
89
+ )
64
90
65
91
def _generate_id (size ):
66
92
letters_and_numbers = string .ascii_letters
@@ -287,9 +313,8 @@ def _list_of_slides(markdown_string):
287
313
if not markdown_string .endswith ('\n ---\n ' ):
288
314
markdown_string += '\n ---\n '
289
315
290
- text_blocks = re .split (
291
- '\n --\n |\n ---\n |\n ----\n |\n -----\n |\n ------\n ' , markdown_string
292
- )
316
+ text_blocks = re .split ('\n -{2,}\n ' , markdown_string )
317
+
293
318
list_of_slides = []
294
319
for text in text_blocks :
295
320
if not all (char in ['\n ' , '-' , ' ' ] for char in text ):
@@ -307,6 +332,16 @@ def _list_of_slides(markdown_string):
307
332
308
333
def _top_spec_for_text_at_bottom (text_block , width_per , per_from_bottom = 0 ,
309
334
min_top = 30 ):
335
+ # This function ensures that if there is a large block of
336
+ # text in your slide it will not overflow off the bottom
337
+ # of the slide.
338
+ # The input for this function are a block of text and the
339
+ # params that define where it will be placed in the slide.
340
+ # The function makes some calculations and will output a
341
+ # 'top' value (i.e. the left, top, height, width css params)
342
+ # so that the text block will come down to some specified
343
+ # distance from the bottom of the page.
344
+
310
345
# TODO: customize this function for different fonts/sizes
311
346
max_lines = 37
312
347
one_char_percent_width = 0.764
@@ -495,7 +530,8 @@ def _return_layout_specs(num_of_boxes, url_lines, title_lines, text_block,
495
530
text_font_color = '#F4FAFB'
496
531
elif num_of_boxes == 1 :
497
532
if code_blocks != [] or (url_lines != [] and
498
- 'https://plot.ly' in url_lines [0 ]):
533
+ get_config ()['plotly_domain' ] in
534
+ url_lines [0 ]):
499
535
if code_blocks != []:
500
536
w_range = 40
501
537
else :
@@ -675,7 +711,8 @@ def _return_layout_specs(num_of_boxes, url_lines, title_lines, text_block,
675
711
specs_for_text = (
676
712
2 , text_top , 2 , width_per - 2
677
713
)
678
- elif url_lines != [] and 'https://plot.ly' in url_lines [0 ]:
714
+ elif (url_lines != [] and
715
+ get_config ()['plotly_domain' ] in url_lines [0 ]):
679
716
# url
680
717
if slide_num % 2 == 0 :
681
718
# top half
@@ -769,7 +806,7 @@ def _return_layout_specs(num_of_boxes, url_lines, title_lines, text_block,
769
806
num_of_boxes , grouptype = 'middle' ,
770
807
width_range = 100 , height_range = 40 , middle_center = 30
771
808
)
772
- specs_for_title = (0 , 3 , 20 , 100 )
809
+ specs_for_title = (0 , 0 , 20 , 100 )
773
810
specs_for_text = (
774
811
2.5 , text_top , 2 , width_per
775
812
)
@@ -882,16 +919,7 @@ def _markdown_to_presentation(self, markdown_string, style, imgStretch):
882
919
883
920
# validate blocks of code
884
921
if slide .count ('```' ) % 2 != 0 :
885
- raise exceptions .PlotlyError (
886
- "If you are putting a block of code into your markdown "
887
- "presentation, make sure your denote the start and end "
888
- "of the code environment with the '```' characters. For "
889
- "example, your markdown string would include something "
890
- "like:\n \n ```python\n x = 2\n y = 1\n print x\n ```\n \n "
891
- "Notice how the language that you want the code to be "
892
- "displayed in is immediately to the right of first "
893
- "entering '```', i.e. '```python'."
894
- )
922
+ raise exceptions .PlotlyError (CODE_ENV_ERROR )
895
923
896
924
# find code blocks
897
925
code_indices = []
@@ -944,11 +972,14 @@ def _markdown_to_presentation(self, markdown_string, style, imgStretch):
944
972
title_lines .append (line )
945
973
elif (_url_parens_contained ('Plotly' , line ) or
946
974
_url_parens_contained ('Image' , line )):
947
- if line .startswith ('Plotly(' ) and 'plot.ly' not in line :
975
+ if (line .startswith ('Plotly(' ) and
976
+ get_config ()['plotly_domain' ] not in line ):
948
977
raise exceptions .PlotlyError (
949
978
"You are attempting to insert a Plotly Chart "
950
979
"in your slide but your url does not have "
951
- "plot.ly in it."
980
+ "your plotly ___domain '{}' in it." .format (
981
+ get_config ()['plotly_domain' ]
982
+ )
952
983
)
953
984
url_lines .append (line )
954
985
else :
@@ -1042,7 +1073,7 @@ def _markdown_to_presentation(self, markdown_string, style, imgStretch):
1042
1073
slide = slide_num , props_attr = props_attr )
1043
1074
else :
1044
1075
# url
1045
- if 'https://plot.ly' in url_or_code :
1076
+ if get_config ()[ 'plotly_domain' ] in url_or_code :
1046
1077
box_name = 'Plotly'
1047
1078
else :
1048
1079
box_name = 'Image'
@@ -1053,18 +1084,18 @@ def _markdown_to_presentation(self, markdown_string, style, imgStretch):
1053
1084
height = specs [2 ], width = specs [3 ],
1054
1085
slide = slide_num )
1055
1086
1056
- # if imgStretch:
1057
- # for slide in self['presentation']['slides']:
1058
- # for child in slide['children']:
1059
- # if child['type'] in ['Image', 'Plotly']:
1060
- # deep_child = child['props']['style']
1061
- # width = deep_child['width']
1062
- # height = deep_child['height']
1063
- #
1064
- # if width >= height:
1065
- # deep_child['max-width'] = deep_child.pop('width')
1066
- # else:
1067
- # deep_child['max-height'] = deep_child.pop('height')
1087
+ if imgStretch :
1088
+ for slide in self ['presentation' ]['slides' ]:
1089
+ for child in slide ['children' ]:
1090
+ if child ['type' ] in ['Image' , 'Plotly' ]:
1091
+ deep_child = child ['props' ]['style' ]
1092
+ width = deep_child ['width' ]
1093
+ height = deep_child ['height' ]
1094
+
1095
+ if width >= height :
1096
+ deep_child ['max-width' ] = deep_child .pop ('width' )
1097
+ else :
1098
+ deep_child ['max-height' ] = deep_child .pop ('height' )
1068
1099
1069
1100
def _add_empty_slide (self ):
1070
1101
self ['presentation' ]['slides' ].append (
0 commit comments