shukdevdatta123 commited on
Commit
8e7238b
·
verified ·
1 Parent(s): 162cef9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -34
app.py CHANGED
@@ -94,46 +94,50 @@ def triads_graph():
94
  elif graph_mode == "Create Your Own":
95
  st.write("### Create Your Own Triads")
96
 
97
- # Let user define custom triads
98
  triad_input = st.text_area(
99
- "Enter the edges for your triad (e.g., [(1, 2), (2, 1), (3, 1)])",
100
- value="[(1, 2), (2, 1), (3, 1)]"
101
  )
102
 
103
- # Try to evaluate the input as a list of edges
104
  try:
105
- custom_triad = eval(triad_input)
106
- if isinstance(custom_triad, list) and all(isinstance(edge, tuple) and len(edge) == 2 for edge in custom_triad):
107
- G = nx.DiGraph()
108
- G.add_nodes_from([1, 2, 3])
109
- G.add_edges_from(custom_triad)
110
-
111
- # Plot the graph
112
- fig, ax = plt.subplots(figsize=(5, 5))
113
- nx.draw_networkx(
114
- G,
115
- ax=ax,
116
- with_labels=False,
117
- node_color=["green"],
118
- node_size=200,
119
- arrowsize=20,
120
- width=2,
121
- pos=nx.planar_layout(G),
122
- )
123
- ax.set_xlim(val * 1.2 for val in ax.get_xlim())
124
- ax.set_ylim(val * 1.2 for val in ax.get_ylim())
125
- ax.text(
126
- 0,
127
- 0,
128
- "Custom Triad",
129
- fontsize=15,
130
- fontweight="extra bold",
131
- horizontalalignment="center",
132
- bbox={"boxstyle": "square,pad=0.3", "fc": "none"},
133
- )
 
 
 
 
134
  st.pyplot(fig)
135
  else:
136
- st.error("Invalid format. Please enter a list of edges in the format [(1, 2), (2, 1), ...].")
137
  except Exception as e:
138
  st.error(f"Error parsing input: {e}")
139
 
 
94
  elif graph_mode == "Create Your Own":
95
  st.write("### Create Your Own Triads")
96
 
97
+ # Input: Enter triads as a dictionary (e.g., {'triad_name': [(1, 2), (2, 1)]})
98
  triad_input = st.text_area(
99
+ "Enter your triads in the format: {'triad_name': [(edge1), (edge2), ...]}",
100
+ value="{'003': [], '012': [(1, 2)]}"
101
  )
102
 
103
+ # Try to evaluate the input as a dictionary of triads
104
  try:
105
+ custom_triads = eval(triad_input)
106
+ if isinstance(custom_triads, dict) and all(isinstance(value, list) and all(isinstance(edge, tuple) and len(edge) == 2 for edge in value) for value in custom_triads.values()):
107
+ fig, axes = plt.subplots(len(custom_triads), 1, figsize=(10, len(custom_triads) * 5))
108
+ if len(custom_triads) == 1: # Handle case where only one triad is entered
109
+ axes = [axes]
110
+
111
+ for (title, triad), ax in zip(custom_triads.items(), axes):
112
+ G = nx.DiGraph()
113
+ G.add_nodes_from([1, 2, 3])
114
+ G.add_edges_from(triad)
115
+
116
+ nx.draw_networkx(
117
+ G,
118
+ ax=ax,
119
+ with_labels=False,
120
+ node_color=["green"],
121
+ node_size=200,
122
+ arrowsize=20,
123
+ width=2,
124
+ pos=nx.planar_layout(G),
125
+ )
126
+ ax.set_xlim(val * 1.2 for val in ax.get_xlim())
127
+ ax.set_ylim(val * 1.2 for val in ax.get_ylim())
128
+ ax.text(
129
+ 0,
130
+ 0,
131
+ title,
132
+ fontsize=15,
133
+ fontweight="extra bold",
134
+ horizontalalignment="center",
135
+ bbox={"boxstyle": "square,pad=0.3", "fc": "none"},
136
+ )
137
+ fig.tight_layout()
138
  st.pyplot(fig)
139
  else:
140
+ st.error("Invalid format. Please enter a dictionary of triads in the format {'triad_name': [(edge1), (edge2), ...]}.")
141
  except Exception as e:
142
  st.error(f"Error parsing input: {e}")
143