|
import pandas as pd |
|
import networkx as nx |
|
import matplotlib.pyplot as plt |
|
from pyvis.network import Network |
|
|
|
from data import df |
|
|
|
|
|
def analysis(): |
|
G = nx.DiGraph() |
|
|
|
|
|
relationship_columns = ['father', 'sibling', 'spouse', 'mother', 'child'] |
|
|
|
G.add_nodes_from(df["itemLabel"]) |
|
|
|
for index, row in df.iterrows(): |
|
main_entity = row['itemLabel'] |
|
for relationship in relationship_columns: |
|
if pd.notna(row[relationship]): |
|
G.add_edge(main_entity, row[relationship], relationship=str(relationship), label=relationship) |
|
|
|
plt.figure(figsize=(50, 20)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
net = Network(width="1200px", height="800px", bgcolor="#222222", font_color="white", directed=True) |
|
net.from_nx(G) |
|
|
|
for node in net.nodes: |
|
node["title"] = node["id"] |
|
node["value"] = len(G[node["id"]]) |
|
|
|
|
|
for edge in net.edges: |
|
|
|
relationship = G[edge["from"]][edge["to"]].get("relationship", "Unknown") |
|
edge["title"] = relationship |
|
edge["color"] = "blue" |
|
edge["width"] = 2 if relationship != "Unknown" else 1 |
|
|
|
html = net.generate_html() |
|
|
|
html = html.replace("'", "\"") |
|
|
|
return f"""<iframe style="width: 100%; height: 800px;margin:0 auto" name="result" allow="midi; geolocation; microphone; camera; |
|
display-capture; encrypted-media;" sandbox="allow-modals allow-forms |
|
allow-scripts allow-same-origin allow-popups |
|
allow-top-navigation-by-user-activation allow-downloads" allowfullscreen="" |
|
allowpaymentrequest="" frameborder="0" srcdoc='{html}'></iframe>""" |
|
|