-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
125 lines (108 loc) · 2.69 KB
/
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import pathlib
import holoviews as hv
import pandas as pd
import panel as pn
from modules.constants import (
BLACK_COLOR,
COMMUTING_PURPOSE,
DASH_DESCR,
DASH_TITLE,
EDGES_DTYPES,
ITA_REGIONS,
NODES_DTYPES,
)
from modules.graphs import get_flow_map
from modules.indicators import (
get_incoming_numind,
get_internal_numind,
get_outgoing_numind,
)
# Load the bokeh extension
hv.extension("bokeh")
# Disable webgl: https://github.com/holoviz/panel/issues/4855
hv.renderer("bokeh").webgl = False # Disable Webgl
# Set the sizing mode
pn.extension(sizing_mode="stretch_width")
ROOT = pathlib.Path(__file__).parent
# Load the edges as a Dataframe
edges_df = pd.read_json(
os.path.join(ROOT, "data/edges.json"),
orient="split",
dtype=EDGES_DTYPES,
)
# Load the nodes as a Dataframe
nodes_df = pd.read_json(
os.path.join(ROOT, "data/nodes.json"),
orient="split",
dtype=NODES_DTYPES,
)
# Region selector
region_options = dict(map(reversed, ITA_REGIONS.items()))
region_options = dict(sorted(region_options.items()))
region_select = pn.widgets.Select(
name="Region:",
options=region_options,
sizing_mode="stretch_width",
)
# Toggle buttons to select the commuting purpose
purpose_select = pn.widgets.ToggleGroup(
name="",
options=COMMUTING_PURPOSE,
behavior="radio",
sizing_mode="stretch_width",
)
# Description pane
descr_pane = pn.pane.HTML(DASH_DESCR, style={"text-align": "left"})
# Numeric indicator for incoming flows
incoming_numind_bind = pn.bind(
get_incoming_numind,
edges=edges_df,
region_code=region_select,
comm_purpose=purpose_select,
)
# Numeric indicator for outgoing flows
outgoing_numind_bind = pn.bind(
get_outgoing_numind,
edges=edges_df,
region_code=region_select,
comm_purpose=purpose_select,
)
# Numeric indicator for internal flows
internal_numind_bind = pn.bind(
get_internal_numind,
edges=edges_df,
region_code=region_select,
comm_purpose=purpose_select,
)
# Flow map
flowmap_bind = pn.bind(
get_flow_map,
nodes=nodes_df,
edges=edges_df,
region_code=region_select,
comm_purpose=purpose_select,
)
# Compose the layout
layout = pn.Row(
pn.Column(
region_select,
purpose_select,
pn.Row(incoming_numind_bind, outgoing_numind_bind),
internal_numind_bind,
descr_pane,
width=350,
),
flowmap_bind,
)
# Turn into a deployable application
pn.template.FastListTemplate(
site="",
logo=os.path.join(ROOT, "icons/home_work.svg"),
title=DASH_TITLE,
theme="default",
theme_toggle=False,
header_background=BLACK_COLOR,
main=[layout],
main_max_width="1000px",
).servable()