kozo2 commited on
Commit
9670807
·
verified ·
1 Parent(s): 0117895

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ import streamlit as st
4
+ import streamlit.components.v1 as components
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
+ def build_html(elements_json, height):
36
+ """Return complete HTML/JS string for standalone Cytoscape viewer."""
37
+ return f"""
38
+ <!DOCTYPE html>
39
+ <html>
40
+ <head>
41
+ <meta charset="utf-8" />
42
+ <script src="https://unpkg.com/[email protected]/dist/cytoscape.min.js"></script>
43
+ <script src="https://unpkg.com/[email protected]/cytoscape-dagre.js"></script>
44
+ <style>
45
+ html, body {{ margin: 0; height: 100%; }}
46
+ #cy {{ width: 100%; height: {height}px; border: 1px solid #ccc; }}
47
+ </style>
48
+ </head>
49
+ <body>
50
+ <div id="cy"></div>
51
+ <script>
52
+ const elements = {elements_json};
53
+ const cy = cytoscape({{
54
+ container: document.getElementById('cy'),
55
+ elements: elements,
56
+ style: [
57
+ {{
58
+ selector: 'node',
59
+ style: {{
60
+ 'label': 'data(label)',
61
+ 'background-color': '{random_rgb()}',
62
+ 'text-valign': 'center',
63
+ 'color': '#fff',
64
+ 'font-size': 12
65
+ }}
66
+ }},
67
+ {{
68
+ selector: 'edge',
69
+ style: {{
70
+ 'width': 'mapData(weight, 1, 5, 1, 5)',
71
+ 'line-color': '#ccc',
72
+ 'target-arrow-color': '#ccc',
73
+ 'target-arrow-shape': 'triangle'
74
+ }}
75
+ }}
76
+ ],
77
+ layout: {{ name: 'dagre', directed: true }}
78
+ }});
79
+ </script>
80
+ </body>
81
+ </html>
82
+ """
83
+
84
+ # --- STREAMLIT UI --------------------------------------------------------------
85
+ st.set_page_config(page_title="Cytoscape + Streamlit Demo", layout="wide")
86
+ st.title("Cytoscape Network Viewer in Streamlit")
87
+
88
+ with st.sidebar:
89
+ st.header("Controls")
90
+ n_nodes = st.slider("Nodes", 3, 50, DEFAULT_NODES)
91
+ n_edges = st.slider("Edges", max(1, n_nodes - 1), 100, DEFAULT_EDGES)
92
+
93
+ if st.button("Regenerate"):
94
+ st.cache_data.clear()
95
+
96
+ # --- RENDER CYTOSCAPE ----------------------------------------------------------
97
+ elements = build_cytoscape_elements(n_nodes, n_edges)
98
+ html = build_html(json.dumps(elements), HEIGHT)
99
+ components.html(html, height=HEIGHT)