简体   繁体   English

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

[英]Is there a way to add a new input to a python dash callback

I am writing a dash app with Divs with images in them.我正在编写一个带有 Divs 的破折号应用程序,其中包含图像。 I want to generate a new image when a new line to a SQLite database is added.当添加到 SQLite 数据库的新行时,我想生成一个新图像。 In addition, I want clicking on this new image to move it to a different Div.此外,我想单击这个新图像以将其移动到不同的 Div。 The problem I'm having is that although I can add Input([new image id],n_clicks) to the list of inputs, outputs and states that I passed to the callback, it is not listening to this new input.我遇到的问题是,虽然我可以将 Input([new image id],n_clicks) 添加到我传递给回调的输入、输出和状态列表中,但它并没有监听这个新输入。 Simplified pieces of code are:简化的代码片段是:

For generating the the image:用于生成图像:

 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)

and for adding the new image to the div, moving image to next div when clicked and for attempting to add n_clicks for the new image to the callback inputs并将新图像添加到 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]


This code successfully adds the image to the first div and moves an image from the first div to the second when clicked, but clicking on an image that was added to the first div by the callback does nothing since its n_clicks input wasn't present in callback_children when the script was launched.此代码成功地将图像添加到第一个 div 并在单击时将图像从第一个 div 移动到第二个,但是单击回调添加到第一个 div 的图像什么也不做,因为它的 n_clicks 输入不存在脚本启动时的 callback_children。 Is there a way to update the callback inputs to add n_clicks for the new image?有没有办法更新回调输入以为新图像添加 n_clicks?

You'll need to use pattern-matching callbacks for this.您需要为此使用模式匹配回调 Updating a Dash callback after initialization won't change its behavior, but these callbacks can use somewhat dynamic inputs and outputs, which should do the trick.在初始化后更新 Dash 回调不会改变它的行为,但是这些回调可以使用一些动态的输入和输出,这应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM