【发布时间】:2021-06-07 11:41:28
【问题描述】:
我被指派与地图上的填充区域进行文本框交互。这是我的仪表板当前的样子:
所以一旦我在文本框中输入我的数字并按下提交,图形地图(位于左侧)应该根据我在 Lat1、Long1 和 Lat2、Long2 中输入的数字显示填充区域。但是,我不太确定如何在数字输入、数字以及按钮之间进行回调。我上网搜索,但我找不到任何可以阅读的资源。我对此非常陌生,因此非常感谢任何帮助!因此,如果您有任何想法,请告诉我!谢谢你:)
这是我的输入框代码:
def custom_input1(paragraph_text, min_value=0, max_value=200, step=1):
return html.Div(
children=[
html.P(paragraph_text, style={"paddingRight": 10}),
dbc.Input(type="float", min=min_value, max=max_value, step=step,id='input1'),
],
style={"display": "flex"},
)
def custom_input2(paragraph_text, min_value=0, max_value=200, step=1):
return html.Div(
children=[
html.P(paragraph_text, style={"paddingRight": 10}),
dbc.Input(type="float", min=min_value, max=max_value, step=step,id='input2'),
],
style={"display": "flex"},
)
def custom_input3(paragraph_text, min_value=0, max_value=200, step=1):
return html.Div(
children=[
html.P(paragraph_text, style={"paddingRight": 10}),
dbc.Input(type="float", min=min_value, max=max_value, step=step,id='input3'),
],
style={"display": "flex"},
)
def custom_input4(paragraph_text, min_value=0, max_value=200, step=1):
return html.Div(
children=[
html.P(paragraph_text, style={"paddingRight": 10}),
dbc.Input(type="float", min=min_value, max=max_value, step=step,id='input4'),
],
style={"display": "flex"},
)
html.Div(children=[html.Div([
html.H1("Query1", style={'text-align': 'center','fontSize': 30}),
dbc.Row(
[
dbc.Col(dcc.Graph(id="graphQ", figure={}), md=8),
dbc.Col(
children=[
dbc.Row(
[
dbc.Col(custom_input1("Lat1")),
dbc.Col(custom_input2("Long1")),
],
style={"paddingBottom": 30},
),
dbc.Row(
[
dbc.Col(custom_input3("Lat2")),
dbc.Col(custom_input4("Lat2")),
],
style={"paddingBottom": 30},
),
html.Div(id="output"),
html.Button('Submit', id='submit_button', n_clicks=0),
],
md=4,
),
]
),
])]))
@app.callback(
Output("output", "children"),
Input("input1", "value"),
Input("input2", "value"),
Input("input3", "value"),
Input("input4", "value"),
)
def update_output(input1,input2,input3,input4):
return 'Lat1: {} and Long1: {}\nLat2: {} and Long2: {}'.format(input1, input2, input3, input4)
这是我的失败代码:(不太确定这是否是正确的方法哈哈……)
figt = go.Figure(go.Scattermapbox(
fill = "toself",
lon = [{}.format(input2,input4)], lat = [{}.format(input1,input3)],
marker = { 'size': 10, 'color': "orange" }))
figt.update_layout(
mapbox = {
'style': "stamen-terrain",
'center': {'lon': -73, 'lat': 46 },
'zoom': 5},
showlegend = False)
【问题讨论】:
-
运行代码的结果是什么?
-
对于输入框编码,它只显示文本框、按钮和图形。真的没有任何结果。对于图形编码,有一个错误
lon = [{}.format(input2,input4)], lat = [{}.format(input1,input3)], AttributeError: 'dict' object has no attribute 'format'我确定 {}.format(x,x) 不是正确的方法,但我不知道执行此类功能的代码是什么 -
您可以尝试避免该问题的一种方法是尝试将 lon 和 lat 定义为 fig 之外的空列表,使用输入附加到该列表,然后使用原始列表名称和设置它等于 lon 和 lat。
-
嗯,你介意给我看一下代码吗?
-
我的意思是您可以拥有一个函数并多次重复使用该函数,请参阅 this pastebin 示例代码,以显示该想法。
标签: python plotly-dash plotly-python