Dash Cytoscape – how to access component elements inside a callback function

I am a relative newbie at Plotly Dash Python programming. I am trying o get to grips with the methodology of using the Dash Cytoscape component. What I want to do is to be able to [1] Define an initial zero node or null network, then [2] generate the network (say) from a dataframe within a callback function. Very few of the example codes I have found show how to update components within a callback. I did find the example called usage-reset-button.py here:

https://github.com/plotly/dash-cytoscape/blob/main/demos/usage-reset-button.py

What I have tried to do with this is is to modify the example code so that I declare the Cytoscape component in the layout, with no elements, but add elements to that dynamically in the callback. If I use static coordinates it seems to work fine. If I generate the coordinates dynamically using random numbers, then I expect the two node network to move around, but it does not do that. I even print() out the text of ‘myelements’ to the console and this shows changing node coordinates but the network I see is always fixed. I am finding Dash programming pretty tough, mainly because I do not see easy ways to access the components in the callbacks. Knowing what the valid component properties are and finding examples of these is a tough search, so many quite simple examples and so few of what I want and need to do. My example code is below. If someone can suggest why the network seen on the web page never changes, that would be a step forward. Mark

—————————–

I tried to understand ways of using Dash by modifying the example code to the code below:

see web page

https://github.com/plotly/dash-cytoscape/blob/main/demos/usage-reset-button.py

“””
An example to show how to reset the position, zoom level, and layout of a Cytoscape graph, using a
button attached to a callback.
“””
import dash
from dash import Input, Output, html, callback, State
import dash_cytoscape as cyto
import random

cytoelements = []
#    {"data": {"id": "one", "label": "Node 1"}, "position": {"x": 50, "y": 50}},
#    {"data": {"id": "two", "label": "Node 2"}, "position": {"x": 200, "y": 200}},
#    {"data": {"source": "one", "target": "two", "label": "Node 1 to 2"}},

layout = {"name": "grid"}

app = dash.Dash(__name__)
app.layout = html.Div(
    [
        html.Button("Reset", id="bt-reset"),
        cyto.Cytoscape(id="cytoscape", elements=cytoelements, layout=layout, zoom=1),
    ]
)


@callback(
    Output("cytoscape", "zoom"),
    Output("cytoscape", "elements"),
    Input("bt-reset", "n_clicks"),
    # State("cytoscape","elements"),
    # prevent_initial_call=True
)
# def reset_layout(n_clicks,elements):
def reset_layout(n_clicks):
    print(n_clicks, "click")
    x1=random.randrange(1,200)
    y1=random.randrange(1,200)
    x2=random.randrange(1,200)
    y2=random.randrange(1,200)
    print("coordinates ",x1,y1,x2,y2)
    myelements = [
        {"data": {"id": "one", "label": "Node A"}, "position": {"x": x1, "y": y1}},
        {"data": {"id": "two", "label": "Node B"}, "position": {"x": x2, "y": y2}},
        {"data": {"source": "one", "target": "two", "label": "Node 1 to 2"}},
    ]
    print(myelements)
    return [1, myelements]


if __name__ == "__main__":
    app.run_server(debug=True)

# -----------------------------------

New contributor

xyzmjf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật