Skip to content

Commit 0716580

Browse files
committed
update files
1 parent 5f06e3b commit 0716580

File tree

3 files changed

+360
-0
lines changed

3 files changed

+360
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 2
6+
}

hands_on_06/Untitled.ipynb

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from jupyter_dash import JupyterDash\n",
10+
"\n",
11+
"import dash \n",
12+
"import dash_core_components as dcc \n",
13+
"import dash_html_components as html \n",
14+
"from dash.dependencies import Input, Output\n",
15+
"\n",
16+
"import plotly.express as px"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"# Binder向け設定\n",
26+
"JupyterDash.infer_jupyter_proxy_config()"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 6,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"gapminder= px.data.gapminder()\n",
36+
"\n",
37+
"app = JupyterDash()\n",
38+
"\n",
39+
"app.layout = html.Div(\n",
40+
"[\n",
41+
" html.H1(\"HEllo Jupyter Dash !\"),\n",
42+
" \n",
43+
" dcc.Dropdown(id=\"xaxis\", options=[{\"value\": i, \"label\": i} for i in gapminder.columns[3:6]], value=\"gdpPercap\"),\n",
44+
" dcc.Dropdown(id=\"yaxis\", options=[{\"value\": i, \"label\": i} for i in gapminder.columns[3:6]], value=\"pop\"),\n",
45+
" \n",
46+
" dcc.Graph(id=\"graph\")\n",
47+
"]\n",
48+
")\n",
49+
"\n",
50+
"@app.callback(Output(\"graph\", \"figure\"), [Input(\"xaxis\", \"value\"), Input(\"yaxis\", \"value\")])\n",
51+
"def update_graph(xaxis_value, yaxis_value):\n",
52+
" return px.scatter(gapminder, x=xaxis_value, y=yaxis_value, color=\"continent\", log_x=True, log_y=True)\n",
53+
"\n",
54+
"# 出力はjuptyerlab / inline / external\n",
55+
"app.run_server(mode=\"jupyterlab\")"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 8,
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"data": {
65+
"text/html": [
66+
"<div>\n",
67+
"<style scoped>\n",
68+
" .dataframe tbody tr th:only-of-type {\n",
69+
" vertical-align: middle;\n",
70+
" }\n",
71+
"\n",
72+
" .dataframe tbody tr th {\n",
73+
" vertical-align: top;\n",
74+
" }\n",
75+
"\n",
76+
" .dataframe thead th {\n",
77+
" text-align: right;\n",
78+
" }\n",
79+
"</style>\n",
80+
"<table border=\"1\" class=\"dataframe\">\n",
81+
" <thead>\n",
82+
" <tr style=\"text-align: right;\">\n",
83+
" <th></th>\n",
84+
" <th>country</th>\n",
85+
" <th>continent</th>\n",
86+
" <th>year</th>\n",
87+
" <th>lifeExp</th>\n",
88+
" <th>pop</th>\n",
89+
" <th>gdpPercap</th>\n",
90+
" <th>iso_alpha</th>\n",
91+
" <th>iso_num</th>\n",
92+
" </tr>\n",
93+
" </thead>\n",
94+
" <tbody>\n",
95+
" <tr>\n",
96+
" <td>0</td>\n",
97+
" <td>Afghanistan</td>\n",
98+
" <td>Asia</td>\n",
99+
" <td>1952</td>\n",
100+
" <td>28.801</td>\n",
101+
" <td>8425333</td>\n",
102+
" <td>779.445314</td>\n",
103+
" <td>AFG</td>\n",
104+
" <td>4</td>\n",
105+
" </tr>\n",
106+
" <tr>\n",
107+
" <td>1</td>\n",
108+
" <td>Afghanistan</td>\n",
109+
" <td>Asia</td>\n",
110+
" <td>1957</td>\n",
111+
" <td>30.332</td>\n",
112+
" <td>9240934</td>\n",
113+
" <td>820.853030</td>\n",
114+
" <td>AFG</td>\n",
115+
" <td>4</td>\n",
116+
" </tr>\n",
117+
" <tr>\n",
118+
" <td>2</td>\n",
119+
" <td>Afghanistan</td>\n",
120+
" <td>Asia</td>\n",
121+
" <td>1962</td>\n",
122+
" <td>31.997</td>\n",
123+
" <td>10267083</td>\n",
124+
" <td>853.100710</td>\n",
125+
" <td>AFG</td>\n",
126+
" <td>4</td>\n",
127+
" </tr>\n",
128+
" <tr>\n",
129+
" <td>3</td>\n",
130+
" <td>Afghanistan</td>\n",
131+
" <td>Asia</td>\n",
132+
" <td>1967</td>\n",
133+
" <td>34.020</td>\n",
134+
" <td>11537966</td>\n",
135+
" <td>836.197138</td>\n",
136+
" <td>AFG</td>\n",
137+
" <td>4</td>\n",
138+
" </tr>\n",
139+
" <tr>\n",
140+
" <td>4</td>\n",
141+
" <td>Afghanistan</td>\n",
142+
" <td>Asia</td>\n",
143+
" <td>1972</td>\n",
144+
" <td>36.088</td>\n",
145+
" <td>13079460</td>\n",
146+
" <td>739.981106</td>\n",
147+
" <td>AFG</td>\n",
148+
" <td>4</td>\n",
149+
" </tr>\n",
150+
" <tr>\n",
151+
" <td>...</td>\n",
152+
" <td>...</td>\n",
153+
" <td>...</td>\n",
154+
" <td>...</td>\n",
155+
" <td>...</td>\n",
156+
" <td>...</td>\n",
157+
" <td>...</td>\n",
158+
" <td>...</td>\n",
159+
" <td>...</td>\n",
160+
" </tr>\n",
161+
" <tr>\n",
162+
" <td>1699</td>\n",
163+
" <td>Zimbabwe</td>\n",
164+
" <td>Africa</td>\n",
165+
" <td>1987</td>\n",
166+
" <td>62.351</td>\n",
167+
" <td>9216418</td>\n",
168+
" <td>706.157306</td>\n",
169+
" <td>ZWE</td>\n",
170+
" <td>716</td>\n",
171+
" </tr>\n",
172+
" <tr>\n",
173+
" <td>1700</td>\n",
174+
" <td>Zimbabwe</td>\n",
175+
" <td>Africa</td>\n",
176+
" <td>1992</td>\n",
177+
" <td>60.377</td>\n",
178+
" <td>10704340</td>\n",
179+
" <td>693.420786</td>\n",
180+
" <td>ZWE</td>\n",
181+
" <td>716</td>\n",
182+
" </tr>\n",
183+
" <tr>\n",
184+
" <td>1701</td>\n",
185+
" <td>Zimbabwe</td>\n",
186+
" <td>Africa</td>\n",
187+
" <td>1997</td>\n",
188+
" <td>46.809</td>\n",
189+
" <td>11404948</td>\n",
190+
" <td>792.449960</td>\n",
191+
" <td>ZWE</td>\n",
192+
" <td>716</td>\n",
193+
" </tr>\n",
194+
" <tr>\n",
195+
" <td>1702</td>\n",
196+
" <td>Zimbabwe</td>\n",
197+
" <td>Africa</td>\n",
198+
" <td>2002</td>\n",
199+
" <td>39.989</td>\n",
200+
" <td>11926563</td>\n",
201+
" <td>672.038623</td>\n",
202+
" <td>ZWE</td>\n",
203+
" <td>716</td>\n",
204+
" </tr>\n",
205+
" <tr>\n",
206+
" <td>1703</td>\n",
207+
" <td>Zimbabwe</td>\n",
208+
" <td>Africa</td>\n",
209+
" <td>2007</td>\n",
210+
" <td>43.487</td>\n",
211+
" <td>12311143</td>\n",
212+
" <td>469.709298</td>\n",
213+
" <td>ZWE</td>\n",
214+
" <td>716</td>\n",
215+
" </tr>\n",
216+
" </tbody>\n",
217+
"</table>\n",
218+
"<p>1704 rows × 8 columns</p>\n",
219+
"</div>"
220+
],
221+
"text/plain": [
222+
" country continent year lifeExp pop gdpPercap iso_alpha \\\n",
223+
"0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG \n",
224+
"1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG \n",
225+
"2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG \n",
226+
"3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG \n",
227+
"4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG \n",
228+
"... ... ... ... ... ... ... ... \n",
229+
"1699 Zimbabwe Africa 1987 62.351 9216418 706.157306 ZWE \n",
230+
"1700 Zimbabwe Africa 1992 60.377 10704340 693.420786 ZWE \n",
231+
"1701 Zimbabwe Africa 1997 46.809 11404948 792.449960 ZWE \n",
232+
"1702 Zimbabwe Africa 2002 39.989 11926563 672.038623 ZWE \n",
233+
"1703 Zimbabwe Africa 2007 43.487 12311143 469.709298 ZWE \n",
234+
"\n",
235+
" iso_num \n",
236+
"0 4 \n",
237+
"1 4 \n",
238+
"2 4 \n",
239+
"3 4 \n",
240+
"4 4 \n",
241+
"... ... \n",
242+
"1699 716 \n",
243+
"1700 716 \n",
244+
"1701 716 \n",
245+
"1702 716 \n",
246+
"1703 716 \n",
247+
"\n",
248+
"[1704 rows x 8 columns]"
249+
]
250+
},
251+
"execution_count": 8,
252+
"metadata": {},
253+
"output_type": "execute_result"
254+
}
255+
],
256+
"source": [
257+
"gapminder"
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": null,
263+
"metadata": {},
264+
"outputs": [],
265+
"source": []
266+
}
267+
],
268+
"metadata": {
269+
"kernelspec": {
270+
"display_name": "Python 3",
271+
"language": "python",
272+
"name": "python3"
273+
},
274+
"language_info": {
275+
"codemirror_mode": {
276+
"name": "ipython",
277+
"version": 3
278+
},
279+
"file_extension": ".py",
280+
"mimetype": "text/x-python",
281+
"name": "python",
282+
"nbconvert_exporter": "python",
283+
"pygments_lexer": "ipython3",
284+
"version": "3.7.4"
285+
}
286+
},
287+
"nbformat": 4,
288+
"nbformat_minor": 4
289+
}

hands_on_06/pmc_allsmaller.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import dash
2+
import dash_core_components as dcc
3+
import dash_html_components as html
4+
from dash.dependencies import Input, Output, State, MATCH, ALL, ALLSMALLER
5+
import pandas as pd
6+
7+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv')
8+
9+
app = dash.Dash(__name__, suppress_callback_exceptions=True)
10+
11+
app.layout = html.Div([
12+
html.Button('Add Filter', id='add-filter-ex3', n_clicks=0),
13+
html.Div(id='container-ex3', children=[]),
14+
])
15+
16+
@app.callback(
17+
Output('container-ex3', 'children'),
18+
[Input('add-filter-ex3', 'n_clicks')],
19+
[State('container-ex3', 'children')])
20+
def display_dropdowns(n_clicks, existing_children):
21+
existing_children.append(html.Div([
22+
dcc.Dropdown(
23+
id={
24+
'type': 'filter-dropdown-ex3',
25+
'index': f"test_{n_clicks}"
26+
},
27+
options=[{'label': i, 'value': i} for i in df['country'].unique()],
28+
value=df['country'].unique()[n_clicks]
29+
),
30+
html.Div(id={
31+
'type': 'output-ex3',
32+
'index': f"test_{n_clicks}"
33+
})
34+
]))
35+
return existing_children
36+
37+
38+
@app.callback(
39+
Output({'type': 'output-ex3', 'index': MATCH}, 'children'),
40+
[Input({'type': 'filter-dropdown-ex3', 'index': MATCH}, 'value'),
41+
Input({'type': 'filter-dropdown-ex3', 'index': ALLSMALLER}, 'value')],
42+
)
43+
def display_output(matching_value, previous_values):
44+
previous_values_in_reversed_order = previous_values[::-1]
45+
all_values = [matching_value] + previous_values_in_reversed_order
46+
47+
dff = df[df['country'].str.contains('|'.join(all_values))]
48+
avgLifeExp = dff['lifeExp'].mean()
49+
50+
# Return a slightly different string depending on number of values
51+
if len(all_values) == 1:
52+
return html.Div('{:.2f} is the life expectancy of {}'.format(
53+
avgLifeExp, matching_value
54+
))
55+
elif len(all_values) == 2:
56+
return html.Div('{:.2f} is the average life expectancy of {}'.format(
57+
avgLifeExp, ' and '.join(all_values)
58+
))
59+
else:
60+
return html.Div('{:.2f} is the average life expectancy of {}, and {}'.format(
61+
avgLifeExp, ', '.join(all_values[:-1]), all_values[-1]
62+
))
63+
64+
if __name__ == '__main__':
65+
app.run_server(debug=True)

0 commit comments

Comments
 (0)