|
26 | 26 | "source": [
|
27 | 27 | "1. Load data from file\n",
|
28 | 28 | "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", |
32 | 34 | "\n",
|
33 | 35 | "\n",
|
34 | 36 | "We will learn how to...\n",
|
|
54 | 56 | "execution_count": null,
|
55 | 57 | "id": "d8a59617-f05b-48fc-afcd-79f534a02e96",
|
56 | 58 | "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", |
60 | 78 | "\n",
|
61 | 79 | "main_widget"
|
62 | 80 | ]
|
|
78 | 96 | "outputs": [],
|
79 | 97 | "source": [
|
80 | 98 | "#| export\n",
|
| 99 | + "# %answer key/dashboard/widgets.py 6\n", |
| 100 | + "\n", |
81 | 101 | "import pandas as pd\n",
|
82 | 102 | "import os\n",
|
83 | 103 | "from matplotlib import pyplot as plt\n",
|
84 | 104 | "from scipy.signal import savgol_filter\n",
|
85 |
| - "import ipywidgets as widgets # add import statement" |
| 105 | + "# add import statement" |
86 | 106 | ]
|
87 | 107 | },
|
88 | 108 | {
|
|
174 | 194 | "outputs": [],
|
175 | 195 | "source": [
|
176 | 196 | "#| 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'" |
179 | 201 | ]
|
180 | 202 | },
|
181 | 203 | {
|
|
239 | 261 | "outputs": [],
|
240 | 262 | "source": [
|
241 | 263 | "#| 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)]" |
243 | 267 | ]
|
244 | 268 | },
|
245 | 269 | {
|
|
402 | 426 | "metadata": {},
|
403 | 427 | "outputs": [],
|
404 | 428 | "source": [
|
| 429 | + "%%exception\n", |
| 430 | + "\n", |
405 | 431 | "year_range.observe(on_range_change)\n",
|
406 | 432 | "year_range.value = (1993, 1996)"
|
407 | 433 | ]
|
|
432 | 458 | "outputs": [],
|
433 | 459 | "source": [
|
434 | 460 | "#| 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)" |
436 | 464 | ]
|
437 | 465 | },
|
438 | 466 | {
|
|
510 | 538 | "outputs": [],
|
511 | 539 | "source": [
|
512 | 540 | "#| export\n",
|
| 541 | + "\n", |
513 | 542 | "def display_selected_data(change): \n",
|
514 | 543 | " selected_data_output.clear_output(wait=True)\n",
|
515 | 544 | " with selected_data_output: \n",
|
|
685 | 714 | "metadata": {},
|
686 | 715 | "outputs": [],
|
687 | 716 | "source": [
|
| 717 | + "# %answer key/04/01.py\n", |
| 718 | + "\n", |
688 | 719 | "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])]" |
692 | 723 | ]
|
693 | 724 | },
|
694 | 725 | {
|
|
736 | 767 | "outputs": [],
|
737 | 768 | "source": [
|
738 | 769 | "#| export\n",
|
| 770 | + "# %answer key/dashboard/widgets.py 70\n", |
| 771 | + "\n", |
739 | 772 | "def on_poly_order_change(change):\n",
|
740 | 773 | " 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", |
742 | 775 | " selected = original_data[(original_data['Year'] >= year_range.value[0]) & (original_data['Year'] <= year_range.value[1])]"
|
743 | 776 | ]
|
744 | 777 | },
|
|
801 | 834 | "outputs": [],
|
802 | 835 | "source": [
|
803 | 836 | "#| export\n",
|
| 837 | + "# %answer key/dashboard/widgets.py 76\n", |
| 838 | + "\n", |
804 | 839 | "def on_window_size_change(change):\n",
|
805 | 840 | " 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", |
807 | 842 | " original_data['Savitzky-Golay'] = savgol_filter(original_data['Temperature'], change['new'], poly_order.value)\n",
|
808 | 843 | " selected = original_data[(original_data['Year'] >= year_range.value[0]) & (original_data['Year'] <= year_range.value[1])]"
|
809 | 844 | ]
|
|
893 | 928 | "metadata": {},
|
894 | 929 | "outputs": [],
|
895 | 930 | "source": [
|
| 931 | + "# %answer key/04/02.py\n", |
| 932 | + "\n", |
896 | 933 | "plt.xlabel('Year')\n",
|
897 | 934 | "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", |
900 | 937 | "plt.show()"
|
901 | 938 | ]
|
902 | 939 | },
|
|
924 | 961 | "outputs": [],
|
925 | 962 | "source": [
|
926 | 963 | "#| 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", |
928 | 967 | "plot_output"
|
929 | 968 | ]
|
930 | 969 | },
|
|
960 | 999 | "outputs": [],
|
961 | 1000 | "source": [
|
962 | 1001 | "#| 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", |
967 | 1007 | " 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", |
969 | 1009 | " plt.plot(selected['Year'], selected['Savitzky-Golay']) \n",
|
970 | 1010 | " plt.show() "
|
971 | 1011 | ]
|
|
1066 | 1106 | "outputs": [],
|
1067 | 1107 | "source": [
|
1068 | 1108 | "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')" |
1071 | 1110 | ]
|
1072 | 1111 | },
|
1073 | 1112 | {
|
|
0 commit comments