Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import dash
  2. import dash_core_components as dcc
  3. import dash_html_components as html
  4. import pandas as pd
  5. import plotly.graph_objs as go
  6. from collections import Counter
  7. import pycountry
  8.  
  9. dataset = pd.read_csv('globalterrorismdb_0616dist.csv', encoding="ISO-8859-1",dtype = 'unicode')
  10.  
  11. app = dash.Dash()
  12.  
  13. app.title = 'Mijn eerste dash dashboard'
  14.  
  15. #titel = html.H1(children='Test Dashboard')
  16. #subtitel = html.Div(children='... gemaakt in Dash', style={'color': 'blue'})
  17.  
  18. slider = dcc.Slider(
  19. id='my-slider',
  20. min=[dataset['iyear'].min()],
  21. max=[dataset['iyear'].max()],
  22. step=1,
  23. value=[dataset['iyear'].min()]
  24. )
  25.  
  26. graph = dcc.Graph(id='graph')
  27.  
  28.  
  29. plot_style = {'width': '50%', 'display': 'inline-block'}
  30. plots = [html.Div(children=[graph], style=plot_style)]
  31.  
  32. app.layout = html.Div(children=[slider] + plots)
  33.  
  34. @app.callback(
  35. dash.dependencies.Output('graph', 'figure'),
  36. [dash.dependencies.Input('my-slider', 'value')])
  37. def update_output(value):
  38. value = value[0]
  39. #value = '1970'
  40. df = dataset[dataset['iyear'] == value]
  41.  
  42. countries = {}
  43. for country in pycountry.countries:
  44. countries[country.name] = country.alpha_3
  45.  
  46. codes = [countries[country] for country in df['country_txt'] if country in countries]
  47.  
  48. aantal = Counter(codes)
  49.  
  50. aantal_per_jaar = pd.DataFrame.from_dict(aantal, orient='index').reset_index()
  51. aantal_per_jaar = aantal_per_jaar.rename(columns={'index':'land', 0:value})
  52.  
  53. choropleth_map_trace = [go.Choropleth(
  54. locations = aantal_per_jaar['land'],
  55. z = aantal_per_jaar[value],
  56. autocolorscale = True,
  57. reversescale = False,
  58. marker = dict(
  59. line = dict (
  60. color = 'rgb(180,180,180)',
  61. width = 0.5
  62. )
  63. ),
  64. colorbar = dict(
  65. title = 'Terrorisme'
  66. )
  67. )
  68. ]
  69.  
  70. choropleth = dcc.Graph(
  71. id = 'Graph',
  72. figure = dict(
  73. data= choropleth_map_trace,
  74. layout = dict(
  75. title = 'Totaal aantal aanslagen over de jaren heen <br>' ,
  76. geo = dict(
  77. showframe = True,
  78. showcoastlines = True,
  79. projection = dict(
  80. type = 'Mercator'
  81. )
  82. )
  83. )
  84. )
  85. )
  86.  
  87.  
  88.  
  89. #app.layout = html.Div(children=[titel, subtitel, choropleth])
  90.  
  91. if __name__ == '__main__':
  92. app.run_server(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement