kozo2 commited on
Commit
3698810
·
verified ·
1 Parent(s): 35e33b0

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +30 -74
src/streamlit_app.py CHANGED
@@ -1,83 +1,39 @@
1
- import json
2
- import random
3
  import streamlit as st
4
  from st_cytoscape import cytoscape
5
 
6
- # --- CONFIGURATION -------------------------------------------------------------
7
- HEIGHT = 600
8
- DEFAULT_NODES = 10
9
- DEFAULT_EDGES = 12
10
-
11
- # --- HELPER FUNCTIONS ----------------------------------------------------------
12
- def random_rgb():
13
- return "#{:02x}{:02x}{:02x}".format(
14
- random.randint(50, 200),
15
- random.randint(50, 200),
16
- random.randint(50, 200)
17
- )
18
-
19
- def build_cytoscape_elements(n_nodes, n_edges):
20
- """Return Cytoscape-compatible elements JSON."""
21
- nodes = [{"data": {"id": str(i), "label": f"Node {i}"}} for i in range(n_nodes)]
22
- edges = [
23
- {
24
- "data": {
25
- "id": f"{u}-{v}",
26
- "source": str(u),
27
- "target": str(v),
28
- "weight": random.randint(1, 5),
29
- }
30
- }
31
- for u, v in [random.sample(range(n_nodes), 2) for _ in range(n_edges)]
32
- ]
33
- return nodes + edges
34
-
35
- # --- STREAMLIT UI --------------------------------------------------------------
36
- st.set_page_config(page_title="Cytoscape + Streamlit Demo", layout="wide")
37
- st.title("Cytoscape Network Viewer in Streamlit")
38
 
39
- with st.sidebar:
40
- st.header("Controls")
41
- n_nodes = st.slider("Nodes", 3, 50, DEFAULT_NODES)
42
- n_edges = st.slider("Edges", max(1, n_nodes - 1), 100, DEFAULT_EDGES)
43
-
44
- if st.button("Regenerate"):
45
- st.cache_data.clear()
46
 
47
- # --- RENDER CYTOSCAPE ----------------------------------------------------------
48
- elements = build_cytoscape_elements(n_nodes, n_edges)
 
 
49
 
50
- # Define the stylesheet for nodes and edges
51
  stylesheet = [
52
- {
53
- "selector": 'node',
54
- "style": {
55
- 'label': 'data(label)',
56
- 'background-color': random_rgb(),
57
- 'text-valign': 'center',
58
- 'color': '#fff',
59
- 'font-size': 12
60
- }
61
- },
62
- {
63
- "selector": 'edge',
64
- "style": {
65
- 'width': 'mapData(weight, 1, 5, 1, 5)',
66
- 'line-color': '#bbb',
67
- 'target-arrow-color': '#bbb',
68
- 'curve-style': 'bezier',
69
- 'target-arrow-shape': 'triangle'
70
- }
71
- }
72
  ]
73
 
74
- # Render the Cytoscape graph using the st_cytoscape component
75
- cytoscape(
76
- elements=elements,
77
- stylesheet=stylesheet,
78
- layout={'name': 'dagre', 'rankDir': 'LR'},
79
- height=f"{HEIGHT}px",
80
- user_zooming_enabled=True,
81
- user_panning_enabled=True,
82
- selection_type="additive"
83
- )
 
 
 
1
  import streamlit as st
2
  from st_cytoscape import cytoscape
3
 
4
+ # elements = [
5
+ # {"data": {"id": "X"}, "selected": True, "selectable": False},
6
+ # {"data": {"id": "Y"}},
7
+ # {"data": {"id": "Z"}},
8
+ # {"data": {"source": "X", "target": "Y", "id": "X➞Y"}},
9
+ # {"data": {"source": "Z", "target": "Y", "id": "Z➞Y"}},
10
+ # {"data": {"source": "Z", "target": "X", "id": "Z➞X"}},
11
+ # ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ import requests
14
+ import json
 
 
 
 
 
15
 
16
+ url = "https://raw.githubusercontent.com/cytoscape/cytoscape.js/refs/heads/master/documentation/demos/tokyo-railways/tokyo-railways.json"
17
+ response = requests.get(url)
18
+ data = json.loads(response.text)
19
+ elements = data['elements']['nodes'] + data['elements']['edges']
20
 
 
21
  stylesheet = [
22
+ {"selector": "node",
23
+ "style": {"label": "data(station_name)", "width": 10, "height": 10}},
24
+ # {
25
+ # "selector": "edge",
26
+ # "style": {
27
+ # "width": 3,
28
+ # "curve-style": "bezier",
29
+ # "target-arrow-shape": "triangle",
30
+ # },
31
+ # },
 
 
 
 
 
 
 
 
 
 
32
  ]
33
 
34
+
35
+ selected = cytoscape(elements, stylesheet, width="100%", height="540px", layout={"name": "preset"}, key="graph")
36
+
37
+ print(selected)
38
+ st.markdown("**Selected nodes**: %s" % (", ".join(selected["nodes"])))
39
+ st.markdown("**Selected edges**: %s" % (", ".join(selected["edges"])))