-
Notifications
You must be signed in to change notification settings - Fork 0
/
band_gantt_dash.py
182 lines (147 loc) · 5.74 KB
/
band_gantt_dash.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
import os
import random
from collections import defaultdict
from datetime import datetime
import colorlover as cl
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.figure_factory as ff
import requests
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
app.title='Band Gantt'
server = app.server
app.layout = html.Div(children=[
dcc.Location(id='url', refresh=False),
html.Div(children=[
html.Div(children=[
html.H1(children='Band Gantt'),
html.Div(children=[
html.Div([
dcc.Input(
id='band_search',
placeholder='Enter a band name...',
type='text',
value=''
)
]),
html.Div(
id='search_results',
children=[],
style={
'background-color': "#fcfcfc",
'border-radius': '20px',
'zIndex':'1',
'position':'absolute'
}
)
]),
]),
html.Div(id='graph', children=[], className="twelve columns")
],className="row")
])
@app.callback(
Output(component_id='search_results', component_property='children'),
[Input(component_id='band_search', component_property='value')]
)
def search_for_band(query_string):
if len(query_string) > 2:
band_search_results = requests.get(
"https://musicbrainz.org/ws/2/artist?query={}&limit=10&fmt=json".format(query_string + '*')
).json()
formatted_results = []
for artist in band_search_results['artists']:
formatted_results.append(
html.Span(children=[
dcc.Link(artist['name'], href=artist['id']),
html.Br()
])
)
return formatted_results
@app.callback(
Output(component_id='band_search', component_property='value'),
[Input(component_id='url', component_property='pathname')]
)
def clear_search_bar(pathname):
return ''
@app.callback(
Output(component_id='graph', component_property='children'),
[Input(component_id='url', component_property='pathname')]
)
def create_graph(pathname):
if not pathname or pathname == "/":
# A few example bands to generate at random when the app opens
example_ids = [
'b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d', # The Beatles
'c1d4f2ba-cf39-460c-9528-6b827d3417a1', # Yes
'eeb1195b-f213-4ce1-b28c-8565211f8e43', # Guns N' Roses
'36bfa85f-737b-41db-a8fc-b8825850ffc3', # Pavement
]
artist_id = random.choice(example_ids)
else:
artist_id = pathname[1:] # removes leading slash in pathname
band_info = requests.get(
"https://musicbrainz.org/ws/2/artist/{}?inc=artist-rels&fmt=json".format(artist_id)
).json()
if 'error' in band_info:
return html.H3('Invalid artist id.')
members = band_member_dict(band_info)
if members:
band_start_date, band_end_date = start_and_end_date(band_info)
band_member_names = set([member['Resource'] for member in members])
fig_colors = variable_color_scale(len(band_member_names))
fig_colors_dict = dict(zip(band_member_names, fig_colors))
fig = ff.create_gantt(df=members, colors=fig_colors_dict, group_tasks=True,
index_col='Resource', title=band_info['name'])
fig = customize_gantt(fig,band_start_date,band_end_date)
return dcc.Graph(
id='gantt',
figure=fig,
config={'displayModeBar': False}
)
else:
return html.H3("No band member information available.")
def customize_gantt(gantt_fig,start_date,end_date):
'''Make changes to figure generated from create_gantt()'''
del gantt_fig['layout']['xaxis']['rangeselector']
del gantt_fig['layout']['height']
del gantt_fig['layout']['width']
gantt_fig['layout']['autosize'] = True
gantt_fig['layout']['xaxis']['autorange'] = True
gantt_fig['layout']['yaxis']['autorange'] = True
gantt_fig['layout']['margin'] = dict(r=10,t=100,b=50,l=150)
return gantt_fig
def start_and_end_date(band_info):
band_start_date = band_info['life-span']['begin']
if band_info['life-span']['ended'] == True:
band_end_date = band_info['life-span']['end']
else:
band_end_date = datetime.now()
return (band_start_date,band_end_date)
def band_member_dict(band_info):
'''returns a dict of band member info in a format useful for generating a
plotly Gantt chart'''
band_start_date, band_end_date = start_and_end_date(band_info)
members = []
for relation in band_info['relations']:
if relation['type'] in ['member of band', 'instrumental supporting musician']:
member = relation['artist']['name']
start = relation['begin'] if relation['begin'] else band_start_date
finish = relation['end'] if relation['end'] else band_end_date
members.append({
"Task": member,
"Start": start,
"Finish": finish,
"Resource": member
})
return members
def variable_color_scale(band_size):
'''returns a color scale that accomodates bands smaller than 2 members and
larger than 12 members'''
colors = cl.scales['12']['qual']['Paired'] * (band_size // 12 + 1)
colors = colors[: 12 - (band_size % 12 + 1):-1]
return colors
if __name__ == '__main__':
app.run_server(debug=True)