def main(): global G_igraph, G_nx, nodes_vector, aca_graph G_igraph, G_nx, nodes_vector = dp.generate_data() aca_graph = dp.separate_data_by_academy(nodes_vector, G_igraph)
def main(): global G_igraph, G_nx, nodes_vector, aca_graph G_igraph, G_nx, nodes_vector = dp.generate_data() # TODO 读取上传文件的图信息,获得图信息 (G_igraph, G_nx) 和 nodes_vector aca_graph = dp.separate_data_by_academy(nodes_vector, G_igraph)
def upload_file(): ''' 解析上传文件,构造图 ''' global G_igraph, G_nx, nodes_vector, aca_graph if request.method == 'POST': uploaded_file = request.files['file'] if uploaded_file and allowed_file(uploaded_file.filename): filename = secure_filename(uploaded_file.filename) # 保存上传文件到服务器 uploaded_file.save( os.path.join(app.config['UPLOAD_FOLDER'], filename)) file_path = app.config['UPLOAD_FOLDER'] + uploaded_file.filename # 第一次遍历文件,找出编号最大的节点 with open(file_path, 'r') as f: max_num = 0 graph_nodes = [] for line in f: if line and not line.strip().startswith('#'): from_node_str, to_node_str = line.rsplit(None, 1) from_node = map(int, from_node_str.split(',')) to_node = map(int, to_node_str.split(',')) from_node_id = from_node[0] to_node_id = to_node[0] # 判断节点是否在节点集合里 if from_node_id not in graph_nodes: graph_nodes.append(from_node_id) if to_node_id not in graph_nodes: graph_nodes.append(to_node_id) if from_node_id > max_num or to_node_id > max_num: max_num = max(from_node_id, to_node_id) nodes_vector = [0] * (max_num + 1) G_igraph.add_vertices(graph_nodes) # 添加节点集合到图里 f.seek(0) # 返回文件头 # 再次遍历文件,填充 nodes_vector,构造 G_igraph for line in f: if line and not line.strip().startswith('#'): from_node_str, to_node_str = line.rsplit(None, 1) from_node = map(int, from_node_str.split(',')) to_node = map(int, to_node_str.split(',')) from_node_id = from_node[0] from_node_vector = from_node[1:] to_node_id = to_node[0] to_node_vector = to_node[1:] # 添加边 G_igraph.add_edge(from_node_id, to_node_id) nodes_vector[from_node_id] = from_node_vector nodes_vector[to_node_id] = to_node_vector G_nx = nx.DiGraph(G_igraph.get_edgelist()) aca_graph = dp.separate_data_by_academy(nodes_vector, G_igraph) return render_template('main.html')