-
Notifications
You must be signed in to change notification settings - Fork 22
/
relationships_server.py
72 lines (58 loc) · 2.45 KB
/
relationships_server.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
"""
Purpose: Provide reactive output for the relationships dataset.
- Use inputs from the UI Sidebar to filter the dataset.
- Update reactive outputs in the UI Main Panel.
Matching the IDs in the UI Sidebar and function/output names in the UI Main Panel
to this server code is critical. They are case sensitive and must match exactly.
See: https://holoviews.org/reference/elements/bokeh/Chord.html
"""
import pandas as pd
import holoviews as hv
from holoviews import opts, dim
from bokeh.sampledata.les_mis import data
import jupyter_bokeh as jbk
from shinywidgets import render_widget
from util_logger import setup_logger
logger, logname = setup_logger(__name__)
hv.extension("bokeh")
def get_relationships_server_functions(input, output, session):
"""Define functions to create UI outputs."""
# get a pandas dataframe of the links and the nodes
df_links = pd.DataFrame(data["links"])
df_nodes = pd.DataFrame(data["nodes"])
# get a holoviews dataset of nodes. the 'index' column is the node id
nodes = hv.Dataset(df_nodes, "index")
@output
@render_widget
def relationships_output_widget1():
if input.RELATIONSHIPS_SHOW_TOGGLE():
# logger.info("UI inputs changed. Updating relationships output widget2")
# create a chord diagram from the links and the nodes
# value = (5, None) only links with a value 5+ will be shown
chord = hv.Chord((df_links, nodes)).select(value=(5, None))
# Set some chart options
# Category20 is a color palette
# The edge_color is based on the source node
# The node_color is based on the index (node id)
# The labels are the names of the nodes (from the name column)
chord.opts(
opts.Chord(
cmap="Category20",
edge_cmap="Category20",
edge_color=dim("source").str(),
labels="name",
node_color=dim("index").str(),
title="Les Misérables Relationships 5+ (HoloViews Bokeh)",
width=800,
height=800,
)
)
widget = hv.render(chord, backend="bokeh")
wrapped_widget = jbk.BokehModel(widget)
return wrapped_widget
else:
return None
# return a list of function names for use in reactive outputs
return [
relationships_output_widget1,
]