import streamlit as st from pyvis.network import Network import networkx as nx import random import streamlit.components.v1 as components st.title('파트너 매칭 프로그램') st.write('이름을 한 줄에 하나씩 입력하세요.') names_input = st.text_area('이름 목록', height=200) if st.button('파트너 매칭하기'): names = [name.strip() for name in names_input.strip().split('\n') if name.strip()] if len(names) < 2: st.error('매칭할 이름이 2개 이상 필요합니다.') else: random.shuffle(names) mid = len(names) // 2 group_A = names[:mid] group_B = names[mid:] # 그래프 생성 G = nx.Graph() G.add_nodes_from(group_A, bipartite=0) G.add_nodes_from(group_B, bipartite=1) # 모든 가능한 간선 추가 for a in group_A: for b in group_B: G.add_edge(a, b) # 매칭 알고리즘 실행 (최대 매칭) mate = nx.algorithms.bipartite.maximum_matching(G, top_nodes=group_A) # 매칭 결과만으로 새로운 그래프 생성 matching_edges = [(u, v) for u, v in mate.items() if u in group_A] # pyvis 네트워크 생성 net = Network(height='600px', width='100%', bgcolor='#222222', font_color='white') # 노드 추가 for node in G.nodes(): net.add_node(node, label=node, title=node) # 매칭된 간선은 강조하여 추가 for edge in matching_edges: net.add_edge(edge[0], edge[1], color='red', width=3) # 네트워크 설정 net.toggle_physics(True) # HTML 코드 생성 net_html = net.generate_html() # Streamlit에 표시 components.html(net_html, height=600) # 매칭 결과 출력 st.subheader('매칭 결과:') for u, v in matching_edges: st.write(f'{u} - {v}') unmatched = set(names) - set(mate.keys()) - set(mate.values()) if unmatched: st.write('매칭되지 않은 사람:') for name in unmatched: st.write(name)