Re: Untitled

From Toxic Peccary, 3 Months ago, written in Plain Text, viewed 66 times. This paste is a reply to Untitled from Lousy Sheep - view diff
URL http://codebin.org/view/2d23fe19 Embed
Download Paste or View Raw
  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.  
  8. import plotly.graph_objs as go
  9.  
  10. import pandas as pd
  11.  
  12. # задаём данные для отрисовки
  13. from sqlalchemy import create_engine
  14.  
  15. # пример подключения к базе данных для Postresql
  16. #db_config = {'user': 'my_user',
  17. #             'pwd': 'my_user_password',
  18. #             'host': 'localhost',
  19. #             'port': 5432,
  20. #             'db': 'games'}
  21. #engine = create_engine('postgresql://{}:{}@{}:{}/{}'.format(db_config['user'],
  22. #                                                            db_config['pwd'],
  23. #                                                            db_config['host'],
  24. #                                                            db_config['port'],
  25. #                                                            db_config['db']))
  26. # пример подключения к базе данных для Sqlite
  27. engine = create_engine('sqlite:////db/games.db', echo = False)
  28.  
  29. # получаем сырые данные
  30. query = '''
  31.             SELECT * FROM data_raw
  32.         '''
  33. games_raw = pd.io.sql.read_sql(query, con = engine)
  34.  
  35. # преобразуем типы
  36. games_raw['year_of_release'] = pd.to_datetime(games_raw['year_of_release'])
  37. columns = ['na_players', 'eu_players', 'jp_players', 'other_players']
  38. for column in columns: games_raw[column] = pd.to_numeric(games_raw[column], errors = 'coerce')
  39. games_raw['total'] = games_raw[['na_players', 'eu_players', 'jp_players', 'other_players']].sum(axis = 1)
  40.  
  41. # формируем графики для отрисовки
  42. data = []
  43. for genre in games_raw.genre.unique():
  44.     current = games_raw.query('genre == @genre')
  45.     data += [go.Box(y = current['total'], # напишите код
  46.                     name = genre)]
  47.  
  48. # задаём лейаут
  49. external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
  50. app = dash.Dash(__name__, external_stylesheets=external_stylesheets,compress=False)
  51. app.layout = html.Div(children=[  
  52.    
  53.     # формируем заголовок тегом HTML
  54.     html.H1(children = 'Продажи игр по жанрам'),
  55.  
  56.     dcc.Graph(
  57.         figure = {'data': data,
  58.                   'layout': go.Layout(xaxis = {'title': 'Жанр'},
  59.                                       yaxis = {'title': 'Продажи'})
  60.                  },
  61.         id = 'games_by_genre' # напишите код
  62.     ),        
  63.  
  64. ])
  65.  
  66. # описываем логику дашборда
  67. if __name__ == '__main__':
  68.     app.run_server(host='0.0.0.0', port=3000)
  69.  

Reply to "Re: Untitled"

Here you can reply to the paste above