donb-hf commited on
Commit
f6bf57c
Β·
verified Β·
1 Parent(s): 0905706

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -64,23 +64,26 @@ def add_to_graph(
64
  return search_string, fig
65
 
66
  def create_plotly_graph(graph: nx.DiGraph) -> go.Figure:
67
- pos: Dict[Any, Tuple[float, float]] = nx.spring_layout(graph)
68
 
69
- # Edge trace initialization
70
  edge_x, edge_y = [], []
 
71
 
72
- for edge in graph.edges():
73
  x0, y0 = pos[edge[0]]
74
  x1, y1 = pos[edge[1]]
75
  edge_x.extend([x0, x1, None])
76
  edge_y.extend([y0, y1, None])
 
77
 
78
- # Create edge trace with improved style
79
  edge_trace = go.Scatter(
80
  x=edge_x,
81
  y=edge_y,
82
  line=dict(width=1.0, color='#888'),
83
- hoverinfo='none',
 
84
  mode='lines',
85
  name="Edges"
86
  )
@@ -97,32 +100,32 @@ def create_plotly_graph(graph: nx.DiGraph) -> go.Figure:
97
  )
98
  node_color.append(color_map[graph.nodes[node]['type']])
99
 
100
- # Create node trace for hover interactivity
101
  node_trace = go.Scatter(
102
  x=node_x,
103
  y=node_y,
104
  text=node_text,
105
- mode='markers', # Changed from 'markers+text' to 'markers' for hover only
106
  hoverinfo='text',
107
  marker=dict(
108
  showscale=True,
109
- colorscale='Viridis', # Updated to 'Viridis' for a more vibrant color scheme
110
  reversescale=True,
111
  color=node_color,
112
- size=15, # Increased size for better visibility
113
- line_width=2
114
  ),
115
  name="Nodes"
116
  )
117
 
118
- # Create the figure and add traces with improved layout and style
119
  fig = go.Figure()
120
  fig.add_trace(edge_trace)
121
  fig.add_trace(node_trace)
122
  fig.update_layout(
123
  title='Your Educational Landscape',
124
  titlefont_size=20,
125
- showlegend=True, # Show legend
126
  hovermode='closest',
127
  margin=dict(b=20, l=5, r=5, t=40),
128
  annotations=[dict(
@@ -133,9 +136,19 @@ def create_plotly_graph(graph: nx.DiGraph) -> go.Figure:
133
  )],
134
  xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
135
  yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
136
- plot_bgcolor='rgba(240, 240, 240, 0.95)' # Set a light background for better contrast
137
  )
138
 
 
 
 
 
 
 
 
 
 
 
139
  return fig
140
 
141
  def clear_graph() -> str:
 
64
  return search_string, fig
65
 
66
  def create_plotly_graph(graph: nx.DiGraph) -> go.Figure:
67
+ pos: Dict[Any, Tuple[float, float]] = nx.spring_layout(graph, seed=42) # Adding seed for consistent layout
68
 
69
+ # Edge trace initialization with descriptive labels
70
  edge_x, edge_y = [], []
71
+ edge_text = []
72
 
73
+ for edge in graph.edges(data=True):
74
  x0, y0 = pos[edge[0]]
75
  x1, y1 = pos[edge[1]]
76
  edge_x.extend([x0, x1, None])
77
  edge_y.extend([y0, y1, None])
78
+ edge_text.append(f"{edge[0]} -> {edge[1]}: {edge[2]['relationship']}") # Adding edge relationship label
79
 
80
+ # Create edge trace with hover text for relationship descriptions
81
  edge_trace = go.Scatter(
82
  x=edge_x,
83
  y=edge_y,
84
  line=dict(width=1.0, color='#888'),
85
+ hoverinfo='text',
86
+ text=edge_text,
87
  mode='lines',
88
  name="Edges"
89
  )
 
100
  )
101
  node_color.append(color_map[graph.nodes[node]['type']])
102
 
103
+ # Create node trace with enhanced marker styles and hover info
104
  node_trace = go.Scatter(
105
  x=node_x,
106
  y=node_y,
107
  text=node_text,
108
+ mode='markers',
109
  hoverinfo='text',
110
  marker=dict(
111
  showscale=True,
112
+ colorscale='Viridis',
113
  reversescale=True,
114
  color=node_color,
115
+ size=20, # Increased size for better visibility
116
+ line=dict(width=3, color='DarkSlateGrey') # Added border for better contrast
117
  ),
118
  name="Nodes"
119
  )
120
 
121
+ # Create the figure and add traces with enhanced layout and better spacing
122
  fig = go.Figure()
123
  fig.add_trace(edge_trace)
124
  fig.add_trace(node_trace)
125
  fig.update_layout(
126
  title='Your Educational Landscape',
127
  titlefont_size=20,
128
+ showlegend=True,
129
  hovermode='closest',
130
  margin=dict(b=20, l=5, r=5, t=40),
131
  annotations=[dict(
 
136
  )],
137
  xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
138
  yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
139
+ plot_bgcolor='rgba(240, 240, 240, 0.95)'
140
  )
141
 
142
+ # Add legend groups
143
+ for node_type, color in color_map.items():
144
+ fig.add_trace(go.Scatter(
145
+ x=[],
146
+ y=[],
147
+ mode='markers',
148
+ marker=dict(size=10, color=color),
149
+ name=node_type
150
+ ))
151
+
152
  return fig
153
 
154
  def clear_graph() -> str: