-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpyvis_network_app.py
77 lines (63 loc) · 2.85 KB
/
pyvis_network_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import streamlit as st
import streamlit.components.v1 as components
import pandas as pd
import networkx as nx
from pyvis.network import Network
# Read dataset (CSV)
df_interact = pd.read_csv('data/processed_drug_interactions.csv')
# Set header title
st.title('Network Graph Visualization of Drug-Drug Interactions')
# Define list of selection options and sort alphabetically
drug_list = ['Metformin', 'Glipizide', 'Lisinopril', 'Simvastatin',
'Warfarin', 'Aspirin', 'Losartan', 'Ibuprofen']
drug_list.sort()
# Implement multiselect dropdown menu for option selection (returns a list)
selected_drugs = st.multiselect('Select drug(s) to visualize', drug_list)
# Set info message on initial site load
if len(selected_drugs) == 0:
st.text('Choose at least 1 drug to start')
# Create network graph when user selects >= 1 item
else:
df_select = df_interact.loc[df_interact['drug_1_name'].isin(selected_drugs) | \
df_interact['drug_2_name'].isin(selected_drugs)]
df_select = df_select.reset_index(drop=True)
# Create networkx graph object from pandas dataframe
G = nx.from_pandas_edgelist(df_select, 'drug_1_name', 'drug_2_name', 'weight')
# Initiate PyVis network object
drug_net = Network(
height='400px',
width='100%',
bgcolor='#222222',
font_color='white'
)
# Take Networkx graph and translate it to a PyVis graph format
drug_net.from_nx(G)
# Generate network with specific layout settings
drug_net.repulsion(
node_distance=420,
central_gravity=0.33,
spring_length=110,
spring_strength=0.10,
damping=0.95
)
# Save and read graph as HTML file (on Streamlit Sharing)
try:
path = '/tmp'
drug_net.save_graph(f'{path}/pyvis_graph.html')
HtmlFile = open(f'{path}/pyvis_graph.html', 'r', encoding='utf-8')
# Save and read graph as HTML file (locally)
except:
path = '/html_files'
drug_net.save_graph(f'{path}/pyvis_graph.html')
HtmlFile = open(f'{path}/pyvis_graph.html', 'r', encoding='utf-8')
# Load HTML file in HTML component for display on Streamlit page
components.html(HtmlFile.read(), height=435)
# Footer
st.markdown(
"""
<br>
<h6><a href="https://github.com/kennethleungty/Pyvis-Network-Graph-Streamlit" target="_blank">GitHub Repo</a></h6>
<h6><a href="https://kennethleungty.medium.com" target="_blank">Medium article</a></h6>
<h6>Disclaimer: This app is NOT intended to provide any form of medical advice or recommendations. Please consult your doctor or pharmacist for professional advice relating to any drug therapy.</h6>
""", unsafe_allow_html=True
)