Skip to content

Instantly share code, notes, and snippets.

@Calibr3-IO
Created July 12, 2020 14:18
Show Gist options
  • Save Calibr3-IO/95e548eb4486700f4edf4df45a527a68 to your computer and use it in GitHub Desktop.
Save Calibr3-IO/95e548eb4486700f4edf4df45a527a68 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": "<a><img src=\"https://ibm.box.com/shared/static/ugcqz6ohbvff804xp84y4kqnvvk3bq1g.png\" width=\"200\" align=\"center\"></a>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h1>US Economic Data Analysis</h1>\n<h2>Description</h2>\n"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Extracting essential data from a dataset and displaying it is a necessary part of data science; therefore individuals can make correct decisions based on the data. In this assignment, you will extract some essential economic indicators from some data, you will then display these economic indicators in a Dashboard. You can then share the dashboard via an URL.\n<p>\n<a href=\"https://en.wikipedia.org/wiki/Gross_domestic_product\"> Gross domestic product (GDP)</a> is a measure of the market value of all the final goods and services produced in a period. GDP is an indicator of how well the economy is doing. A drop in GDP indicates the economy is producing less; similarly an increase in GDP suggests the economy is performing better. In this lab, you will examine how changes in GDP impact the unemployment rate. You will take screen shots of every step, you will share the notebook and the URL pointing to the dashboard.</p>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h2>Table of Contents</h2>\n<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n <ul>\n <li><a href=\"#Section_1\"> Define a Function that Makes a Dashboard </a></li>\n <li><a href=\"#Section_2\">Question 1: Create a dataframe that contains the GDP data and display it</a> </li>\n <li><a href=\"#Section_3\">Question 2: Create a dataframe that contains the unemployment data and display it</a></li>\n <li><a href=\"#Section_4\">Question 3: Display a dataframe where unemployment was greater than 8.5%</a></li>\n <li><a href=\"#Section_5\">Question 4: Use the function make_dashboard to make a dashboard</a></li>\n <li><a href=\"#Section_6\"><b>(Optional not marked)</b> Save the dashboard on IBM cloud and display it</a></li>\n </ul>\n<p>\n Estimated Time Needed: <strong>180 min</strong></p>\n</div>\n\n<hr>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h2 id=\"Section_1\"> Define Function that Makes a Dashboard </h2>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "We will import the following libraries."
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": "\n <div class=\"bk-root\">\n <a href=\"https://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n <span id=\"1001\">Loading BokehJS ...</span>\n </div>"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n\n if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n var JS_MIME_TYPE = 'application/javascript';\n var HTML_MIME_TYPE = 'text/html';\n var EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n var CLASS_NAME = 'output_bokeh rendered_html';\n\n /**\n * Render data to the DOM node\n */\n function render(props, node) {\n var script = document.createElement(\"script\");\n node.appendChild(script);\n }\n\n /**\n * Handle when an output is cleared or removed\n */\n function handleClearOutput(event, handle) {\n var cell = handle.cell;\n\n var id = cell.output_area._bokeh_element_id;\n var server_id = cell.output_area._bokeh_server_id;\n // Clean up Bokeh references\n if (id != null && id in Bokeh.index) {\n Bokeh.index[id].model.document.clear();\n delete Bokeh.index[id];\n }\n\n if (server_id !== undefined) {\n // Clean up Bokeh references\n var cmd = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n cell.notebook.kernel.execute(cmd, {\n iopub: {\n output: function(msg) {\n var id = msg.content.text.trim();\n if (id in Bokeh.index) {\n Bokeh.index[id].model.document.clear();\n delete Bokeh.index[id];\n }\n }\n }\n });\n // Destroy server and session\n var cmd = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n cell.notebook.kernel.execute(cmd);\n }\n }\n\n /**\n * Handle when a new output is added\n */\n function handleAddOutput(event, handle) {\n var output_area = handle.output_area;\n var output = handle.output;\n\n // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n if ((output.output_type != \"display_data\") || (!output.data.hasOwnProperty(EXEC_MIME_TYPE))) {\n return\n }\n\n var toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n\n if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n // store reference to embed id on output_area\n output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n }\n if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n var bk_div = document.createElement(\"div\");\n bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n var script_attrs = bk_div.children[0].attributes;\n for (var i = 0; i < script_attrs.length; i++) {\n toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n }\n // store reference to server id on output_area\n output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n }\n }\n\n function register_renderer(events, OutputArea) {\n\n function append_mime(data, metadata, element) {\n // create a DOM node to render to\n var toinsert = this.create_output_subarea(\n metadata,\n CLASS_NAME,\n EXEC_MIME_TYPE\n );\n this.keyboard_manager.register_events(toinsert);\n // Render to node\n var props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n render(props, toinsert[toinsert.length - 1]);\n element.append(toinsert);\n return toinsert\n }\n\n /* Handle when an output is cleared or removed */\n events.on('clear_output.CodeCell', handleClearOutput);\n events.on('delete.Cell', handleClearOutput);\n\n /* Handle when a new output is added */\n events.on('output_added.OutputArea', handleAddOutput);\n\n /**\n * Register the mime type and append_mime function with output_area\n */\n OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n /* Is output safe? */\n safe: true,\n /* Index of renderer in `output_area.display_order` */\n index: 0\n });\n }\n\n // register the mime type if in Jupyter Notebook environment and previously unregistered\n if (root.Jupyter !== undefined) {\n var events = require('base/js/events');\n var OutputArea = require('notebook/js/outputarea').OutputArea;\n\n if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n register_renderer(events, OutputArea);\n }\n }\n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n var NB_LOAD_WARNING = {'data': {'text/html':\n \"<div style='background-color: #fdd'>\\n\"+\n \"<p>\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"</p>\\n\"+\n \"<ul>\\n\"+\n \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n \"<li>use INLINE resources instead, as so:</li>\\n\"+\n \"</ul>\\n\"+\n \"<code>\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"</code>\\n\"+\n \"</div>\"}};\n\n function display_loaded() {\n var el = document.getElementById(\"1001\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n }\n finally {\n delete root._bokeh_onload_callbacks\n }\n console.info(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(js_urls, callback) {\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n }\n if (js_urls == null || js_urls.length === 0) {\n run_callbacks();\n return null;\n }\n console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = js_urls.length;\n for (var i = 0; i < js_urls.length; i++) {\n var url = js_urls[i];\n var s = document.createElement('script');\n s.src = url;\n s.async = false;\n s.onreadystatechange = s.onload = function() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.log(\"Bokeh: all BokehJS libraries loaded\");\n run_callbacks()\n }\n };\n s.onerror = function() {\n console.warn(\"failed to load library \" + url);\n };\n console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.getElementsByTagName(\"head\")[0].appendChild(s);\n }\n };var element = document.getElementById(\"1001\");\n if (element == null) {\n console.log(\"Bokeh: ERROR: autoload.js configured with elementid '1001' but no matching script tag was found. \")\n return false;\n }\n\n var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.0.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.0.4.min.js\"];\n\n var inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n \n function(Bokeh) {\n \n },\n function(Bokeh) {\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-1.0.4.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.0.4.min.css\");\n }\n ];\n\n function run_inline_js() {\n \n if ((root.Bokeh !== undefined) || (force === true)) {\n for (var i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(js_urls, function() {\n console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));",
"application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n var force = true;\n\n if (typeof (root._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n \n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n var NB_LOAD_WARNING = {'data': {'text/html':\n \"<div style='background-color: #fdd'>\\n\"+\n \"<p>\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"</p>\\n\"+\n \"<ul>\\n\"+\n \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n \"<li>use INLINE resources instead, as so:</li>\\n\"+\n \"</ul>\\n\"+\n \"<code>\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"</code>\\n\"+\n \"</div>\"}};\n\n function display_loaded() {\n var el = document.getElementById(\"1001\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n }\n finally {\n delete root._bokeh_onload_callbacks\n }\n console.info(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(js_urls, callback) {\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n }\n if (js_urls == null || js_urls.length === 0) {\n run_callbacks();\n return null;\n }\n console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = js_urls.length;\n for (var i = 0; i < js_urls.length; i++) {\n var url = js_urls[i];\n var s = document.createElement('script');\n s.src = url;\n s.async = false;\n s.onreadystatechange = s.onload = function() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.log(\"Bokeh: all BokehJS libraries loaded\");\n run_callbacks()\n }\n };\n s.onerror = function() {\n console.warn(\"failed to load library \" + url);\n };\n console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.getElementsByTagName(\"head\")[0].appendChild(s);\n }\n };var element = document.getElementById(\"1001\");\n if (element == null) {\n console.log(\"Bokeh: ERROR: autoload.js configured with elementid '1001' but no matching script tag was found. \")\n return false;\n }\n\n var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.0.4.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-gl-1.0.4.min.js\"];\n\n var inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n \n function(Bokeh) {\n \n },\n function(Bokeh) {\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.css\");\n console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-tables-1.0.4.min.css\");\n Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-tables-1.0.4.min.css\");\n }\n ];\n\n function run_inline_js() {\n \n if ((root.Bokeh !== undefined) || (force === true)) {\n for (var i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n var cell = $(document.getElementById(\"1001\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(js_urls, function() {\n console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));"
},
"metadata": {},
"output_type": "display_data"
}
],
"source": "import pandas as pd\nfrom bokeh.plotting import figure, output_file, show,output_notebook\noutput_notebook()"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "In this section, we define the function <code>make_dashboard</code>. \nYou don't have to know how the function works, you should only care about the inputs. The function will produce a dashboard as well as an html file. You can then use this html file to share your dashboard. If you do not know what an html file is don't worry everything you need to know will be provided in the lab. "
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": "def make_dashboard(x, gdp_change, unemployment, title, file_name):\n output_file(file_name)\n p = figure(title=title, x_axis_label='year', y_axis_label='%')\n p.line(x.squeeze(), gdp_change.squeeze(), color=\"royalblue\", line_width=4, legend=\"% GDP change\")\n p.line(x.squeeze(), unemployment.squeeze(), color=\"firebrick\", line_width=4, legend=\"% unemployed\")\n show(p)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "The dictionary <code>links</code> contain the CSV files with all the data. The value for the key <code>GDP</code> is the file that contains the GDP data. The value for the key <code>unemployment</code> contains the unemployment data."
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": "links={'GDP':'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/clean_gdp.csv',\\\n 'unemployment':'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/coursera_project/clean_unemployment.csv'}"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3 id=\"Section_2\"> Question 1: Create a dataframe that contains the GDP data and display the first five rows of the dataframe.</h3>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Use the dictionary <code>links</code> and the function <code>pd.read_csv</code> to create a Pandas dataframes that contains the GDP data."
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<b>Hint: <code>links[\"GDP\"]</code> contains the path or name of the file.</b>"
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": "gdp_data = pd.read_csv(links[\"GDP\"])"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Use the method <code>head()</code> to display the first five rows of the GDP data, then take a screen-shot."
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>date</th>\n <th>level-current</th>\n <th>level-chained</th>\n <th>change-current</th>\n <th>change-chained</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1948</td>\n <td>274.8</td>\n <td>2020.0</td>\n <td>-0.7</td>\n <td>-0.6</td>\n </tr>\n <tr>\n <th>1</th>\n <td>1949</td>\n <td>272.8</td>\n <td>2008.9</td>\n <td>10.0</td>\n <td>8.7</td>\n </tr>\n <tr>\n <th>2</th>\n <td>1950</td>\n <td>300.2</td>\n <td>2184.0</td>\n <td>15.7</td>\n <td>8.0</td>\n </tr>\n <tr>\n <th>3</th>\n <td>1951</td>\n <td>347.3</td>\n <td>2360.0</td>\n <td>5.9</td>\n <td>4.1</td>\n </tr>\n <tr>\n <th>4</th>\n <td>1952</td>\n <td>367.7</td>\n <td>2456.1</td>\n <td>6.0</td>\n <td>4.7</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " date level-current level-chained change-current change-chained\n0 1948 274.8 2020.0 -0.7 -0.6\n1 1949 272.8 2008.9 10.0 8.7\n2 1950 300.2 2184.0 15.7 8.0\n3 1951 347.3 2360.0 5.9 4.1\n4 1952 367.7 2456.1 6.0 4.7"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": "gdp_data.head(5)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3 id=\"Section_2\"> Question 2: Create a dataframe that contains the unemployment data. Display the first five rows of the dataframe. </h3>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Use the dictionary <code>links</code> and the function <code>pd.read_csv</code> to create a Pandas dataframes that contains the unemployment data."
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": "unemp_data = pd.read_csv(links[\"unemployment\"])"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Use the method <code>head()</code> to display the first five rows of the GDP data, then take a screen-shot."
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>date</th>\n <th>unemployment</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1948</td>\n <td>3.750000</td>\n </tr>\n <tr>\n <th>1</th>\n <td>1949</td>\n <td>6.050000</td>\n </tr>\n <tr>\n <th>2</th>\n <td>1950</td>\n <td>5.208333</td>\n </tr>\n <tr>\n <th>3</th>\n <td>1951</td>\n <td>3.283333</td>\n </tr>\n <tr>\n <th>4</th>\n <td>1952</td>\n <td>3.025000</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " date unemployment\n0 1948 3.750000\n1 1949 6.050000\n2 1950 5.208333\n3 1951 3.283333\n4 1952 3.025000"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": "unemp_data.head(5)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3 id=\"Section_3\">Question 3: Display a dataframe where unemployment was greater than 8.5%. Take a screen-shot.</h3>"
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>date</th>\n <th>unemployment</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>34</th>\n <td>1982</td>\n <td>9.708333</td>\n </tr>\n <tr>\n <th>35</th>\n <td>1983</td>\n <td>9.600000</td>\n </tr>\n <tr>\n <th>61</th>\n <td>2009</td>\n <td>9.283333</td>\n </tr>\n <tr>\n <th>62</th>\n <td>2010</td>\n <td>9.608333</td>\n </tr>\n <tr>\n <th>63</th>\n <td>2011</td>\n <td>8.933333</td>\n </tr>\n </tbody>\n</table>\n</div>",
"text/plain": " date unemployment\n34 1982 9.708333\n35 1983 9.600000\n61 2009 9.283333\n62 2010 9.608333\n63 2011 8.933333"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": "unemp_data[unemp_data[\"unemployment\"] > 8.5]"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3 id=\"Section_4\">Question 4: Use the function make_dashboard to make a dashboard</h3>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "In this section, you will call the function <code>make_dashboard</code> , to produce a dashboard. We will use the convention of giving each variable the same name as the function parameter."
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Create a new dataframe with the column <code>'date'</code> called <code>x</code> from the dataframe that contains the GDP data."
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": "x = pd.DataFrame(gdp_data, columns=[\"date\"])"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Create a new dataframe with the column <code>'change-current' </code> called <code>gdp_change</code> from the dataframe that contains the GDP data."
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": "gdp_change = pd.DataFrame(gdp_data, columns=[\"change-current\"])"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Create a new dataframe with the column <code>'unemployment' </code> called <code>unemployment</code> from the dataframe that contains the unemployment data."
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": "unemployment = pd.DataFrame(unemp_data, columns=[\"unemployment\"])"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Give your dashboard a string title, and assign it to the variable <code>title</code>"
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": "title = \"Unemployment According to GDP\""
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Finally, the function <code>make_dashboard</code> will output an <code>.html</code> in your direictory, just like a <code>csv</code> file. The name of the file is <code>\"index.html\"</code> and it will be stored in the varable <code>file_name</code>."
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": "file_name = \"index.html\""
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Call the function <code>make_dashboard</code> , to produce a dashboard. Assign the parameter values accordingly take a the <b>, take a screen shot of the dashboard and submit it</b>."
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": "\n\n\n\n\n\n <div class=\"bk-root\" id=\"8c8c6e46-426d-498a-8b8b-00e651939215\" data-root-id=\"1003\"></div>\n"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": "(function(root) {\n function embed_document(root) {\n \n var docs_json = {\"20c0d95a-6860-4cd6-a4e9-a476916f33bd\":{\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1013\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"1018\",\"type\":\"LinearAxis\"}],\"renderers\":[{\"id\":\"1013\",\"type\":\"LinearAxis\"},{\"id\":\"1017\",\"type\":\"Grid\"},{\"id\":\"1018\",\"type\":\"LinearAxis\"},{\"id\":\"1022\",\"type\":\"Grid\"},{\"id\":\"1031\",\"type\":\"BoxAnnotation\"},{\"id\":\"1049\",\"type\":\"Legend\"},{\"id\":\"1041\",\"type\":\"GlyphRenderer\"},{\"id\":\"1054\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"1002\",\"type\":\"Title\"},\"toolbar\":{\"id\":\"1029\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"1005\",\"type\":\"DataRange1d\"},\"x_scale\":{\"id\":\"1009\",\"type\":\"LinearScale\"},\"y_range\":{\"id\":\"1007\",\"type\":\"DataRange1d\"},\"y_scale\":{\"id\":\"1011\",\"type\":\"LinearScale\"}},\"id\":\"1003\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"plot\":null,\"text\":\"Unemployment According to GDP\"},\"id\":\"1002\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"1027\",\"type\":\"ResetTool\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"#1f77b4\",\"line_width\":4,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1053\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1028\",\"type\":\"HelpTool\"},{\"attributes\":{\"data_source\":{\"id\":\"1051\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1052\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1053\",\"type\":\"Line\"},\"selection_glyph\":null,\"view\":{\"id\":\"1055\",\"type\":\"CDSView\"}},\"id\":\"1054\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"callback\":null},\"id\":\"1005\",\"type\":\"DataRange1d\"},{\"attributes\":{\"callback\":null},\"id\":\"1007\",\"type\":\"DataRange1d\"},{\"attributes\":{\"source\":{\"id\":\"1051\",\"type\":\"ColumnDataSource\"}},\"id\":\"1055\",\"type\":\"CDSView\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"1031\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1009\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1061\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1011\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1062\",\"type\":\"Selection\"},{\"attributes\":{\"axis_label\":\"year\",\"formatter\":{\"id\":\"1045\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"1003\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"1014\",\"type\":\"BasicTicker\"}},\"id\":\"1013\",\"type\":\"LinearAxis\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"#1f77b4\",\"line_width\":4,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1040\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1014\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1073\",\"type\":\"Selection\"},{\"attributes\":{\"plot\":{\"id\":\"1003\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"1014\",\"type\":\"BasicTicker\"}},\"id\":\"1017\",\"type\":\"Grid\"},{\"attributes\":{\"data_source\":{\"id\":\"1038\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"1039\",\"type\":\"Line\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"1040\",\"type\":\"Line\"},\"selection_glyph\":null,\"view\":{\"id\":\"1042\",\"type\":\"CDSView\"}},\"id\":\"1041\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"axis_label\":\"%\",\"formatter\":{\"id\":\"1047\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"1003\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"1019\",\"type\":\"BasicTicker\"}},\"id\":\"1018\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1019\",\"type\":\"BasicTicker\"},{\"attributes\":{\"source\":{\"id\":\"1038\",\"type\":\"ColumnDataSource\"}},\"id\":\"1042\",\"type\":\"CDSView\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"1003\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"1019\",\"type\":\"BasicTicker\"}},\"id\":\"1022\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1047\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_inspect\":\"auto\",\"active_multi\":null,\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"1023\",\"type\":\"PanTool\"},{\"id\":\"1024\",\"type\":\"WheelZoomTool\"},{\"id\":\"1025\",\"type\":\"BoxZoomTool\"},{\"id\":\"1026\",\"type\":\"SaveTool\"},{\"id\":\"1027\",\"type\":\"ResetTool\"},{\"id\":\"1028\",\"type\":\"HelpTool\"}]},\"id\":\"1029\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"1072\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"items\":[{\"id\":\"1050\",\"type\":\"LegendItem\"},{\"id\":\"1064\",\"type\":\"LegendItem\"}],\"plot\":{\"id\":\"1003\",\"subtype\":\"Figure\",\"type\":\"Plot\"}},\"id\":\"1049\",\"type\":\"Legend\"},{\"attributes\":{\"callback\":null,\"data\":{\"x\":[1948,1949,1950,1951,1952,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016],\"y\":{\"__ndarray__\":\"ZmZmZmZm5r8AAAAAAAAkQGZmZmZmZi9AmpmZmZmZF0AAAAAAAAAYQDMzMzMzM9M/zczMzMzMIUBmZmZmZmYWQAAAAAAAABZAAAAAAAAA+D/NzMzMzMwgQAAAAAAAABBAmpmZmZmZDUCamZmZmZkdQGZmZmZmZhZAmpmZmZmZHUDNzMzMzMwgQDMzMzMzMyNAzczMzMzMFkDNzMzMzMwiQGZmZmZmZiBAAAAAAAAAFkAAAAAAAAAhQJqZmZmZmSNAzczMzMzMJkDNzMzMzMwgQAAAAAAAACJAZmZmZmZmJkAzMzMzMzMmQAAAAAAAACpAZmZmZmZmJ0CamZmZmZkhQGZmZmZmZihAMzMzMzMzEUBmZmZmZmYhQDMzMzMzMyZAAAAAAAAAHkAAAAAAAAAWQAAAAAAAABhAmpmZmZmZH0DNzMzMzMweQM3MzMzMzBZAZmZmZmZmCkCamZmZmZkXQM3MzMzMzBRAMzMzMzMzGUAzMzMzMzMTQM3MzMzMzBZAzczMzMzMGEDNzMzMzMwWQDMzMzMzMxlAAAAAAAAAGkCamZmZmZkJQDMzMzMzMwtAMzMzMzMzE0BmZmZmZmYaQM3MzMzMzBpAAAAAAAAAGEBmZmZmZmYSQM3MzMzMzPw/zczMzMzM/L9mZmZmZmYOQJqZmZmZmQ1AzczMzMzMEEDNzMzMzMwMQJqZmZmZmRFAAAAAAAAAEECamZmZmZkFQM3MzMzMzBBA\",\"dtype\":\"float64\",\"shape\":[69]}},\"selected\":{\"id\":\"1062\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1061\",\"type\":\"UnionRenderers\"}},\"id\":\"1038\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"label\":{\"value\":\"% GDP change\"},\"renderers\":[{\"id\":\"1041\",\"type\":\"GlyphRenderer\"}]},\"id\":\"1050\",\"type\":\"LegendItem\"},{\"attributes\":{\"label\":{\"value\":\"% unemployed\"},\"renderers\":[{\"id\":\"1054\",\"type\":\"GlyphRenderer\"}]},\"id\":\"1064\",\"type\":\"LegendItem\"},{\"attributes\":{},\"id\":\"1023\",\"type\":\"PanTool\"},{\"attributes\":{\"line_color\":\"royalblue\",\"line_width\":4,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1039\",\"type\":\"Line\"},{\"attributes\":{\"callback\":null,\"data\":{\"x\":[1948,1949,1950,1951,1952,1953,1954,1955,1956,1957,1958,1959,1960,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016],\"y\":{\"__ndarray__\":\"AAAAAAAADkAzMzMzMzMYQFVVVVVV1RRARURERERECkA0MzMzMzMIQGdmZmZmZgdA3d3d3d1dFkB4d3d3d3cRQAAAAAAAgBBAMzMzMzMzEUDd3d3d3V0bQM3MzMzMzBVArKqqqqoqFkBERERERMQaQEVERERERBZAEhERERGRFkAiIiIiIqIUQImIiIiICBJAVVVVVVVVDkC5u7u7u7sOQHd3d3d3dwxA7+7u7u7uC0Dv7u7u7u4TQM3MzMzMzBdAZ2ZmZmZmFkDu7u7u7m4TQBIRERERkRZAMzMzMzPzIEDLzMzMzMweQDUzMzMzMxxAREREREREGEBnZmZmZmYXQDUzMzMzsxxAeHd3d3d3HkCqqqqqqmojQDMzMzMzMyNAiYiIiIgIHkBERERERMQcQAAAAAAAABxANTMzMzOzGEB3d3d3d/cVQIiIiIiICBVAd3d3d3d3FkBlZmZmZmYbQHh3d3d39x1AISIiIiKiG0BnZmZmZmYYQN/d3d3dXRZAIyIiIiKiFUBERERERMQTQAAAAAAAABJA393d3d3dEEC5u7u7u7sPQHd3d3d39xJAIyIiIiIiF0B4d3d3d/cXQKyqqqqqKhZAVVVVVVVVFEDv7u7u7m4SQHh3d3d3dxJAMzMzMzMzF0AREREREZEiQHd3d3d3NyNA3t3d3d3dIUBnZmZmZiYgQO/u7u7ubh1AIyIiIiKiGECamZmZmRkVQAAAAAAAgBNA\",\"dtype\":\"float64\",\"shape\":[69]}},\"selected\":{\"id\":\"1073\",\"type\":\"Selection\"},\"selection_policy\":{\"id\":\"1072\",\"type\":\"UnionRenderers\"}},\"id\":\"1051\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1024\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1045\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"overlay\":{\"id\":\"1031\",\"type\":\"BoxAnnotation\"}},\"id\":\"1025\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"line_color\":\"firebrick\",\"line_width\":4,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1052\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1026\",\"type\":\"SaveTool\"}],\"root_ids\":[\"1003\"]},\"title\":\"Bokeh Application\",\"version\":\"1.0.4\"}};\n var render_items = [{\"docid\":\"20c0d95a-6860-4cd6-a4e9-a476916f33bd\",\"roots\":{\"1003\":\"8c8c6e46-426d-498a-8b8b-00e651939215\"}}];\n root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n\n }\n if (root.Bokeh !== undefined) {\n embed_document(root);\n } else {\n var attempts = 0;\n var timer = setInterval(function(root) {\n if (root.Bokeh !== undefined) {\n embed_document(root);\n clearInterval(timer);\n }\n attempts++;\n if (attempts > 100) {\n console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n clearInterval(timer);\n }\n }, 10, root)\n }\n})(window);",
"application/vnd.bokehjs_exec.v0+json": ""
},
"metadata": {
"application/vnd.bokehjs_exec.v0+json": {
"id": "1003"
}
},
"output_type": "display_data"
}
],
"source": "make_dashboard(x=x, gdp_change=gdp_change, unemployment=unemployment, title=title, file_name=file_name)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h3 id=\"Section_5\"> <b>(Optional not marked)</b>Save the dashboard on IBM cloud and display it </h3>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "From the tutorial <i>PROVISIONING AN OBJECT STORAGE INSTANCE ON IBM CLOUD</i> copy the JSON object containing the credentials you created. You\u2019ll want to store everything you see in a credentials variable like the one below (obviously, replace the placeholder values with your own). Take special note of your <code>access_key_id</code> and <code>secret_access_key</code>. <b>Do not delete <code># @hidden_cell </code> as this will not allow people to see your credentials when you share your notebook. </b>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<code>\ncredentials = {<br>\n &nbsp; \"apikey\": \"your-api-key\",<br>\n &nbsp; \"cos_hmac_keys\": {<br>\n &nbsp; \"access_key_id\": \"your-access-key-here\", <br>\n &nbsp; \"secret_access_key\": \"your-secret-access-key-here\"<br>\n &nbsp; },<br>\n</code>\n<code>\n &nbsp;\"endpoints\": \"your-endpoints\",<br>\n &nbsp; \"iam_apikey_description\": \"your-iam_apikey_description\",<br>\n &nbsp; \"iam_apikey_name\": \"your-iam_apikey_name\",<br>\n &nbsp; \"iam_role_crn\": \"your-iam_apikey_name\",<br>\n &nbsp; \"iam_serviceid_crn\": \"your-iam_serviceid_crn\",<br>\n &nbsp;\"resource_instance_id\": \"your-resource_instance_id\"<br>\n}\n</code>"
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": "# The code was removed by Watson Studio for sharing."
},
{
"cell_type": "markdown",
"metadata": {},
"source": "You will need the endpoint make sure the setting are the same as <i> PROVISIONING AN OBJECT STORAGE INSTANCE ON IBM CLOUD </i> assign the name of your bucket to the variable <code>bucket_name </code> "
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": "endpoint = 'https://s3-api.us-geo.objectstorage.softlayer.net'"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "From the tutorial <i> PROVISIONING AN OBJECT STORAGE INSTANCE ON IBM CLOUD </i> assign the name of your bucket to the variable <code>bucket_name </code> "
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-17-15f274bb6382>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-17-15f274bb6382>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m bucket_name = # Type your bucket name on IBM Cloud\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": "bucket_name = # Type your bucket name on IBM Cloud"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "We can access IBM Cloud Object Storage with Python useing the <code>boto3</code> library, which we\u2019ll import below:"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "import boto3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "We can interact with IBM Cloud Object Storage through a <code>boto3</code> resource object."
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "resource = boto3.resource(\n 's3',\n aws_access_key_id = credentials[\"cos_hmac_keys\"]['access_key_id'],\n aws_secret_access_key = credentials[\"cos_hmac_keys\"][\"secret_access_key\"],\n endpoint_url = endpoint,\n)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "We are going to use <code>open</code> to create a file object. To get the path of the file, you are going to concatenate the name of the file stored in the variable <code>file_name</code>. The directory stored in the variable directory using the <code>+</code> operator and assign it to the variable \n<code>html_path</code>. We will use the function <code>getcwd()</code> to find current the working directory."
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "import os\n\ndirectory = os.getcwd()\nhtml_path = directory + \"/\" + file_name"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "Now you must read the html file, use the function <code>f = open(html_path, mode)</code> to create a file object and assign it to the variable <code>f</code>. The parameter <code>file</code> should be the variable <code>html_path</code>, the mode should be <code>\"r\"</code> for read. "
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Type your code here"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "To load your dataset into the bucket we will use the method <code>put_object</code>, you must set the parameter name to the name of the bucket, the parameter <code>Key</code> should be the name of the HTML file and the value for the parameter Body should be set to <code>f.read()</code>."
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Fill up the parameters in the following function:\n# resource.Bucket(name=).put_object(Key=, Body=)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "In the dictionary <code>Params</code> provide the bucket name as the value for the key <i>'Bucket'</i>. Also for the value of the key <i>'Key'</i> add the name of the <code>html</code> file, both values should be strings."
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "# Fill in the value for each key\n# Params = {'Bucket': ,'Key': }"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "The following lines of code will generate a URL to share your dashboard. The URL only last seven days, but don't worry you will get full marks if the URL is visible in your notebook. "
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "import sys\ntime = 7*24*60**2\nclient = boto3.client(\n 's3',\n aws_access_key_id = credentials[\"cos_hmac_keys\"]['access_key_id'],\n aws_secret_access_key = credentials[\"cos_hmac_keys\"][\"secret_access_key\"],\n endpoint_url=endpoint,\n\n)\nurl = client.generate_presigned_url('get_object',Params=Params,ExpiresIn=time)\nprint(url)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h2 id=\"Section_5\"> How to submit </h2>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<p>Once you complete your notebook you will have to share it to be marked. Select the icon on the top right a marked in red in the image below, a dialogue box should open, select the option all&nbsp;content excluding sensitive code cells.</p>\n\n<p><img height=\"440\" width=\"700\" src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/EdX/ReadMe%20files/share_noteook1.png\" alt=\"share notebook\" /></p>\n<p></p>\n\n<p>You can then share the notebook&nbsp; via a&nbsp; URL by scrolling down as shown in the following image:</p>\n<p style=\"text-align: center;\"> <img height=\"308\" width=\"350\" src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/projects/EdX/ReadMe%20files/link2.png\" alt=\"share notebook\" /> </p>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<hr>\n<p>Copyright &copy; 2019 IBM Developer Skills Network. This notebook and its source code are released under the terms of the <a href=\"https://cognitiveclass.ai/mit-license/\">MIT License</a>.</p>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h2>About the Authors:</h2> \n\n<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\">Joseph Santarcangelo</a> has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.\n<p>\nOther contributors: <a href=\"https://www.linkedin.com/in/yi-leng-yao-84451275/\">Yi leng Yao</a>, <a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a> \n</p>"
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<h2>References :</h2> "
},
{
"cell_type": "markdown",
"metadata": {},
"source": "<ul>\n <il>\n 1) <a href=\"https://research.stlouisfed.org/\">Economic Research at the St. Louis Fed </a>:<a href=\"https://fred.stlouisfed.org/series/UNRATE/\"> Civilian Unemployment Rate</a>\n </il> \n <p>\n <il>\n 2) <a href=\"https://github.com/datasets\">Data Packaged Core Datasets\n </a>\n </il> \n </p>\n \n</ul>\n</div>"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.6",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment