{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Warning!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example notebook is not up-to-date with the latest version of Plotly's Python API (version 1.0.\\*). \n", "\n", "Refer to Plotly's Python [User Guide](http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s00_homepage/s00_homepage.ipynb) \n", "and more specifically [section 7.0](http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming.ipynb) and [section 7.1](http://nbviewer.ipython.org/github/plotly/python-user-guide/blob/master/s7_streaming/s7_streaming_p1-first-stream.ipynb) for an updated version of this notebook.\n", "\n", "
" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Quickstart to Plotly's Real-time Streaming API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotly Streaming enables your plotly graphs to update in real-time, without refreshing your browser.\n", "\n", "Learn more about real-time streaming graphs with plotly here:\n", "\n", "[https://github.com/plotly/streaming-demos](https://github.com/plotly/streaming-demos)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import plotly\n", "import json\n", "import time\n", "from math import cos, sin" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "print(plotly.__version__)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.5.11\n" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "# Fill in the config.json file in this directory with your plotly username, \n", "# plotly API key, and your generated plotly streaming tokens\n", "# Sign up to plotly here: https://plot.ly/ssu\n", "# View your API key and streaming tokens here: https://plot.ly/settings\n", "\n", "with open('./config.json') as config_file:\n", " plotly_user_config = json.load(config_file)\n", "\n", "username = plotly_user_config['plotly_username'] \n", "api_key = plotly_user_config['plotly_api_key']\n", "stream_token = plotly_user_config['plotly_streaming_tokens'][5]\n", "stream_server = 'http://stream.plot.ly'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# Initialize your plotly object\n", "p = plotly.plotly(username, api_key)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# Initialize your plotly real-time streaming graph with a REST API call\n", "# Embed the stream token in one of the traces of a plotly-data object - one token per trace\n", "# Also embed 'maxpoints', the number of points that you want plotted at a time\n", "\n", "# The `iplot` command will embed our plotly graph as an iframe in this notebook\n", "# Each plotly graph has a unique url that you can share and anyone can view \n", "# your streaming graph in real-time\n", "\n", "# The unique URL for this graph is https://plot.ly/~streaming-demos/1\n", "\n", "p.iplot([{'x': [], 'y': [], 'type': 'scatter',\n", " 'stream': {'token': stream_token, 'maxpoints': 100}\n", " }], layout={'xaxis': {'range':[-1,1]}, 'yaxis': {'range': [-1,1]}}, \n", " filename='Stream Example', fileopt='overwrite')" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "# Now stream! Initialize a plotly stream object with your token\n", "s = plotly.stream(stream_token)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "# Write a point and watch the plot update!\n", "s.write({'x': 0.5, 'y': 0.5})" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Send a bunch of points\n", "i=0\n", "while True:\n", " s.write({'x': cos(5*i/50.)*sin(i/50.), 'y': cos(5*i/50.)*cos(i/50.)})\n", " i+=1\n", " time.sleep(0.05)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# When your done, close your stream\n", "s.close()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Give it a try yourself!\n", "\n", "##### More Streaming IPython Notebooks:\n", "\n", "[http://nbviewer.ipython.org/github/plotly/Streaming-Demos/tree/master/IPython%20examples/](http://nbviewer.ipython.org/github/plotly/Streaming-Demos/tree/master/IPython%20examples/)\n", "\n", "##### More about Plotly Streaming \n", "\n", "[https://github.com/plotly/Streaming-Demos](https://github.com/plotly/Streaming-Demos)\n", "\n", "##### Get in touch!\n", "\n", "[http://twitter.com/plotlygraphs](@plotlygraphs)\n", "\n", "[https://facebook.com/plotly](https://facebook.com/plotly)\n", "\n", "" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# CSS styling within IPython notebook\n", "from IPython.core.display import HTML\n", "import urllib2\n", "def css_styling():\n", " url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'\n", " styles = urllib2.urlopen(url).read()\n", " return HTML(styles)\n", "\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }