1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import dash import dash_core_components as dcc import dash_html_components as html import plotly.express as px from dash.dependencies import Input, Output
tips = px.data.tips()
app = dash.Dash(__name__)
app.layout = html.Div( [ html.H3(id='title', style={'textAlign':'center'}), html.Div( [ html.Div( [ html.H4('曜日選択'), dcc.Dropdown( id='day_selector', options=[ {'value':d, 'label':d} for d in tips.day.unique() ], multi=True, value=['Thur', 'Fri', 'Sat', 'Sun'] ) ] ), html.Div( [ html.H4('グラフ選択'), dcc.Dropdown( id='graph_selector', options=[ {'value':'bar', 'label':'bar'}, {'value':'scatter', 'label':'scatter'} ], value='bar', ) ] ) ] ), dcc.Graph(id='app-graph') ] )
@app.callback( Output('title', 'children'), Output('app-graph', 'figure'), Input('day_selector', 'value'), Input('graph_selector', 'value'), ) def update_graph(selected_days, selected_graph): selected_df = tips[tips['day'].isin(selected_days)] if selected_graph == 'bar': title = ('曜日ごとの売り上げ(棒グラフ)',) figure = px.bar(selected_df, x='day', y='total_bill', height=500) else: title = ('テーブルごとデータ(散布図)',) figure = px.scatter(selected_df, x='total_bill', y='tip', color='smoker', height=500) return title, figure
if __name__ == '__main__': app.run_server(debug=True)
|