##// END OF EJS Templates
allow Image("filename")
MinRK -
Show More
@@ -7,7 +7,7 b' Authors:'
7 """
7 """
8
8
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2008-2011 The IPython Development Team
10 # Copyright (C) 2013 The IPython Development Team
11 #
11 #
12 # Distributed under the terms of the BSD License. The full license is in
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
13 # the file COPYING, distributed as part of this software.
@@ -19,6 +19,8 b' Authors:'
19
19
20 from __future__ import print_function
20 from __future__ import print_function
21
21
22 import os
23
22 from .displaypub import (
24 from .displaypub import (
23 publish_pretty, publish_html,
25 publish_pretty, publish_html,
24 publish_latex, publish_svg,
26 publish_latex, publish_svg,
@@ -29,6 +31,17 b' from .displaypub import ('
29 from IPython.utils.py3compat import string_types
31 from IPython.utils.py3compat import string_types
30
32
31 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 # utility functions
35 #-----------------------------------------------------------------------------
36
37 def _safe_exists(path):
38 """check path, but don't let exceptions raise"""
39 try:
40 return os.path.exists(path)
41 except Exception:
42 return False
43
44 #-----------------------------------------------------------------------------
32 # Main functions
45 # Main functions
33 #-----------------------------------------------------------------------------
46 #-----------------------------------------------------------------------------
34
47
@@ -248,20 +261,26 b' class DisplayObject(object):'
248 Parameters
261 Parameters
249 ----------
262 ----------
250 data : unicode, str or bytes
263 data : unicode, str or bytes
251 The raw data or a URL to download the data from.
264 The raw data or a URL or file to load the data from
252 url : unicode
265 url : unicode
253 A URL to download the data from.
266 A URL to download the data from.
254 filename : unicode
267 filename : unicode
255 Path to a local file to load the data from.
268 Path to a local file to load the data from.
256 """
269 """
257 if data is not None and isinstance(data, string_types) and data.startswith('http'):
270 if data is not None and isinstance(data, string_types):
258 self.url = data
271 if data.startswith('http') and url is None:
259 self.filename = None
272 url = data
260 self.data = None
273 filename = None
261 else:
274 data = None
262 self.data = data
275 elif _safe_exists(data) and filename is None:
263 self.url = url
276 url = None
264 self.filename = None if filename is None else unicode(filename)
277 filename = data
278 data = None
279
280 self.data = data
281 self.url = url
282 self.filename = None if filename is None else unicode(filename)
283
265 self.reload()
284 self.reload()
266
285
267 def reload(self):
286 def reload(self):
@@ -479,7 +498,9 b' class Image(DisplayObject):'
479 ext = self._find_ext(url)
498 ext = self._find_ext(url)
480 elif data is None:
499 elif data is None:
481 raise ValueError("No image data found. Expecting filename, url, or data.")
500 raise ValueError("No image data found. Expecting filename, url, or data.")
482 elif isinstance(data, string_types) and data.startswith('http'):
501 elif isinstance(data, string_types) and (
502 data.startswith('http') or _safe_exists(data)
503 ):
483 ext = self._find_ext(data)
504 ext = self._find_ext(data)
484 else:
505 else:
485 ext = None
506 ext = None
General Comments 0
You need to be logged in to leave comments. Login now