Python 循环创建两列布局
Python loop to create a two column layout
我正在尝试在 Python 和 Dash-Bootstrap-Component 中创建一个两列循环。这个问题是 Python 相关的,我不太明白如何实现这个。
我正在遍历一个值列表。布局应该是多行,每行有两列。
(简洁代码)
figs=[]
figs.append(dict(data=data, layout=layout)) # dash
body = dbc.Container(
[
dbc.Row(
[
dbc.Col(
[
html.H4('ES'+str(i)),
dcc.Graph(figure=figs[i])
],
md=6
),
dbc.Col(
[
html.H4('ES'+str(i+1)),
dcc.Graph(figure=figs[i+1]) # <- how to increment i here? This syntax 'figs[i+1]' throws an error.
]
)
]
)
for i, value in enumerate(figs)
]
)
我需要在第一列中显示一个图表 figs[i]
,然后递增索引以在第二列中显示下一个图表。 figs[i+1]
不起作用,我不确定如何在此代码中嵌套 for 循环或执行 while 循环。我附上了一张图片,显示当对两列使用相同的 figs[i]
时代码有效。
更新:感谢 erkandem 在下面的回答,我得出了一个结论,发布在这里:
figs.append(dict(data=data, layout=layout))
body_py = [0] * len(figs)
for i, value in enumerate(figs):
left = i
right = i + 1 if i+1 < len(figs) else 0
body_py[left] = figs[left]
body_py[right] = figs[right]
body = dbc.Container(
[
dbc.Row(
[
dbc.Col(
[
html.H4('ES '+str(i)),
dcc.Graph(figure=body_py[i])
],
md=6,
)
for i, value in enumerate(body_py)
]
)
]
)
app.layout = html.Div([body])
核心问题归结为 (i + i + 1)
而不是 i + 1
(真的吗?)
figs = [0, 1, 2, 3, 4, 5, 6, 7]
body = [0] * int((len(figs) / 2))
if len(figs) % 2 is not 0:
print('appebnd a dummy figure')
for i in range(len(body)):
# foreplay
row = str(i)
left = i + i
right = (i + i + 1)
print([row, left, right])
# action
body[i] = {row: [dict(h4=str(left), graph=figs[left]), dict(h4=str(right), graph=figs[right])]}
for row in body:
print(row)
打印:
{'0': [{'h4': '0', 'graph': 0}, {'h4': '1', 'graph': 1}]}
{'1': [{'h4': '2', 'graph': 2}, {'h4': '3', 'graph': 3}]}
{'2': [{'h4': '4', 'graph': 4}, {'h4': '5', 'graph': 5}]}
{'3': [{'h4': '6', 'graph': 6}, {'h4': '7', 'graph': 7}]}
我正在尝试在 Python 和 Dash-Bootstrap-Component 中创建一个两列循环。这个问题是 Python 相关的,我不太明白如何实现这个。 我正在遍历一个值列表。布局应该是多行,每行有两列。
(简洁代码)
figs=[]
figs.append(dict(data=data, layout=layout)) # dash
body = dbc.Container(
[
dbc.Row(
[
dbc.Col(
[
html.H4('ES'+str(i)),
dcc.Graph(figure=figs[i])
],
md=6
),
dbc.Col(
[
html.H4('ES'+str(i+1)),
dcc.Graph(figure=figs[i+1]) # <- how to increment i here? This syntax 'figs[i+1]' throws an error.
]
)
]
)
for i, value in enumerate(figs)
]
)
我需要在第一列中显示一个图表 figs[i]
,然后递增索引以在第二列中显示下一个图表。 figs[i+1]
不起作用,我不确定如何在此代码中嵌套 for 循环或执行 while 循环。我附上了一张图片,显示当对两列使用相同的 figs[i]
时代码有效。
更新:感谢 erkandem 在下面的回答,我得出了一个结论,发布在这里:
figs.append(dict(data=data, layout=layout))
body_py = [0] * len(figs)
for i, value in enumerate(figs):
left = i
right = i + 1 if i+1 < len(figs) else 0
body_py[left] = figs[left]
body_py[right] = figs[right]
body = dbc.Container(
[
dbc.Row(
[
dbc.Col(
[
html.H4('ES '+str(i)),
dcc.Graph(figure=body_py[i])
],
md=6,
)
for i, value in enumerate(body_py)
]
)
]
)
app.layout = html.Div([body])
核心问题归结为 (i + i + 1)
而不是 i + 1
(真的吗?)
figs = [0, 1, 2, 3, 4, 5, 6, 7]
body = [0] * int((len(figs) / 2))
if len(figs) % 2 is not 0:
print('appebnd a dummy figure')
for i in range(len(body)):
# foreplay
row = str(i)
left = i + i
right = (i + i + 1)
print([row, left, right])
# action
body[i] = {row: [dict(h4=str(left), graph=figs[left]), dict(h4=str(right), graph=figs[right])]}
for row in body:
print(row)
打印:
{'0': [{'h4': '0', 'graph': 0}, {'h4': '1', 'graph': 1}]}
{'1': [{'h4': '2', 'graph': 2}, {'h4': '3', 'graph': 3}]}
{'2': [{'h4': '4', 'graph': 4}, {'h4': '5', 'graph': 5}]}
{'3': [{'h4': '6', 'graph': 6}, {'h4': '7', 'graph': 7}]}