Skip to content

Commit db1a9ff

Browse files
committed
more chris comments and change middle 3 slide title 'top' pos
1 parent d38fbf3 commit db1a9ff

File tree

2 files changed

+84
-58
lines changed

2 files changed

+84
-58
lines changed

plotly/presentation_objs/presentation_objs.py

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import warnings
1313

1414
from plotly import exceptions
15+
from plotly.config import get_config
1516

1617
HEIGHT = 700.0
1718
WIDTH = 1000.0
@@ -47,6 +48,7 @@
4748
}
4849

4950

51+
5052
def 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\nx = 2\ny = 1\nprint 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

6591
def _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

308333
def _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\nx = 2\ny = 1\nprint 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(

plotly/tests/test_core/test_spectacle_presentation/test_spectacle_presentation.py

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ def test_invalid_style(self):
1919
# one slide
2020
"""
2121

22-
message = 'Your presentation style must be moods or martik.'
23-
24-
self.assertRaisesRegexp(PlotlyError, message, pres.Presentation,
25-
markdown_string, style='foo')
22+
self.assertRaisesRegexp(
23+
PlotlyError, pres.presentation_objs.STYLE_ERROR,
24+
pres.Presentation, markdown_string, style='foo'
25+
)
2626

2727
def test_open_code_block(self):
2828
markdown_string = """
@@ -33,20 +33,11 @@ def test_open_code_block(self):
3333
print x
3434
"""
3535

36-
message = (
37-
"If you are putting a block of code into your markdown "
38-
"presentation, make sure your denote the start and end "
39-
"of the code environment with the '```' characters. For "
40-
"example, your markdown string would include something "
41-
"like:\n\n```python\nx = 2\ny = 1\nprint x\n```\n\n"
42-
"Notice how the language that you want the code to be "
43-
"displayed in is immediately to the right of first "
44-
"entering '```', i.e. '```python'."
36+
self.assertRaisesRegexp(
37+
PlotlyError, pres.presentation_objs.CODE_ENV_ERROR,
38+
pres.Presentation, markdown_string, style='moods'
4539
)
4640

47-
self.assertRaisesRegexp(PlotlyError, message, pres.Presentation,
48-
markdown_string, style='moods')
49-
5041
def test_invalid_code_language(self):
5142
markdown_string = """
5243
```foo
@@ -55,22 +46,26 @@ def test_invalid_code_language(self):
5546
```
5647
"""
5748

58-
message = (
59-
"The language of your code block should be "
60-
"clearly indicated after the first ``` that "
61-
"begins the code block. The valid languages to "
62-
"choose from are" + pres.presentation_objs.list_of_options(
63-
pres.presentation_objs.VALID_LANGUAGES
64-
)
49+
self.assertRaisesRegexp(
50+
PlotlyError, pres.presentation_objs.LANG_ERROR, pres.Presentation,
51+
markdown_string, style='moods'
6552
)
6653

67-
self.assertRaisesRegexp(PlotlyError, message, pres.Presentation,
68-
markdown_string, style='moods')
6954

7055
def test_expected_pres(self):
71-
markdown_string_empty = "\n#title\n"
56+
markdown_string = """
57+
# title
58+
---
59+
# Colors
60+
Colors are everywhere around us.
61+
Plotly(https://plot.ly/~AdamKulidjian/3564/)
62+
Image(https://raw.githubusercontent.com/jackparmer/gradient-backgrounds/master/moods1.png)
63+
```python
64+
x=1
65+
```
66+
"""
7267

73-
my_pres = pres.Presentation(markdown_string_empty, style='moods')
68+
my_pres = pres.Presentation(markdown_string, style='moods')
7469

7570
exp_pres = {
7671
'presentation': {'paragraphStyles': {

0 commit comments

Comments
 (0)