【问题标题】:Is there a way to add a new input to a python dash callback有没有办法向 python 破折号回调添加新输入
【发布时间】:2021-05-05 20:24:20
【问题描述】:

我正在编写一个带有 Div 的仪表板应用程序,其中包含图像。当向 SQLite 数据库添加新行时,我想生成一个新图像。此外,我想单击这个新图像以将其移动到不同的 Div。我遇到的问题是,虽然我可以将 Input([new image id],n_clicks) 添加到我传递给回调的输入、输出和状态列表中,但它并没有监听这个新输入。简化的代码如下:

用于生成图像:

 def text(sql_row,id_name):
    #font_size=16
    color='blue'
    image = Image.new("RGB", (400, 500), color)
    draw = ImageDraw.Draw(image)
    row=10
    for row, item in enumerate(sql_row):
        draw.text((10,35, item)
    return html.Img(src=image,id=id_name)

以及将新图像添加到 div、单击时将图像移动到下一个 div 以及尝试将新图像的 n_clicks 添加到回调输入


#create the callbacks for the function
callback_children=[Output('div1','children'),Output('div2','children')]
for img in image_list:
    callback_children.append(Input(img.id,'n_clicks'))
callback_children=callback_children+[Input('interval-component', 'n_intervals')]+[State(component_id='order_in',component_property='children'),State(component_id='order_up',component_property='children'),State(component_id='order_out',component_property='children')] 





@app.callback(callback_children)#children contain n_clicks input for images in divs, div children states and div children outputs
def update_orders(*args):

...
    #add new image to children of div1:
    global callback_children
    new_img=text(new_item,str(new_sql_row))
    div1_children.append(new_img)#This puts the new image in div1
    callback_children.insert(3,Input(new_img.id,'n_clicks'))

    #move image to next div when clicked
    changed_id = [p['prop_id'] for p in ctx.triggered][0]
    ctx = dash.callback_context
    for item,click_value in list(ctx.inputs.items()):
            img_id=str(item).split('.')[0]
            if img_id in changed_id.split('.')[0]:#if image was clicked
                div2_children.append(img_id)#add image to div2 children
                ...
                #code to remove image from div1 children

                ...

    return [div1_children, div2_children]


此代码成功地将图像添加到第一个 div 并在单击时将图像从第一个 div 移动到第二个,但是单击回调添加到第一个 div 的图像没有任何作用,因为它的 n_clicks 输入不是t 脚本启动时出现在 callback_children 中。有没有办法更新回调输入以为新图像添加 n_clicks?

【问题讨论】:

    标签: python callback plotly-dash


    【解决方案1】:

    您需要为此使用pattern-matching callbacks。在初始化后更新 Dash 回调不会改变其行为,但这些回调可以使用一些动态的输入和输出,这应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多