Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import dash
- import dash_core_components as dcc
- import dash_html_components as html
- from dash.dependencies import Input, Output
- import plotly.graph_objs as go
- from datetime import datetime
- import pandas as pd
- # defining the data to be displayed
- from sqlalchemy import create_engine
- # sample code for connecting to the database with PostgreSQL
- # db_config = {'user': 'my_user',
- # 'pwd': 'my_user_password',
- # 'host': 'localhost',
- # 'port': 5432,
- # 'db': 'games'}
- # engine = create_engine('postgresql://{}:{}@{}:{}/{}'.format(db_config['user'],
- # db_config['pwd'],
- # db_config['host'],
- # db_config['port'],
- # db_config['db']))
- # sample code for connecting to the database with SQLite
- engine = create_engine('sqlite:////db/games.db', echo = False)
- # obtaining raw data
- query = '''
- SELECT * FROM data_raw
- '''
- games_raw = pd.io.sql.read_sql(query, con = engine)
- # converting types
- games_raw['year_of_release'] = pd.to_datetime(games_raw['year_of_release'])
- columns = ['na_players', 'eu_players', 'jp_players', 'other_players']
- for column in columns: games_raw[column] = pd.to_numeric(games_raw[column], errors = 'coerce')
- # defining the data for the report
- games_grouped = (games_raw.groupby(['year_of_release', 'genre'])
- .agg({'name': 'nunique'})
- .reset_index()
- .rename(columns = {'name': 'games_launched'})
- )
- # defining the layout
- external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
- app = dash.Dash(__name__, external_stylesheets=external_stylesheets,compress=False)
- app.layout = html.Div(children=[
- # forming a header with an HTML tag
- html.H1(children = 'Games released by year'),
- # selecting the time range
- html.Label('Time range:'),
- dcc.DatePickerRange(
- start_date = games_raw['year_of_release'].dt.date.min(),
- end_date = datetime(2016,1,1).strftime('%Y-%m-%d'),
- display_format = 'YYYY-MM-DD',
- id = 'dt_selector',
- ),
- # graph of games released by year
- dcc.Graph(
- id = 'launches_by_year'
- ),
- ])
- # dashboard logic
- @app.callback(
- [Output('launches_by_year', 'figure'),
- ],
- [Input('dt_selector', 'start_date'),
- Input('dt_selector', 'end_date'),
- ])
- def update_figures(start_date, end_date):
- # converting input parameters to the required types
- start_date = datetime.strptime(start_date, '%Y-%m-%d')
- end_date = datetime.strptime(end_date, '%Y-%m-%d')
- # applying filters
- filtered_data = games_grouped.query('year_of_release >= @start_date and year_of_release <= @end_date')
- # building graphs to be displayed
- data = []
- for genre in filtered_data['genre'].unique():
- data += [go.Scatter(x = filtered_data.query('genre == @genre')['year_of_release'],
- y = filtered_data.query('genre == @genre')['games_launched'],
- mode = 'lines',
- stackgroup = 'one',
- name = genre)]
- # forming the result to be displayed
- return (
- {
- 'data': data,
- 'layout': go.Layout(xaxis = {'title': 'Date and time'},
- yaxis = {'title': 'Games released'})
- },
- )
- # dashboard logic
- if __name__ == '__main__':
- app.run_server(host='0.0.0.0', port=3000)
Add Comment
Please, Sign In to add comment