Spaces:
Runtime error
Runtime error
hacpdsae2023
commited on
Commit
·
2df8c8c
1
Parent(s):
fafd797
test streamlit-agraph
Browse filesTesting streamlit agraph
app.py
CHANGED
@@ -2,4 +2,60 @@ import streamlit as st
|
|
2 |
from datasets import load_dataset
|
3 |
dataset = load_dataset("roneneldan/TinyStories")
|
4 |
|
5 |
-
st.write(dataset['train'][0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from datasets import load_dataset
|
3 |
dataset = load_dataset("roneneldan/TinyStories")
|
4 |
|
5 |
+
st.write(dataset['train'][0])
|
6 |
+
|
7 |
+
# ego_graph.py
|
8 |
+
# An example of how to plot a node's ego network
|
9 |
+
# (egonet). This indirectly showcases slightly more involved
|
10 |
+
# interoperability between streamlit-agraph and networkx.
|
11 |
+
|
12 |
+
# An egonet can be # created from (almost) any network (graph),
|
13 |
+
# and exemplifies the # concept of a subnetwork (subgraph):
|
14 |
+
# A node's egonet is the (sub)network comprised of the focal node
|
15 |
+
# and all the nodes to whom it is adjacent. The edges included
|
16 |
+
# in the egonet are those nodes are both included in the aforementioned
|
17 |
+
# nodes.
|
18 |
+
|
19 |
+
# Use the following command to launch the app
|
20 |
+
# streamlit run <path-to-script>.py
|
21 |
+
|
22 |
+
# standard library dependencies
|
23 |
+
from operator import itemgetter
|
24 |
+
|
25 |
+
# external dependencies
|
26 |
+
import networkx as nx
|
27 |
+
from streamlit_agraph import agraph, Node, Edge, Config
|
28 |
+
|
29 |
+
# First create a graph using the Barabasi-Albert model
|
30 |
+
n = 2000
|
31 |
+
m = 2
|
32 |
+
G = nx.generators.barabasi_albert_graph(n, m)
|
33 |
+
|
34 |
+
# Then find the node with the largest degree;
|
35 |
+
# This node's egonet will be the focus of this example.
|
36 |
+
node_and_degree = G.degree()
|
37 |
+
most_connected_node = sorted(G.degree, key=lambda x: x[1], reverse=True)[0]
|
38 |
+
degree = G.degree(most_connected_node)
|
39 |
+
|
40 |
+
# Create egonet for the focal node
|
41 |
+
hub_ego = nx.ego_graph(G, most_connected_node[0])
|
42 |
+
|
43 |
+
# Now create the equivalent Node and Edge lists
|
44 |
+
nodes = [Node(id=i, label=str(i), size=200) for i in hub_ego.nodes]
|
45 |
+
edges = [Edge(source=i, target=j, type="CURVE_SMOOTH") for (i,j) in G.edges
|
46 |
+
if i in hub_ego.nodes and j in hub_ego.nodes]
|
47 |
+
|
48 |
+
|
49 |
+
config = Config(width=500,
|
50 |
+
height=500,
|
51 |
+
directed=True,
|
52 |
+
nodeHighlightBehavior=False,
|
53 |
+
highlightColor="#F7A7A6", # or "blue"
|
54 |
+
collapsible=False,
|
55 |
+
node={'labelProperty':'label'},
|
56 |
+
# **kwargs e.g. node_size=1000 or node_color="blue"
|
57 |
+
)
|
58 |
+
|
59 |
+
return_value = agraph(nodes=nodes,
|
60 |
+
edges=edges,
|
61 |
+
config=config)
|