This repository was archived by the owner on Aug 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathusage.py
More file actions
executable file
·147 lines (132 loc) · 4.42 KB
/
usage.py
File metadata and controls
executable file
·147 lines (132 loc) · 4.42 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
from dash_sunburst import Sunburst
app = dash.Dash('')
app.scripts.config.serve_locally = True
app.css.config.serve_locally = True
sunburst_data = {
'name': 'house',
'children': [
{
'name': 'living room',
'children': [
{'name': 'couch', 'size': 6},
{'name': 'tv', 'size': 3},
{'name': 'desk', 'size': 4},
{'name': 'chair', 'size': 1},
{'name': 'table', 'size': 5},
{'name': 'piano', 'size': 2}
]
},
{
'name': 'kitchen',
'children': [
{'name': 'fridge', 'size': 3.5},
{'name': 'dishwasher', 'size': 2.5},
{'name': 'sink', 'size': 1.5},
{'name': 'cabinets', 'size': 8},
{'name': 'oven', 'size': 1.7}
]
},
{'name': 'coat closet', 'size': 4.5},
{'name': 'storage closet', 'size': 10},
{'name': 'bathroom', 'size': 7.5},
{
'name': 'master bedroom',
'children': [
{'name': 'bed', 'size': 9},
{'name': 'recliner', 'size': 3.2},
{'name': 'dresser', 'size': 4.7},
{'name': 'master bath', 'size': 7},
{'name': 'closet', 'size': 5.5}
]
},
{
'name': 'bedroom',
'children': [
{'name': 'bed', 'size': 5.7},
{'name': 'desk', 'size': 3.8},
{'name': 'dresser', 'size': 4.7},
{'name': 'closet', 'size': 5.3}
]
},
{'name': 'hall', 'size': 11}
]
}
app.layout = html.Div([
html.Div(
[Sunburst(id='sun', data=sunburst_data)],
style={'width': '49%', 'display': 'inline-block', 'float': 'left'}),
dcc.Graph(
id='graph',
style={'width': '49%', 'display': 'inline-block', 'float': 'left'}),
html.Div(id='output', style={'clear': 'both'})
])
@app.callback(Output('output', 'children'), [Input('sun', 'selectedPath')])
def display_selected(selected_path):
return 'You have selected path: {}'.format('->'.join(selected_path or []) or 'root')
@app.callback(Output('graph', 'figure'), [Input('sun', 'data'), Input('sun', 'selectedPath')])
def display_graph(data, selected_path):
x = []
y = []
text = []
color = []
joined_selected = '->'.join(selected_path or [])
SELECTED_COLOR = '#03c'
SELECTED_CHILDREN_COLOR = '#8cf'
SELECTED_PARENTS_COLOR = '#f80'
DESELECTED_COLOR = '#ccc'
def node_color(node_path):
joined_node = '->'.join(node_path)
if joined_node == joined_selected:
return SELECTED_COLOR
if joined_node.startswith(joined_selected):
return SELECTED_CHILDREN_COLOR
if joined_selected.startswith(joined_node):
return SELECTED_PARENTS_COLOR
return DESELECTED_COLOR
def append_point(child_count, size, node, node_path):
x.append(child_count)
y.append(size)
text.append(node['name'])
color.append(node_color(node_path))
def crawl(node, node_path):
if 'size' in node:
append_point(1, node['size'], node, node_path)
return (1, node['size'])
else:
node_count, node_size = 1, 0
for child in node['children']:
this_count, this_size = crawl(child, node_path + [child['name']])
node_count += this_count
node_size += this_size
append_point(node_count, node_size, node, node_path)
return (node_count, node_size)
crawl(data, [])
layout = {
'width': 500,
'height': 500,
'xaxis': {'title': 'Total Nodes', 'type': 'log'},
'yaxis': {'title': 'Total Size', 'type': 'log'},
'hovermode': 'closest'
}
return {
'data': [{
'x': x,
'y': y,
'text': text,
'textposition': 'middle right',
'marker': {
'color': color,
'size': [(v*v + 100)**0.5 for v in y],
'opacity': 0.5
},
'mode': 'markers+text',
'cliponaxis': False
}],
'layout': layout
}
if __name__ == '__main__':
app.run_server(debug=True)