import logging
logging.basicConfig(level=logging.DEBUG)
import plotly.tools as tls
import base64
from IPython.display import HTML, display, clear_output, Javascript, clear_output
import plotly.plotly as py
import uuid
import threading
import time
import uuid
import json
from pkg_resources import resource_string
/Users/chris/anaconda/lib/python2.7/site-packages/pandas/computation/expressions.py:21: UserWarning: The installed version of numexpr 2.0.1 is not supported in pandas and will be not be used The minimum supported version is 2.1 "version is 2.1\n".format(ver=ver), UserWarning)
# inject css
css = resource_string('plotly', 'plotly/displayCSS.html').decode('utf-8')
HTML(css)
time.sleep(0.3)
# other requirements
unveil = resource_string('plotly', 'plotly/jquery.unveil.js').decode('utf-8')
html = '<script type="text/javascript">console.log("unveil code");'+unveil+'</script>'
display(HTML(html));
class CachedPlot():
def __init__(self):
self.image_js = ''
self.png = ''
self.plot_js = ''
self.container_id = str(uuid.uuid4())
self.image_failed = False
def container_html(self):
js_display_code = resource_string('plotly',
'plotly/displayLogic.js').decode('utf-8')
html = ('<div class="plotly-output-container" id="'+self.container_id+'">'
'<div class="loading"></div>'
'<div class="toggle"></div>'
'</div>')
# '<script type="text/javascript">$(document).ready(function(){console.log("on document load"); window.displayLogic(\''+self.container_id+'\');});</script>'
return html
def image_code(self):
js = ('console.log("image code");'
'var $container = $(\'#{container_id}\');'
'$container.prepend(\'<img style="width:1000px;" src="{src}">\');');
js = js.format(src='data:image/png;base64,'+base64.b64encode(self.png), container_id=self.container_id)
self.image_js = js
def gen_image(self, figure_or_data):
try:
self.png = py.image.get(figure_or_data, format='png', width=1000, height=600)
except:
self.image_failed = True
def iframe_code(self, figure_or_data):
js = ('console.log("iframe code");'
'var $container = $("#{container_id}");'
'$container.prepend(\'<iframe scrolling="no" style="border:none;display:none;"seamless="seamless" data-src="{url}.embed" height="600" width="100%"> </iframe>\');'
'$(\'.plotly-output-container\').unveil(800);');
url = py.plot(figure_or_data, filename=self.container_id, auto_open=False)
js = js.format(url=url, container_id=self.container_id)
self.plot_js = js
def watcher(self):
clear_output()
image_shown = False
plot_shown = False
time.sleep(0.3)
display(HTML(self.container_html()))
time.sleep(0.3)
while True:
if not image_shown and self.png:
self.image_code()
display(Javascript(self.image_js))
image_shown = True
if not plot_shown and self.plot_js:
display(Javascript(self.plot_js))
plot_shown = True
if plot_shown and (image_shown or self.image_failed):
break
time.sleep(0.05)
def go(self, figure_or_data):
plot_thread = threading.Thread(target=self.iframe_code, args=(figure_or_data, ))
image_thread = threading.Thread(target=self.gen_image, args=(figure_or_data, ))
watcher_thread = threading.Thread(target=self.watcher)
plot_thread.start()
image_thread.start()
watcher_thread.start()
links = [
'https://plot.ly/~etpinard/249',
'https://plot.ly/~etpinard/251',
'https://plot.ly/~etpinard/254',
'https://plot.ly/~etpinard/256',
'https://plot.ly/~etpinard/256',
'https://plot.ly/~etpinard/272',
'https://plot.ly/~etpinard/294',
'https://plot.ly/~etpinard/295',
'https://plot.ly/~etpinard/145']
CachedPlot().go(py.get_figure(links[0]))
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly DEBUG:requests.packages.urllib3.connectionpool:"GET /apigetfile/etpinard/249 HTTP/1.1" 200 None INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly
DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None
CachedPlot().go(py.get_figure(links[1]))
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly
DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
CachedPlot().go(py.get_figure(links[2]))
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
CachedPlot().go(py.get_figure(links[3]))
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly
DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None
CachedPlot().go(py.get_figure(links[4]))
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly
DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
CachedPlot().go(py.get_figure(links[5]))
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly
DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None
CachedPlot().go(py.get_figure(links[6]))
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly
DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None
CachedPlot().go(py.get_figure(links[7]))
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly
DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None
CachedPlot().go(py.get_figure(links[8]))
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly
DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None