-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
subclass IPython.core.display HTML object for more flexibility. #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
61b5087
d8b9e07
4e32590
2224ea7
d6dd588
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import absolute_import | ||
|
||
import plotly.tools as tls | ||
import imghdr | ||
import threading | ||
|
||
|
||
def test_plotly_display(): | ||
plot_info = {"un": "plotlyimagetest", "fid": "2"} | ||
url = "https://plot.ly/~{un}/{fid}".format(**plot_info) | ||
disp_obj = tls.embed(url) | ||
format_to_func = { | ||
"jpeg": jpeg_worker, | ||
"png": png_worker, | ||
"svg": svg_worker, | ||
"pdf": pdf_worker | ||
} | ||
results = {} | ||
threads = [] | ||
for f_format, func in format_to_func.items(): | ||
threads += [threading.Thread(target=func, args=(disp_obj, results))] | ||
threads[-1].setDaemon(True) | ||
threads[-1].start() | ||
for thread in threads: | ||
thread.join() | ||
for f_format in format_to_func: | ||
result = results.get(f_format, False) | ||
print("{f_format}: {result}".format(f_format=f_format, result=result)) | ||
assert results.get(f_format) | ||
|
||
|
||
def jpeg_worker(display_obj, results): | ||
img = display_obj._repr_jpeg_() | ||
if imghdr.what('', img) == "jpeg": | ||
results["jpeg"] = True | ||
|
||
|
||
def png_worker(display_obj, results): | ||
img = display_obj._repr_png_() | ||
if imghdr.what('', img) == "png": | ||
results["png"] = True | ||
|
||
|
||
def svg_worker(display_obj, results): | ||
img = display_obj._repr_svg_() | ||
if img[:4] == '<svg': | ||
results["svg"] = True | ||
|
||
|
||
def pdf_worker(display_obj, results): | ||
img = display_obj._repr_pdf_() | ||
if img[:4] == '%PDF': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto on pdf... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that checking the validity of the images should occur on plotly's On Tue, Aug 19, 2014 at 12:56 AM, Andrew [email protected] wrote:
|
||
results["pdf"] = True |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import os.path | ||
import warnings | ||
import six | ||
import requests | ||
|
||
from plotly import utils | ||
from plotly import exceptions | ||
|
@@ -30,6 +31,12 @@ def warning_on_one_line(message, category, filename, lineno, file=None, line=Non | |
except ImportError: | ||
_matplotlylib_imported = False | ||
|
||
try: | ||
import IPython | ||
_ipython_imported = True | ||
except ImportError: | ||
_ipython_imported = False | ||
|
||
PLOTLY_DIR = os.path.join(os.path.expanduser("~"), ".plotly") | ||
CREDENTIALS_FILE = os.path.join(PLOTLY_DIR, ".credentials") | ||
CONFIG_FILE = os.path.join(PLOTLY_DIR, ".config") | ||
|
@@ -283,11 +290,21 @@ def embed(file_owner_or_url, file_id=None, width="100%", height=525): | |
return html(s, hide=False) | ||
except: | ||
pass | ||
try: | ||
from IPython.display import HTML, display | ||
display(HTML(s)) | ||
except: | ||
pass | ||
if _ipython_imported: | ||
if file_id: | ||
url = "{plotly_domain}/~{un}/{fid}".format( | ||
plotly_domain=get_config_file()['plotly_domain'], | ||
un=file_owner_or_url, | ||
fid=file_id) | ||
else: | ||
url = file_owner_or_url | ||
return PlotlyDisplay(url) | ||
else: | ||
warnings.warn( | ||
"Looks like you're not using IPython or Sage to embed this plot. " | ||
"If you just want the *embed code*, try using `get_embed()` " | ||
"instead." | ||
"\nQuestions? [email protected]") | ||
|
||
|
||
### mpl-related tools ### | ||
|
@@ -532,3 +549,40 @@ def _replace_newline(obj): | |
return s | ||
else: | ||
return obj # we return the actual reference... but DON'T mutate. | ||
|
||
|
||
if _ipython_imported: | ||
class PlotlyDisplay(IPython.core.display.HTML): | ||
"""An IPython display object for use with plotly urls | ||
|
||
PlotlyDisplay objects should be instantiated with a url for a plot. | ||
IPython will *choose* the proper display representation from any | ||
Python object, and using provided methods if they exist. By defining | ||
the following, if an HTML display is unusable, the PlotlyDisplay | ||
object can provide alternate representations. | ||
|
||
""" | ||
def __init__(self, url): | ||
self.resource = url | ||
self.embed_code = get_embed(url) | ||
super(PlotlyDisplay, self).__init__(data=self.embed_code) | ||
|
||
def _repr_svg_(self): | ||
url = self.resource + ".svg" | ||
res = requests.get(url) | ||
return res.content | ||
|
||
def _repr_png_(self): | ||
url = self.resource + ".png" | ||
res = requests.get(url) | ||
return res.content | ||
|
||
def _repr_pdf_(self): | ||
url = self.resource + ".pdf" | ||
res = requests.get(url) | ||
return res.content | ||
|
||
def _repr_jpeg_(self): | ||
url = self.resource + ".jpeg" | ||
res = requests.get(url) | ||
return res.content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
anyone know a way to check if a file is valid svg? (i'm guessing this is not a good way to do it...) (imghdr lib doesn't help...)