{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Since you launched the notebook from within `venv`, you can access the dependencies installed in that virtual environment by using an `import` statement in the notebook. If you install more dependencies later on, remember to relaunch the notebook from the terminal virtual environment before running the `import` statement in the notebook.\n", "\n", "In the project directory, create a new file called `.env` (notice the dot in front of this filename) formatted like `.env.example` to store your credentials from the [Twilio Console](https://www.twilio.com/console).\n", "\n", " This code imports the Python dependencies and loads the environment variables stored in `.env` so you can access the Twilio REST API with your credentials." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import pandas as pd\n", "import os\n", "from dotenv import load_dotenv\n", "load_dotenv()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This makes an HTTP `GET` request to the Twilio API to retrieve all of your SMS messages. The data is saved to a variable called `messages` that we can use while running subsequent cells. The resulting output is a log statement with a count of your SMS messages and a JSON object of those messages." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "accountSID = os.getenv('ACCOUNT_SID')\n", "authToken = os.getenv('AUTH_TOKEN')\n", "url = f'https://api.twilio.com/2010-04-01/Accounts/{accountSID}/Messages.json'\n", "response = requests.get(url, auth=(accountSID, authToken))\n", "messages = response.json()['messages']\n", "print('Number of messages: ', len(messages))\n", "# json is a built-in module for Python - no install required\n", "import json\n", "print(json.dumps(messages, indent=2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that you know how the response from the Twilio API is formatted, you can handle the data like a SQL table using the primary pandas data structure called a [DataFrame](https://pandas.pydata.org/pandas-docs/stable/user_guide/dsintro.html#dataframe). Run this code in the next cell of the notebook to see the first five rows of SMS messages." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(messages)\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tidy up the data by displaying only the `Date`, `Price`, and `Direction` of your messages, renaming the columns and formatting the data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = df.rename(columns={'price': 'Price', 'direction': 'Direction', 'date_sent': 'Date'})\n", "df['Date'] = pd.to_datetime(df.Date).dt.strftime('%m/%d/%Y')\n", "print(df[['Date','Price', 'Direction']])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ax = df.plot(kind='scatter',x='Date',y='Price')\n", "df[['Date','Price','Direction']].apply(lambda row: ax.text(*row),axis=1);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Show this data in a bar graph, adding up the prices and displaying this information over time." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df['Price'] = df['Price'].astype(float)\n", "df['Date'] = pd.to_datetime(df['Date'])\n", "df['Price'].groupby(df['Date'].dt.to_period('D')).sum().plot(kind='bar', color='red')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }