Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
-
from typing import Tuple, Dict, Any, List
|
2 |
-
import gradio as gr
|
3 |
import networkx as nx
|
4 |
import plotly.graph_objs as go
|
5 |
from plotly.subplots import make_subplots
|
|
|
|
|
6 |
import plotly.io as pio
|
7 |
|
8 |
# Initialize the lesson plan graph as a directed graph
|
@@ -77,41 +77,37 @@ def add_to_graph(
|
|
77 |
def create_plotly_graph(graph: nx.DiGraph) -> go.Figure:
|
78 |
pos: Dict[Any, Tuple[float, float]] = nx.spring_layout(graph)
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
y=[],
|
83 |
-
line=dict(width=0.5, color='#888'),
|
84 |
-
hoverinfo='none',
|
85 |
-
mode='lines')
|
86 |
|
87 |
for edge in graph.edges():
|
88 |
x0, y0 = pos[edge[0]]
|
89 |
x1, y1 = pos[edge[1]]
|
90 |
-
|
91 |
-
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
hoverinfo='
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
size=10,
|
105 |
-
line_width=2))
|
106 |
|
107 |
for node in graph.nodes():
|
108 |
x, y = pos[node]
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
112 |
|
113 |
-
|
114 |
-
|
|
|
115 |
for node, adjacencies in enumerate(graph.adjacency()):
|
116 |
node_adjacencies.append(len(adjacencies[1]))
|
117 |
node_info = list(graph.nodes())[node]
|
@@ -119,10 +115,25 @@ def create_plotly_graph(graph: nx.DiGraph) -> go.Figure:
|
|
119 |
hover_text = f"Node: {node_info}<br>Type: {node_data['type']}<br>{node_data.get('description', '')}"
|
120 |
node_hover_texts.append(hover_text)
|
121 |
|
122 |
-
|
123 |
-
node_trace
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
-
|
|
|
126 |
fig.add_trace(edge_trace)
|
127 |
fig.add_trace(node_trace)
|
128 |
fig.update_layout(
|
@@ -130,7 +141,7 @@ def create_plotly_graph(graph: nx.DiGraph) -> go.Figure:
|
|
130 |
titlefont_size=16,
|
131 |
showlegend=False,
|
132 |
hovermode='closest',
|
133 |
-
margin=dict(b=20,l=5,r=5,t=40),
|
134 |
annotations=[dict(
|
135 |
text="",
|
136 |
showarrow=False,
|
@@ -140,7 +151,7 @@ def create_plotly_graph(graph: nx.DiGraph) -> go.Figure:
|
|
140 |
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
141 |
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
|
142 |
)
|
143 |
-
|
144 |
return fig
|
145 |
|
146 |
def clear_graph() -> str:
|
@@ -189,4 +200,4 @@ with demo:
|
|
189 |
|
190 |
# Launch the EduScape app
|
191 |
if __name__ == "__main__":
|
192 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import networkx as nx
|
2 |
import plotly.graph_objs as go
|
3 |
from plotly.subplots import make_subplots
|
4 |
+
from typing import Dict, Tuple, List, Any
|
5 |
+
import gradio as gr
|
6 |
import plotly.io as pio
|
7 |
|
8 |
# Initialize the lesson plan graph as a directed graph
|
|
|
77 |
def create_plotly_graph(graph: nx.DiGraph) -> go.Figure:
|
78 |
pos: Dict[Any, Tuple[float, float]] = nx.spring_layout(graph)
|
79 |
|
80 |
+
# Edge trace initialization
|
81 |
+
edge_x, edge_y = [], []
|
|
|
|
|
|
|
|
|
82 |
|
83 |
for edge in graph.edges():
|
84 |
x0, y0 = pos[edge[0]]
|
85 |
x1, y1 = pos[edge[1]]
|
86 |
+
edge_x.extend([x0, x1, None])
|
87 |
+
edge_y.extend([y0, y1, None])
|
88 |
|
89 |
+
# Create edge trace
|
90 |
+
edge_trace = go.Scatter(
|
91 |
+
x=edge_x,
|
92 |
+
y=edge_y,
|
93 |
+
line=dict(width=0.5, color='#888'),
|
94 |
+
hoverinfo='none',
|
95 |
+
mode='lines'
|
96 |
+
)
|
97 |
+
|
98 |
+
# Node trace initialization
|
99 |
+
node_x, node_y, node_text, node_color = [], [], [], []
|
|
|
|
|
100 |
|
101 |
for node in graph.nodes():
|
102 |
x, y = pos[node]
|
103 |
+
node_x.append(x)
|
104 |
+
node_y.append(y)
|
105 |
+
node_text.append(node)
|
106 |
+
node_color.append(color_map[graph.nodes[node]['type']])
|
107 |
|
108 |
+
# Setting hover texts for each node
|
109 |
+
node_adjacencies = []
|
110 |
+
node_hover_texts = []
|
111 |
for node, adjacencies in enumerate(graph.adjacency()):
|
112 |
node_adjacencies.append(len(adjacencies[1]))
|
113 |
node_info = list(graph.nodes())[node]
|
|
|
115 |
hover_text = f"Node: {node_info}<br>Type: {node_data['type']}<br>{node_data.get('description', '')}"
|
116 |
node_hover_texts.append(hover_text)
|
117 |
|
118 |
+
# Create node trace
|
119 |
+
node_trace = go.Scatter(
|
120 |
+
x=node_x,
|
121 |
+
y=node_y,
|
122 |
+
text=node_hover_texts,
|
123 |
+
mode='markers+text',
|
124 |
+
hoverinfo='text',
|
125 |
+
marker=dict(
|
126 |
+
showscale=False,
|
127 |
+
colorscale='YlGnBu',
|
128 |
+
reversescale=True,
|
129 |
+
color=node_adjacencies,
|
130 |
+
size=10,
|
131 |
+
line_width=2
|
132 |
+
)
|
133 |
+
)
|
134 |
|
135 |
+
# Create the figure and add traces
|
136 |
+
fig = make_subplots()
|
137 |
fig.add_trace(edge_trace)
|
138 |
fig.add_trace(node_trace)
|
139 |
fig.update_layout(
|
|
|
141 |
titlefont_size=16,
|
142 |
showlegend=False,
|
143 |
hovermode='closest',
|
144 |
+
margin=dict(b=20, l=5, r=5, t=40),
|
145 |
annotations=[dict(
|
146 |
text="",
|
147 |
showarrow=False,
|
|
|
151 |
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
152 |
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
|
153 |
)
|
154 |
+
|
155 |
return fig
|
156 |
|
157 |
def clear_graph() -> str:
|
|
|
200 |
|
201 |
# Launch the EduScape app
|
202 |
if __name__ == "__main__":
|
203 |
+
demo.launch()
|