1212import warnings
1313
1414from plotly import exceptions
15+ from plotly .config import get_config
1516
1617HEIGHT = 700.0
1718WIDTH = 1000.0
4748}
4849
4950
51+
5052def list_of_options (iterable , conj = 'and' , period = True ):
5153 """
5254 Returns an English listing of objects seperated by commas ','
@@ -61,6 +63,30 @@ def list_of_options(iterable, conj='and', period=True):
6163 template = (len (iterable ) - 2 )* '{}, ' + '{} ' + conj + ' {}' + period * '.'
6264 return template .format (* iterable )
6365
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+ )
6490
6591def _generate_id (size ):
6692 letters_and_numbers = string .ascii_letters
@@ -287,9 +313,8 @@ def _list_of_slides(markdown_string):
287313 if not markdown_string .endswith ('\n ---\n ' ):
288314 markdown_string += '\n ---\n '
289315
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+
293318 list_of_slides = []
294319 for text in text_blocks :
295320 if not all (char in ['\n ' , '-' , ' ' ] for char in text ):
@@ -307,6 +332,16 @@ def _list_of_slides(markdown_string):
307332
308333def _top_spec_for_text_at_bottom (text_block , width_per , per_from_bottom = 0 ,
309334 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+
310345 # TODO: customize this function for different fonts/sizes
311346 max_lines = 37
312347 one_char_percent_width = 0.764
@@ -495,7 +530,8 @@ def _return_layout_specs(num_of_boxes, url_lines, title_lines, text_block,
495530 text_font_color = '#F4FAFB'
496531 elif num_of_boxes == 1 :
497532 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 ]):
499535 if code_blocks != []:
500536 w_range = 40
501537 else :
@@ -675,7 +711,8 @@ def _return_layout_specs(num_of_boxes, url_lines, title_lines, text_block,
675711 specs_for_text = (
676712 2 , text_top , 2 , width_per - 2
677713 )
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 ]):
679716 # url
680717 if slide_num % 2 == 0 :
681718 # top half
@@ -769,7 +806,7 @@ def _return_layout_specs(num_of_boxes, url_lines, title_lines, text_block,
769806 num_of_boxes , grouptype = 'middle' ,
770807 width_range = 100 , height_range = 40 , middle_center = 30
771808 )
772- specs_for_title = (0 , 3 , 20 , 100 )
809+ specs_for_title = (0 , 0 , 20 , 100 )
773810 specs_for_text = (
774811 2.5 , text_top , 2 , width_per
775812 )
@@ -882,16 +919,7 @@ def _markdown_to_presentation(self, markdown_string, style, imgStretch):
882919
883920 # validate blocks of code
884921 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 )
895923
896924 # find code blocks
897925 code_indices = []
@@ -944,11 +972,14 @@ def _markdown_to_presentation(self, markdown_string, style, imgStretch):
944972 title_lines .append (line )
945973 elif (_url_parens_contained ('Plotly' , line ) or
946974 _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 ):
948977 raise exceptions .PlotlyError (
949978 "You are attempting to insert a Plotly Chart "
950979 "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+ )
952983 )
953984 url_lines .append (line )
954985 else :
@@ -1042,7 +1073,7 @@ def _markdown_to_presentation(self, markdown_string, style, imgStretch):
10421073 slide = slide_num , props_attr = props_attr )
10431074 else :
10441075 # url
1045- if 'https://plot.ly' in url_or_code :
1076+ if get_config ()[ 'plotly_domain' ] in url_or_code :
10461077 box_name = 'Plotly'
10471078 else :
10481079 box_name = 'Image'
@@ -1053,18 +1084,18 @@ def _markdown_to_presentation(self, markdown_string, style, imgStretch):
10531084 height = specs [2 ], width = specs [3 ],
10541085 slide = slide_num )
10551086
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' )
10681099
10691100 def _add_empty_slide (self ):
10701101 self ['presentation' ]['slides' ].append (
0 commit comments