Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import dash
- import dash_core_components as dcc
- import dash_html_components as html
- import pandas as pd
- import plotly.graph_objs as go
- from collections import Counter
- import pycountry
- dataset = pd.read_csv('globalterrorismdb_0616dist.csv', encoding="ISO-8859-1",dtype = 'unicode')
- app = dash.Dash()
- app.title = 'Mijn eerste dash dashboard'
- #titel = html.H1(children='Test Dashboard')
- #subtitel = html.Div(children='... gemaakt in Dash', style={'color': 'blue'})
- slider = dcc.Slider(
- id='my-slider',
- min=[dataset['iyear'].min()],
- max=[dataset['iyear'].max()],
- step=1,
- value=[dataset['iyear'].min()]
- )
- graph = dcc.Graph(id='graph')
- plot_style = {'width': '50%', 'display': 'inline-block'}
- plots = [html.Div(children=[graph], style=plot_style)]
- app.layout = html.Div(children=[slider] + plots)
- @app.callback(
- dash.dependencies.Output('graph', 'figure'),
- [dash.dependencies.Input('my-slider', 'value')])
- def update_output(value):
- value = value[0]
- #value = '1970'
- df = dataset[dataset['iyear'] == value]
- countries = {}
- for country in pycountry.countries:
- countries[country.name] = country.alpha_3
- codes = [countries[country] for country in df['country_txt'] if country in countries]
- aantal = Counter(codes)
- aantal_per_jaar = pd.DataFrame.from_dict(aantal, orient='index').reset_index()
- aantal_per_jaar = aantal_per_jaar.rename(columns={'index':'land', 0:value})
- choropleth_map_trace = [go.Choropleth(
- locations = aantal_per_jaar['land'],
- z = aantal_per_jaar[value],
- autocolorscale = True,
- reversescale = False,
- marker = dict(
- line = dict (
- color = 'rgb(180,180,180)',
- width = 0.5
- )
- ),
- colorbar = dict(
- title = 'Terrorisme'
- )
- )
- ]
- choropleth = dcc.Graph(
- id = 'Graph',
- figure = dict(
- data= choropleth_map_trace,
- layout = dict(
- title = 'Totaal aantal aanslagen over de jaren heen <br>' ,
- geo = dict(
- showframe = True,
- showcoastlines = True,
- projection = dict(
- type = 'Mercator'
- )
- )
- )
- )
- )
- #app.layout = html.Div(children=[titel, subtitel, choropleth])
- if __name__ == '__main__':
- app.run_server(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement