marth2king

something went wrong error

Sep 1st, 2020
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import dash
  5. import dash_core_components as dcc
  6. import dash_html_components as html
  7. from dash.dependencies import Input, Output
  8.  
  9. import plotly.graph_objs as go
  10.  
  11. from datetime import datetime
  12.  
  13. import pandas as pd
  14.  
  15. # defining the data to be displayed
  16. from sqlalchemy import create_engine
  17.  
  18. # sample code for connecting to the database with PostgreSQL
  19. # db_config = {'user': 'my_user',
  20. # 'pwd': 'my_user_password',
  21. # 'host': 'localhost',
  22. # 'port': 5432,
  23. # 'db': 'games'}
  24. # engine = create_engine('postgresql://{}:{}@{}:{}/{}'.format(db_config['user'],
  25. # db_config['pwd'],
  26. # db_config['host'],
  27. # db_config['port'],
  28. # db_config['db']))
  29. # sample code for connecting to the database with SQLite
  30. engine = create_engine('sqlite:////db/games.db', echo = False)
  31.  
  32. # obtaining raw data
  33. query = '''
  34. SELECT * FROM data_raw
  35. '''
  36. games_raw = pd.io.sql.read_sql(query, con = engine)
  37.  
  38. # converting types
  39. games_raw['year_of_release'] = pd.to_datetime(games_raw['year_of_release'])
  40. columns = ['na_players', 'eu_players', 'jp_players', 'other_players']
  41. for column in columns: games_raw[column] = pd.to_numeric(games_raw[column], errors = 'coerce')
  42.  
  43. # defining the data for the report
  44. games_grouped = (games_raw.groupby(['year_of_release', 'genre'])
  45. .agg({'name': 'nunique'})
  46. .reset_index()
  47. .rename(columns = {'name': 'games_launched'})
  48. )
  49.  
  50. # defining the layout
  51. external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
  52. app = dash.Dash(__name__, external_stylesheets=external_stylesheets,compress=False)
  53. app.layout = html.Div(children=[
  54.  
  55. # forming a header with an HTML tag
  56. html.H1(children = 'Games released by year'),
  57.  
  58. # selecting the time range
  59. html.Label('Time range:'),
  60. dcc.DatePickerRange(
  61. start_date = games_raw['year_of_release'].dt.date.min(),
  62. end_date = datetime(2016,1,1).strftime('%Y-%m-%d'),
  63. display_format = 'YYYY-MM-DD',
  64. id = 'dt_selector',
  65. ),
  66.  
  67. # graph of games released by year
  68. dcc.Graph(
  69. id = 'launches_by_year'
  70. ),
  71.  
  72. ])
  73.  
  74. # dashboard logic
  75. @app.callback(
  76. [Output('launches_by_year', 'figure'),
  77. ],
  78. [Input('dt_selector', 'start_date'),
  79. Input('dt_selector', 'end_date'),
  80. ])
  81. def update_figures(start_date, end_date):
  82. # converting input parameters to the required types
  83. start_date = datetime.strptime(start_date, '%Y-%m-%d')
  84. end_date = datetime.strptime(end_date, '%Y-%m-%d')
  85.  
  86. # applying filters
  87. filtered_data = games_grouped.query('year_of_release >= @start_date and year_of_release <= @end_date')
  88.  
  89. # building graphs to be displayed
  90. data = []
  91. for genre in filtered_data['genre'].unique():
  92. data += [go.Scatter(x = filtered_data.query('genre == @genre')['year_of_release'],
  93. y = filtered_data.query('genre == @genre')['games_launched'],
  94. mode = 'lines',
  95. stackgroup = 'one',
  96. name = genre)]
  97.  
  98. # forming the result to be displayed
  99. return (
  100. {
  101. 'data': data,
  102. 'layout': go.Layout(xaxis = {'title': 'Date and time'},
  103. yaxis = {'title': 'Games released'})
  104. },
  105. )
  106.  
  107. # dashboard logic
  108. if __name__ == '__main__':
  109. app.run_server(host='0.0.0.0', port=3000)
Add Comment
Please, Sign In to add comment