import logging
logging.basicConfig(level=logging.DEBUG)
import base64
from IPython.display import HTML, display, clear_output
import plotly.plotly as py
import uuid
/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)
def CachedPlot(figure_or_data):
img_id = str(uuid.uuid4())
def image_html(figure_or_data):
png = py.image.get(figure_or_data, format='png', width=1000, height=600)
html = '<img style="width:100%;" id="{img_id}" src="{src}">'
html = html.format(src='data:image/png;base64,'+base64.b64encode(png), img_id=img_id)
return html
def iframe_html(figure_or_data, ):
graph_id = str(uuid.uuid4())
loading_id = str(uuid.uuid4())
display(HTML('<div id="{loading_id}" '
'style="display:none">'
'Loading interactive plot...</div>'.format(loading_id=loading_id)))
url = py.plot(figure_or_data, filename=graph_id, auto_open=False)
html = (''
'<iframe id="{graph_id}" scrolling="no" '
' style="border:none;"seamless="seamless" display:none;'
' src="{url}.embed" '
' height="600" width="100%">'
'</iframe>'
'<script type="text/javascript">'
'$(document).ready(function () {{'
' var time = new Date().getTime();'
' $("#{graph_id}").attr("data-time", time);'
' $("#{img_id}").show();'
' $("#{loading_id}").show();'
' $("#{graph_id}").hide();'
' $("#{graph_id}").on("load", (function () {{'
' var requestTime = $("#{graph_id}").attr("data-time");'
' if(new Date().getTime() - requestTime > 200){{'
' $("#{img_id}").hide();'
' $("#{graph_id}").show();'
' $("#{loading_id}").hide();'
' }}'
' }}));'
'}});'
'</script>')
html = html.format(url=url, graph_id=graph_id, img_id=img_id, loading_id=loading_id)
return html
display(HTML(image_html(figure_or_data)))
display(HTML(iframe_html(figure_or_data)))
CachedPlot([{'x': [1,2,3], 'y': [10,20,30]}])
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None
import numpy as np
x = np.random.randn(10000)
y = np.random.randn(10000)
CachedPlot([{'x': x, 'y': y, 'mode': 'markers'}])
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly DEBUG:requests.packages.urllib3.connectionpool:"POST /apigenimage/ HTTP/1.1" 200 None
INFO:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): plot.ly DEBUG:requests.packages.urllib3.connectionpool:"POST /clientresp HTTP/1.1" 200 None