Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # dash imports
- import dash
- import dash_core_components as dcc
- import dash_html_components as html
- from dash.dependencies import Input, Output, State
- from pdb import set_trace
- # general imports
- from datetime import datetime
- from GeoJson import dv_regions
- from sql_helper import get_div_dataset
- from vis_helper import get_div_map
- import logging
- logging.basicConfig(level='INFO')
- # load data on startup before serving the app
- # this will be the source of truth
- scopes = ['division', 'county', 'state']
- min_year = 2010
- start_time = datetime.now()
- logging.info('{} building division dataset'.format(datetime.now()))
- div_data = get_div_dataset(min_year=min_year)
- logging.info('{} finished getting dataset. time spent: {}'.format(
- datetime.now(), datetime.now() - start_time) +
- ', gathering geojsons for region selection')
- start_time = datetime.now()
- div_regions = dv_regions()
- logging.info('{} finished gathering geojsons, time spent: {}'.format(
- datetime.now(), datetime.now() - start_time))
- # start app
- app = dash.Dash(__name__)
- app.layout = html.Div([dcc.RadioItems(id='data-scope-radio',
- options=[{'label': k,
- 'value': k} for k in scopes],
- value=scopes[0]),
- dcc.Dropdown(id='data-region-dropdown',
- value='northwest',
- options={'label': 'northwest',
- 'value': 'northwest'}),
- dcc.Dropdown(id='data-type'),
- dcc.Dropdown(id='year-dd'),
- dcc.Dropdown(id='month-dd'),
- html.Button(id='submit-selectors-state',
- n_clicks=0,
- children='Submit'),
- dcc.Graph(id='display-current-selections')])
- figure_out = Output('display-current-selections', 'figure')
- selector_outs = [
- Output('data-region-dropdown', 'options'),
- Output('data-region-dropdown', 'value'),
- Output('data-type', 'options'),
- Output('data-type', 'value'),
- Output('year-dd', 'options'),
- Output('year-dd', 'value'),
- Output('month-dd', 'options'),
- Output('month-dd', 'value')]
- selector_in = [
- Input('data-scope-radio', 'value')]
- selector_states = [
- State('data-scope-radio', 'value'),
- State('data-region-dropdown', 'value'),
- State('data-type', 'value'),
- State('year-dd', 'value'),
- State('month-dd', 'value')]
- button_in = [
- Input('submit-selectors-state', 'n_clicks')]
- months = [{'label': mo_name,
- 'value': mo} for mo_name,
- mo in zip(['Jan',
- 'Feb',
- 'Mar',
- 'Apr',
- 'May',
- 'Jun',
- 'Jul',
- 'Aug',
- 'Sept',
- 'Oct',
- 'Nov',
- 'Dec'],
- range(1,
- 13))]
- # add scope and selectors
- @app.callback(selector_outs, selector_in)
- def add_selectors(data_scope):
- if data_scope in scopes:
- region_opts = [{'label': k, 'value': k}
- for k in div_regions.regions.keys()]
- region_val = region_opts[0]['value']
- dtype_opts = [{'label': k, 'value': k}
- for k in div_regions.data_types.values()]
- dtype_val = dtype_opts[0]['value']
- yr_opts = [{'label': str(k), 'value': k} for k in (
- range(div_data.YEAR.min(), div_data.YEAR.max() + 1))]
- yr_val = yr_opts[-1]['value']
- mo_opts = months
- mo_val = mo_opts[0]['value']
- return region_opts, region_val, dtype_opts, dtype_val, yr_opts, yr_val, mo_opts, mo_val
- # submit changes the plot
- @app.callback(figure_out, button_in, selector_states)
- def submit_selections(
- n_clicks,
- data_scope,
- data_region,
- data_type,
- year,
- month):
- if 'submit-selectors-state' in [p['prop_id']
- for p in dash.callback_context.triggered][0]:
- if data_scope in scopes:
- state_list = div_regions.regions[data_region]['states']
- selection = div_data[
- (div_data['STATE'].isin(state_list)) &
- (div_data['MEASUREMENTTYPE'] == data_type) &
- (div_data['YEAR'] == year) &
- (div_data['MONTH'] == month)].index
- filtered_data = div_data.loc[selection]
- fig = get_div_map(
- filtered_data,
- div_regions,
- data_region,
- data_type)
- set_trace()
- return fig
- if __name__ == '__main__':
- app.run_server()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement