有没有办法将新输入添加到 python 破折号回调

Is there a way to add a new input to a python dash callback

我正在编写一个带有 Divs 的 dash 应用程序,其中包含图像。当向 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 移动到第二个,但单击添加到第一个 [=] 的图像27=] 回调不执行任何操作,因为启动脚本时其 n_clicks 输入不存在于 callback_children 中。有没有办法更新回调输入,为新图像添加 n_clicks?

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