@@ -31,6 +31,12 @@ def warning_on_one_line(message, category, filename, lineno, file=None, line=Non
31
31
except ImportError :
32
32
_matplotlylib_imported = False
33
33
34
+ try :
35
+ import IPython
36
+ _ipython_imported = True
37
+ except ImportError :
38
+ _ipython_imported = False
39
+
34
40
PLOTLY_DIR = os .path .join (os .path .expanduser ("~" ), ".plotly" )
35
41
CREDENTIALS_FILE = os .path .join (PLOTLY_DIR , ".credentials" )
36
42
CONFIG_FILE = os .path .join (PLOTLY_DIR , ".config" )
@@ -284,39 +290,21 @@ def embed(file_owner_or_url, file_id=None, width="100%", height=525):
284
290
return html (s , hide = False )
285
291
except :
286
292
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 )
293
+ if _ipython_imported :
294
+ if file_id :
295
+ url = "{plotly_domain}/~{un}/{fid}" .format (
296
+ plotly_domain = get_config_file ()['plotly_domain' ],
297
+ un = file_owner_or_url ,
298
+ fid = file_id )
299
+ else :
300
+ url = file_owner_or_url
301
+ return PlotlyDisplay (url )
292
302
else :
293
- url = file_owner_or_url
294
- try :
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 )
318
- except :
319
- pass
303
+ warnings .warn (
304
+ "Looks like you're not using IPython or Sage to embed this plot. "
305
+ "If you just want the *embed code*, try using `get_embed()` "
306
+ "instead."
307
+
320
308
321
309
322
310
### mpl-related tools ###
@@ -561,3 +549,40 @@ def _replace_newline(obj):
561
549
return s
562
550
else :
563
551
return obj # we return the actual reference... but DON'T mutate.
552
+
553
+
554
+ if _ipython_imported :
555
+ class PlotlyDisplay (IPython .core .display .HTML ):
556
+ """An IPython display object for use with plotly urls
557
+
558
+ PlotlyDisplay objects should be instantiated with a url for a plot.
559
+ IPython will *choose* the proper display representation from any
560
+ Python object, and using provided methods if they exist. By defining
561
+ the following, if an HTML display is unusable, the PlotlyDisplay
562
+ object can provide alternate representations.
563
+
564
+ """
565
+ def __init__ (self , url ):
566
+ self .resource = url
567
+ self .embed_code = get_embed (url )
568
+ super (PlotlyDisplay , self ).__init__ (data = self .embed_code )
569
+
570
+ def _repr_svg_ (self ):
571
+ url = self .resource + ".svg"
572
+ res = requests .get (url )
573
+ return res .content
574
+
575
+ def _repr_png_ (self ):
576
+ url = self .resource + ".png"
577
+ res = requests .get (url )
578
+ return res .content
579
+
580
+ def _repr_pdf_ (self ):
581
+ url = self .resource + ".pdf"
582
+ res = requests .get (url )
583
+ return res .content
584
+
585
+ def _repr_jpeg_ (self ):
586
+ url = self .resource + ".jpeg"
587
+ res = requests .get (url )
588
+ return res .content
0 commit comments