152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258  | def prepare_plotly_graph(network_data: Union["Value", "NetworkData", None]):
    import plotly.graph_objects as go
    import plotly.io as pio
    if network_data is None:
        return None
    # Set plotly renderer for marimo compatibility
    pio.renderers.default = "json"
    # Get the rustworkx graph
    network_data = extract_network_data(network_data)
    rx_graph = network_data.as_rustworkx_graph(
        rx.PyGraph, incl_node_attributes=[LABEL_COLUMN_NAME]
    )
    # Get node positions using spring layout
    pos = rx.spring_layout(rx_graph, seed=42)
    # Extract node coordinates
    node_x = [pos[node][0] for node in rx_graph.node_indices()]
    node_y = [pos[node][1] for node in rx_graph.node_indices()]
    # Get node labels/info
    node_info = [
        rx_graph.get_node_data(i)[LABEL_COLUMN_NAME] for i in rx_graph.node_indices()
    ]
    # Extract edge coordinates
    edge_x = []
    edge_y = []
    for edge in rx_graph.edge_list():
        x0, y0 = pos[edge[0]]
        x1, y1 = pos[edge[1]]
        edge_x.extend([x0, x1, None])
        edge_y.extend([y0, y1, None])
    # Create edge trace
    edge_trace = go.Scatter(
        x=edge_x,
        y=edge_y,
        line={"width": 0.5, "color": "#888"},
        hoverinfo="none",
        mode="lines",
    )
    # Create node trace
    node_trace = go.Scatter(
        x=node_x,
        y=node_y,
        mode="markers",
        hoverinfo="text",
        text=node_info,
        marker={
            "showscale": True,
            "colorscale": "YlGnBu",
            "reversescale": True,
            "color": [],
            "size": 10,
            "colorbar": {
                "thickness": 15,
                "len": 0.5,
                "x": 1.02,
                "title": "Node Connections",
            },
            "line": {"width": 2},
        },
    )
    # Color nodes by number of connections
    node_adjacencies = []
    for node in rx_graph.node_indices():
        adjacencies = len(list(rx_graph.neighbors(node)))
        node_adjacencies.append(adjacencies)
    node_trace.marker.color = node_adjacencies
    # Create the figure
    fig = go.Figure(
        data=[edge_trace, node_trace],
        layout=go.Layout(
            title={
                "text": "Interactive Network Graph (RustWorkX + Plotly)",
                "font": {"size": 16},
            },
            showlegend=False,
            hovermode="closest",
            margin={"b": 20, "l": 5, "r": 5, "t": 40},
            annotations=[
                {
                    "text": "Hover over nodes to see details",
                    "showarrow": False,
                    "xref": "paper",
                    "yref": "paper",
                    "x": 0.005,
                    "y": -0.002,
                    "xanchor": "left",
                    "yanchor": "bottom",
                    "font": {"color": "#888", "size": 12},
                }
            ],
            xaxis={"showgrid": False, "zeroline": False, "showticklabels": False},
            yaxis={"showgrid": False, "zeroline": False, "showticklabels": False},
        ),
    )
    return fig
  |