【问题标题】:Dash Plotly Textbox callback interactionDash Plotly Textbox 回调交互
【发布时间】: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


【解决方案1】:

注释中的格式有点奇怪,所以这里是代码:

首先,您需要在最顶部定义一个名为纬度和经度的空列表

longitude = []
latitude = []

从那里,使用您提供的回调,您可以使用更新此列表

    @app.callback(
    Output("output", "children"),
    Input("input1", "value"),
    Input("input2", "value"),
    Input("input3", "value"),
    Input("input4", "value"),
)
def update_output(input1,input2,input3,input4):
        latitude.extend([input1,input3])
        longitude.extend([input2,input4])
        return 'Lat1: {} and Long1: {}\nLat2: {} and Long2 {}'.format(input1, input2, input3, input4)

然后像这样使用这些值:

figt = go.Figure(go.Scattermapbox(
    fill = "toself",
    lon = longitude, 
    lat = latitude,
    marker = { 'size': 10, 'color': "orange" }))

figt.update_layout(
    mapbox = {
        'style': "stamen-terrain",
        'center': {'lon': -73, 'lat': 46 },
        'zoom': 5},
    showlegend = False)

您可以使用link在点/轨迹之间填充

【讨论】:

  • 感谢您的代码!想问#placeholder for the inputs l1 = 20 l2 = 30 l3 = 40 l4 = 50是什么@
  • 因为我没有您要使用的输入,所以我只是创建了占位符值,您可以忽略它,并改用您的输入
  • 哦,我的输入值是文本框中的值,这意味着如果我要在 Lat1 文本框中键入示例“103.23”,那么 l1 将 = 到“103.23”。所以就像我在 Lat1 文本框中输入的任何数字一样,数字 = I1。所以问题是我不知道如何将输入数字链接到 figt 的经度和纬度。
  • 我更新了代码,希望它更有意义
  • 我也试过了,但它给了我这个错误Unresolved reference 'input1'
猜你喜欢
  • 2021-02-25
  • 2020-03-07
  • 2020-03-17
  • 1970-01-01
  • 2021-05-22
  • 2021-09-10
  • 2020-08-12
  • 1970-01-01
  • 2020-02-15
相关资源
最近更新 更多