Skip to content

Commit dbfcfc4

Browse files
committed
Finalize 04_widgets
1 parent bc535fc commit dbfcfc4

File tree

5 files changed

+106
-163
lines changed

5 files changed

+106
-163
lines changed

04_widgets.ipynb

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
"source": [
2727
"1. Load data from file\n",
2828
"2. Use a slider widget to select and display selected range of years\n",
29-
" - Use the slider value to update the `selected` data\n",
30-
" - Update the `selected` data *every time* the range is modified\n",
31-
"4. Update selected dataframe with new range\n",
29+
" - Use the slider value to update the `selected` data\n",
30+
" - Update the `selected` data *every time* the range is modified\n",
31+
"3. Update selected dataframe with new range\n",
32+
"4. Add column for Savitzky-Golay filter\n",
33+
"5. Visualizae data with plot\n",
3234
"\n",
3335
"\n",
3436
"We will learn how to...\n",
@@ -54,9 +56,25 @@
5456
"execution_count": null,
5557
"id": "d8a59617-f05b-48fc-afcd-79f534a02e96",
5658
"metadata": {},
57-
"outputs": [],
58-
"source": [
59-
"from dashboard.main import main_widget\n",
59+
"outputs": [
60+
{
61+
"data": {
62+
"application/vnd.jupyter.widget-view+json": {
63+
"model_id": "361e3273c8784ac59db7b491fe95cd56",
64+
"version_major": 2,
65+
"version_minor": 0
66+
},
67+
"text/plain": [
68+
"HBox(children=(VBox(children=(HTML(value='\\n<p><b>Curve Smoothing</b>\\nThis tool is for smoothing and selectin…"
69+
]
70+
},
71+
"execution_count": null,
72+
"metadata": {},
73+
"output_type": "execute_result"
74+
}
75+
],
76+
"source": [
77+
"from key.dashboard.main import main_widget\n",
6078
"\n",
6179
"main_widget"
6280
]
@@ -78,11 +96,13 @@
7896
"outputs": [],
7997
"source": [
8098
"#| export\n",
99+
"# %answer key/dashboard/widgets.py 6\n",
100+
"\n",
81101
"import pandas as pd\n",
82102
"import os\n",
83103
"from matplotlib import pyplot as plt\n",
84104
"from scipy.signal import savgol_filter\n",
85-
"import ipywidgets as widgets # add import statement"
105+
"# add import statement"
86106
]
87107
},
88108
{
@@ -174,8 +194,10 @@
174194
"outputs": [],
175195
"source": [
176196
"#| export\n",
177-
"year_range.max = max(original_data['Year']) # set the 'max' attribute of the slider to the minimum year of the our data \n",
178-
"year_range.min = min(original_data['Year']) # and let's do the same for 'min'"
197+
"# %answer key/dashboard/widgets.py 15\n",
198+
"\n",
199+
"# set the 'max' attribute of the slider to the minimum year of the our data \n",
200+
"# and let's do the same for 'min'"
179201
]
180202
},
181203
{
@@ -239,7 +261,9 @@
239261
"outputs": [],
240262
"source": [
241263
"#| export\n",
242-
"selected = original_data[(original_data['Year'] >= year_range.value[0]) & (original_data['Year'] <= year_range.value[1])] # selected = original_data[(original_data['Year'] >= from_year) & (original_data['Year'] <= to_year)]"
264+
"# %answer key/dashboard/widgets.py 21\n",
265+
"\n",
266+
"# selected = original_data[(original_data['Year'] >= from_year) & (original_data['Year'] <= to_year)]"
243267
]
244268
},
245269
{
@@ -402,6 +426,8 @@
402426
"metadata": {},
403427
"outputs": [],
404428
"source": [
429+
"%%exception\n",
430+
"\n",
405431
"year_range.observe(on_range_change)\n",
406432
"year_range.value = (1993, 1996)"
407433
]
@@ -432,7 +458,9 @@
432458
"outputs": [],
433459
"source": [
434460
"#| export\n",
435-
"year_range.observe(on_range_change, 'value') # year_range.observe(on_range_change)"
461+
"# %answer key/dashboard/widgets.py 40\n",
462+
"\n",
463+
"year_range.observe(on_range_change)"
436464
]
437465
},
438466
{
@@ -510,6 +538,7 @@
510538
"outputs": [],
511539
"source": [
512540
"#| export\n",
541+
"\n",
513542
"def display_selected_data(change): \n",
514543
" selected_data_output.clear_output(wait=True)\n",
515544
" with selected_data_output: \n",
@@ -685,10 +714,12 @@
685714
"metadata": {},
686715
"outputs": [],
687716
"source": [
717+
"# %answer key/04/01.py\n",
718+
"\n",
688719
"def on_window_size_change(change):\n",
689-
" global original_data, selected\n",
690-
" original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], change['new'], poly_order.value) # original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], window_size, poly_order)\n",
691-
" selected = original_data[(original_data['Year'] >= year_range.value[0]) & (original_data['Year'] <= year_range.value[1])]"
720+
" # \n",
721+
" # original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], window_size, poly_order)\n",
722+
" selected = original_data[(original_data['Year'] >= year_range.value[0]) & (original_data['Year'] <= year_range.value[1])]"
692723
]
693724
},
694725
{
@@ -736,9 +767,11 @@
736767
"outputs": [],
737768
"source": [
738769
"#| export\n",
770+
"# %answer key/dashboard/widgets.py 70\n",
771+
"\n",
739772
"def on_poly_order_change(change):\n",
740773
" global original_data, selected\n",
741-
" original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], window_size.value, change['new']) # original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], window_size, poly_order)\n",
774+
" # original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], window_size, poly_order)\n",
742775
" selected = original_data[(original_data['Year'] >= year_range.value[0]) & (original_data['Year'] <= year_range.value[1])]"
743776
]
744777
},
@@ -801,9 +834,11 @@
801834
"outputs": [],
802835
"source": [
803836
"#| export\n",
837+
"# %answer key/dashboard/widgets.py 76\n",
838+
"\n",
804839
"def on_window_size_change(change):\n",
805840
" global original_data, selected, poly_order\n",
806-
" poly_order.max = min(10, change['new'] - 1) # change the maximum of the poly_order widget\n",
841+
" # change the maximum of the poly_order widget\n",
807842
" original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], change['new'], poly_order.value)\n",
808843
" selected = original_data[(original_data['Year'] >= year_range.value[0]) & (original_data['Year'] <= year_range.value[1])]"
809844
]
@@ -893,10 +928,12 @@
893928
"metadata": {},
894929
"outputs": [],
895930
"source": [
931+
"# %answer key/04/02.py\n",
932+
"\n",
896933
"plt.xlabel('Year')\n",
897934
"plt.ylabel('Temperature')\n",
898-
"plt.plot(selected['Year'], selected['Temperature']) # plt.plot(selected_range['Year'], selected_range['Temperature'])\n",
899-
"plt.plot(selected['Year'], selected['Savitzky-Golay']) \n",
935+
"# plt.plot(selected_range['Year'], selected_range['Temperature'])\n",
936+
"# plt.plot(selected_range['Year'], selected_range['Savitzky-Golay'])\n",
900937
"plt.show()"
901938
]
902939
},
@@ -924,7 +961,9 @@
924961
"outputs": [],
925962
"source": [
926963
"#| export\n",
927-
"plot_output = widgets.Output() # create an output widget called selected_data_output\n",
964+
"# %answer key/dashboard/widgets.py 86\n",
965+
"\n",
966+
"# create an output widget called plot_output\n",
928967
"plot_output"
929968
]
930969
},
@@ -960,12 +999,13 @@
960999
"outputs": [],
9611000
"source": [
9621001
"#| export\n",
963-
"def display_plot(change): # def display_selected_data(change): \n",
964-
" plot_output.clear_output(wait=True) #selected_data_output.clear_output(wait=True)\n",
965-
" with plot_output: # with selected_data_output: \n",
966-
" plt.xlabel('Year') # display(selected)\n",
1002+
"\n",
1003+
"def display_plot(change): \n",
1004+
" plot_output.clear_output(wait=True) \n",
1005+
" with plot_output: \n",
1006+
" plt.xlabel('Year') \n",
9671007
" plt.ylabel('Temperature') \n",
968-
" plt.plot(selected['Year'], selected['Temperature']) # plt.plot(selected_range['Year'], selected_range['Temperature'])\n",
1008+
" plt.plot(selected['Year'], selected['Temperature']) \n",
9691009
" plt.plot(selected['Year'], selected['Savitzky-Golay']) \n",
9701010
" plt.show() "
9711011
]
@@ -1066,8 +1106,7 @@
10661106
"outputs": [],
10671107
"source": [
10681108
"from nbdev.export import nb_export\n",
1069-
"nb_export('04_widgets.ipynb', 'dashboard')\n",
1070-
"nb_export('04_widgets.ipynb', 'key/dashboard')"
1109+
"nb_export('04_widgets.ipynb', 'dashboard')"
10711110
]
10721111
},
10731112
{

dashboard/widgets.py

Lines changed: 0 additions & 108 deletions
This file was deleted.

key/04/01.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
def on_window_size_change(change): # def on_window_size_change(change):
3+
global original_data, selected # global original_data, selected
4+
original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], change['new'], poly_order.value) # original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], window_size, poly_order)
5+
selected = original_data[(original_data['Year'] >= year_range.value[0]) & (original_data['Year'] <= year_range.value[1])] # selected = original_data[(original_data['Year'] >= year_range.value[0]) & (original_data['Year'] <= year_range.value[1])]

key/04/02.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
3+
plt.xlabel('Year')
4+
plt.ylabel('Temperature')
5+
plt.plot(selected['Year'], selected['Temperature']) # plt.plot(selected_range['Year'], selected_range['Temperature'])
6+
plt.plot(selected['Year'], selected['Savitzky-Golay']) # plt.plot(selected_range['Year'], selected_range['Savitzky-Golay'])
7+
plt.show()

0 commit comments

Comments
 (0)