{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check which version is installed on your machine and please upgrade if needed. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import plotly\n",
"plotly.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's load the dependencies/packages that we need in order to get a simple stream going."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import plotly.plotly as py \n",
"import plotly.tools as tls \n",
"import plotly.graph_objs as go\n",
"import numpy as np "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Overview\n",
"\n",
"In this example we're going to randomly draw two airports from a dataframe, then have our plane fly from one to the other. Then as soon as it reaches its destination, we will randomly select a new airport for our plane to fly to. \n",
"The dataset we will be using will be based on American airports."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Getting Set Up"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's load the dataset that we'll be using:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd\n",
"dframe = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's get at least two streaming tokens for this task."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4lm5a0gsr8\n"
]
}
],
"source": [
"stream_tokens = tls.get_credentials_file()['stream_ids']\n",
"token = stream_tokens[-2] # I'm getting my stream tokens from the end to ensure I'm not reusing tokens\n",
"print token\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's create some `stream id objects` for each token."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'maxpoints': 3, 'token': u'4lm5a0gsr8'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stream_id = dict(token=token, maxpoints=3)\n",
"stream_id"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To set this plot up, we will first create a scattergeo trace in our data list. Next we will adjust the layout in order to change the plotting surface to a map of the United States, along with some other aesthetic options. "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = [dict(\n",
" type='scattergeo',\n",
" lon=[],\n",
" lat=[],\n",
" mode='markers',\n",
" marker=dict(\n",
" size=8,\n",
" opacity=0.8,\n",
" reversescale=True,\n",
" autocolorscale=False,\n",
" line=dict(\n",
" width=1,\n",
" color='rgba(102, 102, 102)'\n",
" ),\n",
" ),\n",
" stream=stream_id,\n",
" name=\"Plane\")]\n",
"\n",
"layout = dict(\n",
" title = 'Busy Airplane Streaming',\n",
" colorbar = False,\n",
" geo = dict(\n",
" scope='usa',\n",
" projection=dict( type='albers usa' ),\n",
" showland = True,\n",
" landcolor = \"rgb(250, 250, 250)\",\n",
" subunitcolor = \"rgb(217, 217, 217)\",\n",
" countrycolor = \"rgb(217, 217, 217)\",\n",
" countrywidth = 0.5,\n",
" subunitwidth = 0.5\n",
" ),\n",
" )\n",
"\n",
"fig = dict( data=data, layout=layout )\n",
"py.iplot( fig, validate=False, filename='geo-streaming2', auto_open=False, fileopt='extend')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's set up a `stream link object` for our geo-scatter trace and start streaming some data to our plot"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"s = py.Stream(stream_id=token)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Start Streaming"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below we'll wrap the relevant code to generate the data stream in a function we call `lets_stream()`.\n",
"We will also follow up with a while loop that will keep our stream up persistently. Notice that the small green dot is the departing airport, the red dot is the arrival airport, and the blue dot is the plane!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import datetime\n",
"import time\n",
"\n",
"def lets_stream():\n",
"\n",
" s.open()\n",
"\n",
" airports = dframe.sample(4)[['lat', 'long', 'airport']]\n",
" depart = airports.iloc[0]\n",
" arrive = airports.iloc[1]\n",
" num_steps = 20\n",
"\n",
" while True:\n",
"\n",
" count = 0\n",
"\n",
" lats = np.linspace(depart['lat'], arrive['lat'], num_steps)\n",
" lons = np.linspace(depart['long'], arrive['long'], num_steps)\n",
"\n",
" for i, j in zip(lats, lons):\n",
"\n",
" # added pts for the departure and arrival airports!!!\n",
" s.write(dict(lon=[depart['long'], j, arrive['long']],\n",
" lat=[\n",
" depart['lat'], i, arrive['lat']], type='scattergeo',\n",
" marker={'size': [5, 7 + 0.2 * count, 5], 'sizemode': 'area',\n",
" 'color': [\"green\", \"blue\", \"red\"],\n",
" 'symbol': [\"circle\", \"star\", \"x-open\"]},\n",
" text=[depart['airport'],\n",
" '{},{}'.format(count, datetime.datetime.now()),\n",
" arrive['airport']]))\n",
"\n",
" count += 1\n",
" stall = np.random.normal(10, 3)\n",
" time.sleep(int((abs(stall) + 0.01) / 2.0))\n",
" s.heartbeat()\n",
" time.sleep(int((abs(stall) + 0.01) / 2.0))\n",
"\n",
" depart = arrive\n",
" arrive = dframe.sample(1)[['lat', 'long', 'airport']].iloc[0]\n",
"\n",
"while True:\n",
" try:\n",
" lets_stream()\n",
" except Exception as e:\n",
" with open('log.txt', 'a+') as f:\n",
" f.write(str(e))\n",
" print(str(e))\n",
" s.close()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see a live demo of the stream below:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tls.embed('streaming-demos','121')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already up-to-date: publisher in /Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages\r\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n",
" warnings.warn('Did you \"Save\" this notebook before running this command? '\n",
"/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:58: UserWarning: Your URL has more than 2 parts... are you sure?\n",
" warnings.warn('Your URL has more than 2 parts... are you sure?')\n"
]
}
],
"source": [
"from IPython.display import display, HTML\n",
"\n",
"display(HTML(''))\n",
"display(HTML(''))\n",
"\n",
"! pip install publisher --upgrade\n",
"import publisher\n",
"publisher.publish(\n",
" 'geo-streaming', 'python/geo-streaming//', 'Streaming to a Map',\n",
" 'Streaming in Plotly with Python', name=\"Streaming to Maps\",\n",
" title = 'Geo-Streaming with Plotly',\n",
" thumbnail='', language='python',\n",
" layout='user-guide', has_thumbnail='false',\n",
" ipynb= '~notebook_demo/82') "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}