🤬
  • ■ ■ ■ ■ ■ ■
    src/app.py
     1 +import streamlit as st
     2 +import pandas as pd
     3 +from streamlit_agraph import agraph, Config
     4 +from utils import build_agraph_components, get_edges_df, get_subgraph_df, get_nodes_df
     5 + 
     6 + 
     7 +st.set_page_config(layout="wide")
     8 + 
     9 + 
     10 +SLIDER_MIN = 1
     11 +SLIDER_MAX = 100
     12 +SLIDER_DEFAULT = 50
     13 + 
     14 +nodes = get_nodes_df()
     15 +edges = get_edges_df()
     16 +subgraphs = get_subgraph_df()
     17 + 
     18 +with st.sidebar:
     19 + st.title("Corporate risks")
     20 + 
     21 + weight_chains = st.slider(
     22 + "Long ownership chains",
     23 + min_value=SLIDER_MIN,
     24 + max_value=SLIDER_MAX,
     25 + value=SLIDER_DEFAULT,
     26 + )
     27 + weight_cyclic = st.slider(
     28 + "Cyclic ownership",
     29 + min_value=SLIDER_MIN,
     30 + max_value=SLIDER_MAX,
     31 + value=SLIDER_DEFAULT,
     32 + )
     33 + weight_psc_haven = st.slider(
     34 + "Persons of significant control associated with tax havens",
     35 + min_value=SLIDER_MIN,
     36 + max_value=SLIDER_MAX,
     37 + value=SLIDER_DEFAULT,
     38 + )
     39 + weight_pep = st.slider(
     40 + "Officers/PSCs are politically exposed",
     41 + min_value=SLIDER_MIN,
     42 + max_value=SLIDER_MAX,
     43 + value=SLIDER_DEFAULT,
     44 + )
     45 + weight_sanctions = st.slider(
     46 + "Officers/PSCs/Companies are sanctioned",
     47 + min_value=SLIDER_MIN,
     48 + max_value=SLIDER_MAX,
     49 + value=SLIDER_DEFAULT,
     50 + )
     51 + weight_disqualified = st.slider(
     52 + "Officers are disqualified directors",
     53 + min_value=SLIDER_MIN,
     54 + max_value=SLIDER_MAX,
     55 + value=SLIDER_DEFAULT,
     56 + )
     57 + 
     58 + custom_names_a = st.multiselect(
     59 + label="Custom persons of interest",
     60 + options=nodes["node_id"],
     61 + default=None,
     62 + )
     63 + custom_names_b = st.file_uploader(label="Custom persons of interest", type="csv")
     64 + 
     65 + go = st.button("Go")
     66 + 
     67 + 
     68 +with st.container():
     69 + st.write(subgraphs)
     70 + 
     71 + selected_subgraph_hash = st.selectbox(
     72 + label="Select subgraph to explore", options=subgraphs.index
     73 + )
     74 + 
     75 +nodes_selected = nodes.loc[nodes["subgraph_hash"] == selected_subgraph_hash]
     76 +edges_selected = edges.loc[edges["subgraph_hash"] == selected_subgraph_hash]
     77 + 
     78 +with st.container():
     79 + 
     80 + col1, col2 = st.columns(2)
     81 + 
     82 + with col1:
     83 + (node_objects, edge_objects) = build_agraph_components(
     84 + nodes_selected, edges_selected
     85 + )
     86 + agraph(
     87 + nodes=node_objects,
     88 + edges=edge_objects,
     89 + config=Config(
     90 + width=500,
     91 + height=500,
     92 + ),
     93 + )
     94 + 
     95 + with col2:
     96 + st.write(nodes_selected)
     97 + 
  • ■ ■ ■ ■ ■ ■
    src/utils.py
     1 +import streamlit as st
     2 +from streamlit_agraph import Node, Edge
     3 +import json
     4 +import pandas as pd
     5 + 
     6 +NODE_COLOUR_PERSON = "#4684B2"
     7 +NODE_COLOUR_COMPANY = "#46B247"
     8 + 
     9 + 
     10 +@st.cache()
     11 +def get_subgraph_df():
     12 + return pd.read_csv("subgraphs.csv", index_col="subgraph_hash")
     13 + 
     14 + 
     15 +@st.cache()
     16 +def get_nodes_df():
     17 + return pd.read_csv("nodes.csv")
     18 + 
     19 + 
     20 +@st.cache()
     21 +def get_edges_df():
     22 + return pd.read_csv("edges.csv")
     23 + 
     24 + 
     25 +@st.cache()
     26 +def build_agraph_components(
     27 + nodes,
     28 + edges,
     29 + node_colour_person=NODE_COLOUR_PERSON,
     30 + node_colour_company=NODE_COLOUR_COMPANY,
     31 +):
     32 + """Create agraph object from node and edge list"""
     33 + 
     34 + node_objects = []
     35 + edge_objects = []
     36 + 
     37 + for _, row in nodes.iterrows():
     38 + node_metadata = json.loads(row["node_metadata"])
     39 + node_objects.append(
     40 + Node(
     41 + id=row["node_id"],
     42 + label=node_metadata["name"],
     43 + size=25,
     44 + color=node_colour_person
     45 + if row["is_person"] == 1
     46 + else node_colour_company,
     47 + )
     48 + )
     49 + 
     50 + for _, row in edges.iterrows():
     51 + edge_objects.append(
     52 + Edge(
     53 + source=row["source"],
     54 + label=row["type"],
     55 + target=row["target"],
     56 + )
     57 + )
     58 + 
     59 + return (node_objects, edge_objects)
     60 + 
Please wait...
Page is in error, reload to recover