Skip to content

Instantly share code, notes, and snippets.

@abhishekomi
Created May 25, 2021 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhishekomi/6520fcbb326e714235dd9310c0ab4ae4 to your computer and use it in GitHub Desktop.
Save abhishekomi/6520fcbb326e714235dd9310c0ab4ae4 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/IDSNlogo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n",
"\n",
"# Application Programming Interface\n",
"\n",
"Estimated time needed: **15** minutes\n",
"\n",
"## Objectives\n",
"\n",
"After completing this lab you will be able to:\n",
"\n",
"- Create and Use APIs in Python\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introduction\n",
"\n",
"An API lets two pieces of software talk to each other. Just like a function, you don’t have to know how the API works only its inputs and outputs. An essential type of API is a REST API that allows you to access resources via the internet. In this lab, we will review the Pandas Library in the context of an API, we will also review a basic REST API \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Table of Contents\n",
"\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"<li><a href=\"#ref0\">Pandas is an API</a></li>\n",
"<li><a href=\"#ref1\">REST APIs Basics </a></li>\n",
"<li><a href=\"#ref2\">Quiz on Tuples</a></li>\n",
"\n",
"</div>\n",
"\n",
"<hr>\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting pycoingecko\n",
" Downloading https://files.pythonhosted.org/packages/42/34/e953751a2786273cf7ad2b144d34f2ba321885a1d0515de2c2ad19b8d25e/pycoingecko-2.0.0-py3-none-any.whl\n",
"Requirement already satisfied: requests in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from pycoingecko) (2.25.1)\n",
"Requirement already satisfied: idna<3,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests->pycoingecko) (2.10)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests->pycoingecko) (1.26.4)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests->pycoingecko) (2020.12.5)\n",
"Requirement already satisfied: chardet<5,>=3.0.2 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests->pycoingecko) (4.0.0)\n",
"Installing collected packages: pycoingecko\n",
"Successfully installed pycoingecko-2.0.0\n",
"Requirement already satisfied: plotly in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (4.14.3)\n",
"Requirement already satisfied: retrying>=1.3.3 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from plotly) (1.3.3)\n",
"Requirement already satisfied: six in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from plotly) (1.16.0)\n",
"Collecting mplfinance\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/3f/4e/a3de915df8d112579beb3df22e47b3929670b10e9c5dfd4a2b31bf1a34a7/mplfinance-0.12.7a17-py3-none-any.whl (62kB)\n",
"\u001b[K |████████████████████████████████| 71kB 1.0MB/s eta 0:00:01\n",
"\u001b[?25hRequirement already satisfied: pandas in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from mplfinance) (1.1.5)\n",
"Requirement already satisfied: matplotlib in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from mplfinance) (3.3.4)\n",
"Requirement already satisfied: pytz>=2017.2 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from pandas->mplfinance) (2021.1)\n",
"Requirement already satisfied: python-dateutil>=2.7.3 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from pandas->mplfinance) (2.8.1)\n",
"Requirement already satisfied: numpy>=1.15.4 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from pandas->mplfinance) (1.19.5)\n",
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from matplotlib->mplfinance) (2.4.7)\n",
"Requirement already satisfied: pillow>=6.2.0 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from matplotlib->mplfinance) (8.2.0)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from matplotlib->mplfinance) (1.3.1)\n",
"Requirement already satisfied: cycler>=0.10 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages/cycler-0.10.0-py3.6.egg (from matplotlib->mplfinance) (0.10.0)\n",
"Requirement already satisfied: six>=1.5 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from python-dateutil>=2.7.3->pandas->mplfinance) (1.16.0)\n",
"Installing collected packages: mplfinance\n",
"Successfully installed mplfinance-0.12.7a17\n"
]
}
],
"source": [
"!pip install pycoingecko\n",
"!pip install plotly\n",
"!pip install mplfinance"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"PandasAPI\">Pandas is an API </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pandas is actually set of software components , much of which is not even written in Python.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import plotly.graph_objects as go\n",
"from plotly.offline import plot\n",
"import matplotlib.pyplot as plt\n",
"import datetime\n",
"from pycoingecko import CoinGeckoAPI\n",
"from mplfinance.original_flavor import candlestick2_ohlc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You create a dictionary, this is just data.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"dict_={'a':[11,21,31],'b':[12,22,32]}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you create a Pandas object with the Dataframe constructor in API lingo, this is an \"instance\". The data in the dictionary is passed along to the pandas API. You then use the dataframe to communicate with the API.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"pandas.core.frame.DataFrame"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df=pd.DataFrame(dict_)\n",
"type(df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src = \"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/images/pandas_api.png\" width = 800, align = \"center\" alt=\"logistic regression block diagram\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you call the method head the dataframe communicates with the API displaying the first few rows of the dataframe.\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"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>a</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>11</td>\n",
" <td>12</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>21</td>\n",
" <td>22</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>31</td>\n",
" <td>32</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b\n",
"0 11 12\n",
"1 21 22\n",
"2 31 32"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you call the method mean,the API will calculate the mean and return the value.\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"a 21.0\n",
"b 22.0\n",
"dtype: float64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.mean()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref1\">REST APIs</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Rest API’s function by sending a request, the request is communicated via HTTP message. The HTTP message usually contains a JSON file. This contains instructions for what operation we would like the service or resource to perform. In a similar manner, API returns a response, via an HTTP message, this response is usually contained within a JSON.\n",
"\n",
"In cryptocurrency a popular method to display the movements of the price of a currency.\n",
"\n",
"![Example Candlestick Graph](https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/candlestick.png)\n",
"\n",
"Here is a description of the candle sticks.\n",
"\n",
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/explanation.png\" alt=\"Candle Stick Definition\" width=\"800\" height=\"600\">\n",
"\n",
"In this lab, we will be using the <a href=https://www.coingecko.com/en/api?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01>CoinGecko API</a> to create one of these candlestick graphs for Bitcoin. We will use the API to get the price data for 30 days with 24 observation per day, 1 per hour. We will find the max, min, open, and close price per day meaning we will have 30 candlesticks and use that to generate the candlestick graph. Although we are using the CoinGecko API we will use a Python client/wrapper for the API called <a href=https://github.com/man-c/pycoingecko?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01>PyCoinGecko</a>. PyCoinGecko will make performing the requests easy and it will deal with the enpoint targeting.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Lets start off by getting the data we need. Using the <code>get_coin_market_chart_by_id(id, vs_currency, days)</code>. <code>id</code> is the name of the coin you want, <code>vs_currency</code> is the currency you want the price in, and <code>days</code> is how many days back from today you want.\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"cg = CoinGeckoAPI()\n",
"\n",
"bitcoin_data = cg.get_coin_market_chart_by_id(id='bitcoin', vs_currency='usd', days=30)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(bitcoin_data )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The response we get is in the form of a JSON which includes the price, market caps, and total volumes along with timestamps for each observation. We are focused on the prices so we will select that data.\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1619377539676, 49672.26753237071],\n",
" [1619381080719, 49477.527122053776],\n",
" [1619384566226, 48294.23193394389],\n",
" [1619388220980, 47409.78302911801],\n",
" [1619391773137, 48826.52065650017]]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bitcoin_price_data = bitcoin_data['prices']\n",
"\n",
"bitcoin_price_data[0:5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally lets turn this data into a Pandas DataFrame.\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"data = pd.DataFrame(bitcoin_price_data, columns=['TimeStamp', 'Price'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have the DataFrame we will convert the timestamp to datetime and save it as a column called <code>Date</code>. We will map our <code>unix_to_datetime</code> to each timestamp and convert it to a readable datetime.\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"data['Date'] = pd.to_datetime(data['TimeStamp'], unit='ms')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using this modified dataset we can now group by the <code>Date</code> and find the min, max, open, and close for the candlesticks.\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"candlestick_data = data.groupby(data.Date.dt.date, as_index=False).agg({\"Price\": ['min', 'max', 'first', 'last']})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally we are now ready to use plotly to create our Candlestick Chart.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig = go.Figure(data=[go.Candlestick(x=data['Date'],\n",
" open=candlestick_data['Price']['first'], \n",
" high=candlestick_data['Price']['max'],\n",
" low=candlestick_data['Price']['min'], \n",
" close=candlestick_data['Price']['last'])\n",
" ])\n",
"\n",
"fig.update_layout(xaxis_rangeslider_visible=False)\n",
"\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Authors:\n",
"\n",
"## Change Log\n",
"\n",
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"| ----------------- | ------- | ------------- | ---------------------------------- |\n",
"| 2020-11-23 | 3.0 | Azim Hirjani | New API |\n",
"| 2020-09-09 | 2.1 | Malika Singla | Spell Check |\n",
"| 2020-08-26 | 2.0 | Lavanya | Moved lab to course repo in GitLab |\n",
"| | | | |\n",
"\n",
"<hr/>\n",
"\n",
"## <h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment