Skip to content

Commit fe1fa56

Browse files
committed
update
1 parent ef605f4 commit fe1fa56

File tree

6 files changed

+4992
-95
lines changed

6 files changed

+4992
-95
lines changed

hands_on_07/Untitled1.ipynb

Lines changed: 4795 additions & 0 deletions
Large diffs are not rendered by default.

hands_on_07/app2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import dash
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
import pandas as pd
5+
import plotly.express as px
6+
7+
from dash.dependencies import Input, Output
8+
9+
df = pd.read_csv('https://www.customs.go.jp/toukei/shinbun/trade-st/timeseries_202005.csv', header=2, parse_dates=['Years/Months']).dropna()
10+
df['balance'] = df['Exp-Total'] - df['Imp-Total']
11+
df['month'] = df['Years/Months'].apply(lambda x: x.month)
12+
13+
app = dash.Dash(__name__)
14+
15+
16+
app.layout = html.Div([
17+
18+
html.H1('輸出入'),
19+
20+
dcc.Dropdown(
21+
id='mydropdown',
22+
options = [{'value': m, 'label': m} for m in df['month'].unique()],
23+
value=[5],
24+
multi=True
25+
),
26+
27+
dcc.Graph(id='mygraph'),
28+
29+
dcc.Graph(id='mygraph2'),
30+
31+
32+
])
33+
@app.callback(
34+
[Output('mygraph', 'figure'),
35+
Output('mygraph2', 'figure')],
36+
[Input('mydropdown', 'value')]
37+
)
38+
def update_graph(selected_value):
39+
dff = df[df['month'].isin(selected_value)]
40+
return px.line(dff, x='Years/Months', y=['Exp-Total', 'Imp-Total']), px.bar(dff, x='Years/Months', y='balance')
41+
42+
43+
app.run_server(debug=True)
44+

hands_on_07/app3.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import dash
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
import plotly.express as px
5+
6+
from dash.dependencies import Input, Output
7+
8+
9+
gapminder = px.data.gapminder()
10+
11+
app = dash.Dash()
12+
13+
app.layout = html.Div([
14+
15+
dcc.Dropdown(id='dropdown',
16+
options=[{'label': c, 'value': c} for c in gapminder.country.unique()],
17+
value = 'Japan'
18+
19+
20+
),
21+
dcc.Graph(id='graph')
22+
23+
24+
])
25+
@app.callback(
26+
Output('graph', 'figure'),
27+
[Input('dropdown', 'value')]
28+
)
29+
def update_graph(selected_value):
30+
gap = gapminder[gapminder['country'] == selected_value]
31+
return px.scatter(gap, x='year', y='gdpPercap')
32+
33+
app.run_server(debug=True)
34+
35+

hands_on_08/hop.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
import dash
2-
import dash_core_components as dcc
3-
import dash_html_components as html
4-
import plotly.express as px
1+
import dash
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
import plotly.express as px
55

66
graph_list = [px.line, px.scatter, px.bar]
77

88
app = dash.Dash(__name__)
99

10-
app.layout = html.Div([
11-
12-
dcc.Dropdown(
13-
options=[{'label': f'{type.__name__}', 'value': num} for num, type in enumerate(graph_list)],
14-
value = 0
15-
),
16-
17-
html.Button(
18-
"PUSHME"
19-
),
20-
21-
dcc.Graph()
22-
23-
24-
])
25-
26-
27-
if __name__ == '__main__':
10+
app.layout = html.Div(
11+
[
12+
dcc.Dropdown(
13+
options=[
14+
{"label": f"{type.__name__}", "value": num}
15+
for num, type in enumerate(graph_list)
16+
],
17+
value=0,
18+
),
19+
html.Button("PUSHME"),
20+
dcc.Graph(),
21+
]
22+
)
23+
24+
25+
if __name__ == "__main__":
2826
app.run_server(debug=True)

hands_on_08/jump.py

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,40 @@
1-
import dash
2-
import dash_core_components as dcc
3-
import dash_html_components as html
4-
import pandas as pd
5-
import plotly.express as px
1+
import dash
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
import pandas as pd
5+
import plotly.express as px
66
from dash.dependencies import Input, Output
77

8-
df = pd.read_csv('data\melt_zaisei.csv', index_col=0)
8+
df = pd.read_csv("data\melt_zaisei.csv", index_col=0)
99

1010
col_selector = df.columns[:-1]
1111

1212
app = dash.Dash(__name__)
1313

14-
app.layout = html.Div([
15-
16-
dcc.Dropdown(
17-
id = 'mydropdown',
18-
options = [{'label': c, 'value': c} for c in col_selector],
19-
value =['main_title', 'variable', 'title'],
20-
multi=True,
21-
clearable=False
22-
),
23-
dcc.Graph(
24-
id='mytree'
25-
),
26-
dcc.Graph(
27-
id='mysun',
28-
style={'height': 800}
29-
)
30-
31-
])
32-
33-
@app.callback(
34-
Output('mytree', 'figure'),
35-
[Input('mydropdown', 'value')]
14+
app.layout = html.Div(
15+
[
16+
dcc.Dropdown(
17+
id="mydropdown",
18+
options=[{"label": c, "value": c} for c in col_selector],
19+
value=["main_title", "variable", "title"],
20+
multi=True,
21+
clearable=False,
22+
),
23+
dcc.Graph(id="mytree"),
24+
dcc.Graph(id="mysun", style={"height": 800}),
25+
]
3626
)
27+
28+
29+
@app.callback(Output("mytree", "figure"), [Input("mydropdown", "value")])
3730
def make_tree(selected_values):
38-
return px.treemap(
39-
df,
40-
path = selected_values,
41-
values='value'
42-
)
43-
44-
@app.callback(
45-
Output('mysun', 'figure'),
46-
[Input('mydropdown', 'value')]
47-
)
31+
return px.treemap(df, path=selected_values, values="value")
32+
33+
34+
@app.callback(Output("mysun", "figure"), [Input("mydropdown", "value")])
4835
def make_tree(selected_values):
49-
return px.sunburst(
50-
df,
51-
path = selected_values,
52-
values='value'
53-
)
36+
return px.sunburst(df, path=selected_values, values="value")
5437

5538

56-
if __name__ == '__main__':
39+
if __name__ == "__main__":
5740
app.run_server(debug=True)
58-

hands_on_08/step.py

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,88 @@
1-
import dash
2-
import dash_core_components as dcc
3-
import dash_html_components as html
4-
import plotly.express as px
5-
6-
from dash.dependencies import Input, Output, State
1+
import dash
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
import plotly.express as px
5+
from dash.dependencies import MATCH, Input, Output, State
76

87
graph_list = [px.line, px.scatter, px.bar]
98

9+
gapminder = px.data.gapminder()
10+
1011
app = dash.Dash(__name__)
1112

12-
app.layout = html.Div([
13+
app.layout = html.Div(
14+
[
15+
### CALLBACK
16+
dcc.Dropdown(
17+
id="mydropdown",
18+
options=[
19+
{"label": f"{type.__name__}", "value": num}
20+
for num, type in enumerate(graph_list)
21+
],
22+
value=0,
23+
),
24+
html.Button(id="mybutton", children="PUSHME"),
25+
dcc.Graph(id="mygraph"),
26+
##### PATTERN MATCHING CALLBACK
27+
html.Button(id="mybutton2", children="ADDME"),
28+
html.Div(id="pmc", children=[]),
29+
],
30+
style={"padding": "3%"},
31+
)
32+
33+
### CALLBACK
34+
35+
36+
@app.callback(
37+
Output("mygraph", "figure"),
38+
[Input("mybutton", "n_clicks")],
39+
[State("mydropdown", "value")],
40+
)
41+
def update_graph(n_clicks, selected_value):
42+
return graph_list[selected_value](x=[1, 2, 3, 4, 5], y=[1, 2, 3, 4, 5])
43+
1344

14-
dcc.Dropdown(
15-
id='mydropdown',
16-
options=[{'label': f'{type.__name__}', 'value': num} for num, type in enumerate(graph_list)],
17-
value = 0
18-
),
45+
### PATTERN MATCHING CALLBACK
1946

20-
21-
html.Button(
22-
id='mybutton',
23-
children="PUSHME"
24-
),
2547

48+
@app.callback(
49+
Output("pmc", "children"),
50+
[Input("mybutton2", "n_clicks")],
51+
[State("pmc", "children")],
52+
prevent_initial_call=True,
53+
)
54+
def update_layout(n_clicks, exist_children):
2655

27-
dcc.Graph(
28-
id='mygraph'
56+
add_layout = html.Div(
57+
[
58+
dcc.Dropdown(
59+
id={"type": "pmcdropdown", "id": n_clicks},
60+
options=[{"value": c, "label": c} for c in gapminder.country.unique()],
61+
value=gapminder.country.unique()[n_clicks],
62+
),
63+
dcc.Graph(id={"type": "pmcgraph", "id": n_clicks}),
64+
]
2965
)
66+
exist_children.append(add_layout)
67+
return exist_children
3068

31-
])
3269

3370
@app.callback(
34-
Output('mygraph', 'figure'),
35-
[Input('mybutton', 'n_clicks')],
36-
[State('mydropdown', 'value')]
71+
Output({"type": "pmcgraph", "id": MATCH}, "figure"),
72+
[Input({"type": "pmcdropdown", "id": MATCH}, "value")],
3773
)
38-
def update_graph(n_clicks, selected_value):
39-
return graph_list[selected_value](
40-
x=[1,2,3,4,5],
41-
y=[1,2,3,4,5]
74+
def update_match_graph(selected_value):
75+
gap = gapminder[gapminder.country == selected_value]
76+
return px.scatter(
77+
gap,
78+
x="gdpPercap",
79+
y="lifeExp",
80+
size="pop",
81+
animation_frame="year",
82+
range_x=[gap.gdpPercap.min() * 0.8, gap.gdpPercap.max() * 1.2],
83+
range_y=[gap.lifeExp.min() * 0.8, gap.lifeExp.max() * 1.2],
4284
)
4385

44-
if __name__ == '__main__':
86+
87+
if __name__ == "__main__":
4588
app.run_server(debug=True)

0 commit comments

Comments
 (0)