Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61b5087

Browse files
committed
subclass IPython.core.display HTML object for more flexibility.
By defining `_repr_*_` functions, IPython will just call these functions when a non-HTML representation is required.
1 parent b50b491 commit 61b5087

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

plotly/tools.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os.path
1414
import warnings
1515
import six
16+
import requests
1617

1718
from plotly import utils
1819
from plotly import exceptions
@@ -283,9 +284,37 @@ def embed(file_owner_or_url, file_id=None, width="100%", height=525):
283284
return html(s, hide=False)
284285
except:
285286
pass
287+
if file_id:
288+
url = "{plotly_domain}/~{un}/{fid}".format(
289+
plotly_domain=get_config_file()['plotly_domain'],
290+
un=file_owner_or_url,
291+
fid=file_id)
292+
else:
293+
url = file_owner_or_url
286294
try:
287-
from IPython.display import HTML, display
288-
display(HTML(s))
295+
from IPython.core.display import HTML
296+
class PlotlyFrame(HTML):
297+
def __init__(self, url):
298+
self.resource = url
299+
self.embed_code = get_embed(url)
300+
super(PlotlyFrame, self).__init__(data=self.embed_code)
301+
def _repr_svg_(self):
302+
url = self.resource + ".svg"
303+
res = requests.get(url)
304+
return res.content
305+
def _repr_png_(self):
306+
url = self.resource + ".png"
307+
res = requests.get(url)
308+
return res.content
309+
def _repr_pdf_(self):
310+
url = self.resource + ".pdf"
311+
res = requests.get(url)
312+
return res.content
313+
def _repr_jpeg_(self):
314+
url = self.resource + ".jpeg"
315+
res = requests.get(url)
316+
return res.content
317+
return PlotlyFrame(url)
289318
except:
290319
pass
291320

0 commit comments

Comments
 (0)