Skip to content

Commit 7b0e5d1

Browse files
committed
improve 'grid_str' code
1 parent 95cded8 commit 7b0e5d1

File tree

1 file changed

+76
-12
lines changed

1 file changed

+76
-12
lines changed

plotly/tools.py

+76-12
Original file line numberDiff line numberDiff line change
@@ -997,19 +997,83 @@ def _add_domain_is_3d(layout, s_label, x_domain, y_domain):
997997

998998
insets_ref[i_inset] = (x_label, y_label) # fill in ref
999999

1000+
# [grid_str] Set the grid's string representation
1001+
sp = " " # space between cell
1002+
s_str = "[ " # cell start string
1003+
e_str = " ]" # cell end string
1004+
colspan_str = ' -' # colspan string
1005+
rowspan_str = ' |' # rowspan string
1006+
empty_str = ' (empty) ' # empty cell string
1007+
1008+
# Init grid_str with intro message
1009+
grid_str = "This is the format of your plot grid!\n"
1010+
1011+
# Init tmp list of lists of strings (sorta like 'grid_ref' but w/ strings)
1012+
_tmp = [['' for c in range(cols)] for r in range(rows)]
1013+
1014+
# Define cell string as function of (r, c) and grid_ref
1015+
def _get_cell_str(r, c, ref):
1016+
return '({r},{c}) {ref}'.format(r=r+1, c=c+1, ref=','.join(ref))
1017+
1018+
# Find max len of _cell_str, add define a padding function
1019+
cell_len = max([len(_get_cell_str(r, c, ref))
1020+
for r, row_ref in enumerate(grid_ref)
1021+
for c, ref in enumerate(row_ref)
1022+
if ref]) + len(s_str) + len(e_str)
1023+
1024+
def _pad(s, cell_len=cell_len):
1025+
return ' ' * (cell_len - len(s))
1026+
1027+
# Loop through specs, fill in _tmp
1028+
for r, spec_row in enumerate(specs):
1029+
for c, spec in enumerate(spec_row):
1030+
1031+
ref = grid_ref[r][c]
1032+
if ref is None:
1033+
if _tmp[r][c] == '':
1034+
_tmp[r][c] = empty_str + _pad(empty_str)
1035+
continue
1036+
1037+
cell_str = s_str + _get_cell_str(r, c, ref)
1038+
1039+
if spec['colspan'] > 1:
1040+
for cc in range(1, spec['colspan']-1):
1041+
_tmp[r][c+cc] = colspan_str + _pad(colspan_str)
1042+
_tmp[r][c+spec['colspan']-1] = (
1043+
colspan_str + _pad(colspan_str + e_str)) + e_str
1044+
else:
1045+
cell_str += e_str
1046+
1047+
if spec['rowspan'] > 1:
1048+
for rr in range(1, spec['rowspan']-1):
1049+
_tmp[r+rr][c] = rowspan_str + _pad(rowspan_str)
1050+
for cc in range(spec['colspan']):
1051+
_tmp[r+spec['rowspan']-1][c+cc] = (
1052+
rowspan_str + _pad(rowspan_str))
1053+
1054+
_tmp[r][c] = cell_str + _pad(cell_str)
1055+
1056+
# Append grid_str using data from _tmp in the correct order
1057+
for r in row_seq[::-1]:
1058+
grid_str += sp.join(_tmp[r]) + '\n'
1059+
1060+
# Append grid_str to include insets info
1061+
if insets:
1062+
grid_str += "\nWith insets:\n"
1063+
for i_inset, inset in enumerate(insets):
1064+
1065+
r = inset['cell'][0] - 1
1066+
c = inset['cell'][1] - 1
1067+
ref = grid_ref[r][c]
1068+
1069+
grid_str += (
1070+
s_str + ','.join(insets_ref[i_inset]) + e_str +
1071+
' over ' +
1072+
s_str + _get_cell_str(r, c, ref) + e_str + '\n'
1073+
)
1074+
10001075
if print_grid:
1001-
print("This is the format of your plot grid!")
1002-
grid_string = ""
1003-
for grid_str_row in grid_str:
1004-
grid_string = " ".join(grid_str_row) + '\n' + grid_string
1005-
print(grid_string)
1006-
if insets:
1007-
print("With insets:")
1008-
for i_inset, inset in enumerate(insets):
1009-
print(
1010-
insets_str[i_inset] + ' over ' +
1011-
grid_str[inset['cell'][0]][inset['cell'][1]])
1012-
print('')
1076+
print(grid_str)
10131077

10141078
fig = graph_objs.Figure(layout=layout)
10151079
return fig

0 commit comments

Comments
 (0)