Advertisement
Guest User

Untitled

a guest
Jul 24th, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. # dash imports
  2. import dash
  3. import dash_core_components as dcc
  4. import dash_html_components as html
  5. from dash.dependencies import Input, Output, State
  6. from pdb import set_trace
  7.  
  8. # general imports
  9. from datetime import datetime
  10. from GeoJson import dv_regions
  11. from sql_helper import get_div_dataset
  12. from vis_helper import get_div_map
  13. import logging
  14. logging.basicConfig(level='INFO')
  15.  
  16. # load data on startup before serving the app
  17. # this will be the source of truth
  18. scopes = ['division', 'county', 'state']
  19. min_year = 2010
  20. start_time = datetime.now()
  21. logging.info('{} building division dataset'.format(datetime.now()))
  22. div_data = get_div_dataset(min_year=min_year)
  23. logging.info('{} finished getting dataset. time spent: {}'.format(
  24. datetime.now(), datetime.now() - start_time) +
  25. ', gathering geojsons for region selection')
  26. start_time = datetime.now()
  27. div_regions = dv_regions()
  28. logging.info('{} finished gathering geojsons, time spent: {}'.format(
  29. datetime.now(), datetime.now() - start_time))
  30.  
  31.  
  32. # start app
  33. app = dash.Dash(__name__)
  34.  
  35. app.layout = html.Div([dcc.RadioItems(id='data-scope-radio',
  36. options=[{'label': k,
  37. 'value': k} for k in scopes],
  38. value=scopes[0]),
  39. dcc.Dropdown(id='data-region-dropdown',
  40. value='northwest',
  41. options={'label': 'northwest',
  42. 'value': 'northwest'}),
  43. dcc.Dropdown(id='data-type'),
  44. dcc.Dropdown(id='year-dd'),
  45. dcc.Dropdown(id='month-dd'),
  46. html.Button(id='submit-selectors-state',
  47. n_clicks=0,
  48. children='Submit'),
  49. dcc.Graph(id='display-current-selections')])
  50.  
  51. figure_out = Output('display-current-selections', 'figure')
  52. selector_outs = [
  53. Output('data-region-dropdown', 'options'),
  54. Output('data-region-dropdown', 'value'),
  55. Output('data-type', 'options'),
  56. Output('data-type', 'value'),
  57. Output('year-dd', 'options'),
  58. Output('year-dd', 'value'),
  59. Output('month-dd', 'options'),
  60. Output('month-dd', 'value')]
  61. selector_in = [
  62. Input('data-scope-radio', 'value')]
  63. selector_states = [
  64. State('data-scope-radio', 'value'),
  65. State('data-region-dropdown', 'value'),
  66. State('data-type', 'value'),
  67. State('year-dd', 'value'),
  68. State('month-dd', 'value')]
  69. button_in = [
  70. Input('submit-selectors-state', 'n_clicks')]
  71.  
  72.  
  73. months = [{'label': mo_name,
  74. 'value': mo} for mo_name,
  75. mo in zip(['Jan',
  76. 'Feb',
  77. 'Mar',
  78. 'Apr',
  79. 'May',
  80. 'Jun',
  81. 'Jul',
  82. 'Aug',
  83. 'Sept',
  84. 'Oct',
  85. 'Nov',
  86. 'Dec'],
  87. range(1,
  88. 13))]
  89.  
  90. # add scope and selectors
  91.  
  92.  
  93. @app.callback(selector_outs, selector_in)
  94. def add_selectors(data_scope):
  95. if data_scope in scopes:
  96. region_opts = [{'label': k, 'value': k}
  97. for k in div_regions.regions.keys()]
  98. region_val = region_opts[0]['value']
  99. dtype_opts = [{'label': k, 'value': k}
  100. for k in div_regions.data_types.values()]
  101. dtype_val = dtype_opts[0]['value']
  102. yr_opts = [{'label': str(k), 'value': k} for k in (
  103. range(div_data.YEAR.min(), div_data.YEAR.max() + 1))]
  104. yr_val = yr_opts[-1]['value']
  105. mo_opts = months
  106. mo_val = mo_opts[0]['value']
  107. return region_opts, region_val, dtype_opts, dtype_val, yr_opts, yr_val, mo_opts, mo_val
  108.  
  109. # submit changes the plot
  110.  
  111.  
  112. @app.callback(figure_out, button_in, selector_states)
  113. def submit_selections(
  114. n_clicks,
  115. data_scope,
  116. data_region,
  117. data_type,
  118. year,
  119. month):
  120. if 'submit-selectors-state' in [p['prop_id']
  121. for p in dash.callback_context.triggered][0]:
  122. if data_scope in scopes:
  123. state_list = div_regions.regions[data_region]['states']
  124. selection = div_data[
  125. (div_data['STATE'].isin(state_list)) &
  126. (div_data['MEASUREMENTTYPE'] == data_type) &
  127. (div_data['YEAR'] == year) &
  128. (div_data['MONTH'] == month)].index
  129. filtered_data = div_data.loc[selection]
  130. fig = get_div_map(
  131. filtered_data,
  132. div_regions,
  133. data_region,
  134. data_type)
  135. set_trace()
  136. return fig
  137.  
  138.  
  139. if __name__ == '__main__':
  140. app.run_server()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement