Skip to content

Commit 39626f7

Browse files
committed
Real-time streaming support, version bump
1 parent 2034f25 commit 39626f7

5 files changed

Lines changed: 64 additions & 7 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
Untitled0.ipynb
88

99
*.ipynb
10+
11+
build
12+
13+
dist

plotly/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
from .plotly import signup
33
from .plotly import display
44
from .plotly import embed
5-
from .plotly import plotly
5+
from .plotly import plotly
6+
from .plotly import stream

plotly/plotly.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import requests
22
import json
33
import warnings
4+
import httplib
45

56
from .version import __version__
67

@@ -250,5 +251,53 @@ def __makecall(self, args, un, key, origin, kwargs):
250251

251252
return r
252253

254+
class stream:
255+
def __init__(self, token):
256+
''' plotly stream constructor
257+
token found at https://plot.ly/settings
258+
'''
259+
self.token = token
260+
self.connected = False
261+
262+
def init(self):
263+
''' Initialize a streaming connection to plotly
264+
'''
265+
self.conn = httplib.HTTPConnection('stream.plot.ly', 80)
266+
self.conn.putrequest('POST', '/')
267+
self.conn.putheader('Host', 'stream.plot.ly')
268+
self.conn.putheader('User-Agent', 'Python-Plotly')
269+
self.conn.putheader('Transfer-Encoding', 'chunked')
270+
self.conn.putheader('Connection', 'close')
271+
self.conn.putheader('plotly-streamtoken', self.token)
272+
self.conn.endheaders()
273+
self.connected=True
274+
275+
def write(self, data):
276+
''' Write data to plotly's streaming servers
253277
278+
data is a plotly formatted data dict
279+
with data keys 'x', 'y', 'text', 'z', 'marker', 'line'
280+
'x', 'y', 'text', and 'z' can have values of strings, numbers, or lists
281+
'marker', and 'line' have dicts as values with keys 'size', 'color', 'symbol'
254282
283+
Examples:
284+
{'x': 1, 'y': 2}
285+
{'x': [1, 2, 3], 'y': [10, 20, 30]}
286+
{'x': 1, 'y': 3, 'text': 'hover text'}
287+
{'x': 1, 'y': 3, 'marker': {'color': 'blue'}}
288+
{'z': [[1,2,3], [4,5,6]]}
289+
'''
290+
if not self.connected:
291+
self.init()
292+
# plotly's streaming API takes new-line separated json objects
293+
msg = json.dumps(data)+'\n'
294+
msglen = format(len(msg), 'x')
295+
# chunked encoding requests contain the messege length in hex, \r\n, and then the message
296+
self.conn.send('{msglen}\r\n{msg}\r\n'.format(msglen=msglen, msg=msg))
297+
298+
def close(self):
299+
''' Close connection to plotly's streaming servers
300+
'''
301+
self.conn.send('0\r\n\r\n')
302+
self.conn.close()
303+
self.connected=False

plotly/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.5.10'
1+
__version__ = '0.5.11'

setup.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
from setuptools import setup
22
exec(open('plotly/version.py').read())
3+
34
def readme():
45
with open('README.txt') as f:
56
return f.read()
67

78
setup(name='plotly',
89
version=__version__,
10+
use_2to3=True,
911
author='Chris P',
1012
author_email='chris@plot.ly',
1113
maintainer='Chris P',
1214
maintainer_email='chris@plot.ly',
1315
url='https://plot.ly/api/python',
1416
description='Python plotting library for collaborative, interactive, web-based, publication-quality graphs.',
1517
long_description=readme(),
16-
classifiers=['Development Status :: 4 - Beta',
17-
'Programming Language :: Python :: 2.7',
18-
'Programming Language :: Python :: 3.3',
19-
'Topic :: Scientific/Engineering :: Visualization',
20-
],
18+
classifiers=[
19+
'Development Status :: 4 - Beta',
20+
'Programming Language :: Python :: 2.7',
21+
'Programming Language :: Python :: 3.3',
22+
'Topic :: Scientific/Engineering :: Visualization',
23+
],
2124
license='MIT',
2225
packages=['plotly'],
2326
install_requires=[

0 commit comments

Comments
 (0)