Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JasonBerto/99a3001876b9ea503ca5d4205082e5ce to your computer and use it in GitHub Desktop.

Select an option

Save JasonBerto/99a3001876b9ea503ca5d4205082e5ce to your computer and use it in GitHub Desktop.
Ejemplo de sankey con filtros de nodos - Mejora navegacion
import streamlit as st
import plotly.graph_objects as go
'''
Install streamlit : pip install streamlit
run file from terminal: $ streamlit run sankey_filter_nodes_streamlit_app.py
# it will run a python http server you will be able to use this app in you browser
'''
st.title("Filtered Sankey Diagram with People")
# Expanded sample data: people as nodes
nodes = [
"Alice", "Bob", "Carol", "Dave", "Eve", "Frank", "Grace",
"Heidi", "Ivan", "Judy", "Mallory", "Niaj", "Bobafet",
"Peggy", "Trent", "Victor", "Walter"
]
links = [
{"source": nodes.index("Alice"), "target": nodes.index("Bob"), "value": 5},
{"source": nodes.index("Alice"), "target": nodes.index("Carol"), "value": 3},
{"source": nodes.index("Bob"), "target": nodes.index("Dave"), "value": 4},
{"source": nodes.index("Bob"), "target": nodes.index("Eve"), "value": 2},
{"source": nodes.index("Bob"), "target": nodes.index("Frank"), "value": 1},
{"source": nodes.index("Carol"), "target": nodes.index("Eve"), "value": 6},
{"source": nodes.index("Carol"), "target": nodes.index("Frank"), "value": 3},
{"source": nodes.index("Dave"), "target": nodes.index("Frank"), "value": 4},
{"source": nodes.index("Dave"), "target": nodes.index("Grace"), "value": 3},
{"source": nodes.index("Eve"), "target": nodes.index("Grace"), "value": 5},
{"source": nodes.index("Eve"), "target": nodes.index("Heidi"), "value": 2},
{"source": nodes.index("Frank"), "target": nodes.index("Heidi"), "value": 4},
{"source": nodes.index("Grace"), "target": nodes.index("Ivan"), "value": 2},
{"source": nodes.index("Heidi"), "target": nodes.index("Judy"), "value": 1},
{"source": nodes.index("Ivan"), "target": nodes.index("Mallory"), "value": 3},
{"source": nodes.index("Judy"), "target": nodes.index("Niaj"), "value": 2},
{"source": nodes.index("Mallory"), "target": nodes.index("Bobafet"), "value": 4},
{"source": nodes.index("Niaj"), "target": nodes.index("Peggy"), "value": 3},
{"source": nodes.index("Bobafet"), "target": nodes.index("Trent"), "value": 2},
{"source": nodes.index("Peggy"), "target": nodes.index("Victor"), "value": 4},
{"source": nodes.index("Trent"), "target": nodes.index("Walter"), "value": 5},
]
# Text input for partial-match filter
search_term = st.text_input("Filter search term", value="a")
# Recursively find ancestors of matched nodes
def find_ancestors(targets, links):
ancestors = set(targets)
queue = list(targets)
while queue:
current = queue.pop()
for link in links:
if link["target"] == current and link["source"] not in ancestors:
ancestors.add(link["source"])
queue.append(link["source"])
return ancestors
# Find all node indices matching the search term (case-insensitive)
matched_indices = {i for i, label in enumerate(nodes) if search_term.lower() in label.lower()}
valid_nodes = find_ancestors(matched_indices, links)
# Filter links that connect valid nodes
filtered_links = [l for l in links if l["source"] in valid_nodes and l["target"] in valid_nodes]
# Remap node indices
filtered_node_indices = sorted(valid_nodes)
old_to_new = {old: new for new, old in enumerate(filtered_node_indices)}
filtered_node_labels = [nodes[i] for i in filtered_node_indices]
filtered_links_mapped = [
{
"source": old_to_new[l["source"]],
"target": old_to_new[l["target"]],
"value": l["value"]
}
for l in filtered_links
]
# Create Sankey figure
fig = go.Figure(data=[go.Sankey(
node=dict(label=filtered_node_labels),
link=dict(
source=[l["source"] for l in filtered_links_mapped],
target=[l["target"] for l in filtered_links_mapped],
value=[l["value"] for l in filtered_links_mapped]
)
)])
# Display diagram
st.plotly_chart(fig, use_container_width=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment