Py.Cafe

kolibril13/

networkX-threejs-0

Coffee Beans Dashboard using Solara

DocsPricing
  • app.py
  • requirements.txt
  • widget.js
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import matplotlib.pyplot as plt
import networkx as nx
import anywidget
from pathlib import Path
from traitlets import List

#G = nx.cubical_graph()
#G = nx.tutte_graph()
#G = nx.frucht_graph()

G = nx.icosahedral_graph()

#node_positions = nx.spectral_layout(G, dim=3)
node_positions = nx.spring_layout(G, dim=3)


position_extracted = list(node_positions.values())
position_list = [list(position) for position in position_extracted]

# Extract edges with start and end points
edges = []
for edge in G.edges():
    start, end = edge
    start_pos = node_positions[start]
    end_pos = node_positions[end]
    edges.append([list(start_pos), list(end_pos)])

print("Node Positions:", position_list)
print("Type of node_positions:", type(position_list))
print("Edges:", edges)
print("Type of edges:", type(edges))

class HelloWidget(anywidget.AnyWidget):
    _esm = Path("widget.js")
    node_positions = List().tag(sync=True)
    edges = List().tag(sync=True)

three_viewer = HelloWidget()
three_viewer.node_positions = position_list
three_viewer.edges = edges
page = three_viewer