##// 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 12 # Distributed under the terms of the BSD License. The full license is in
13 13 # the file COPYING, distributed as part of this software.
@@ -19,6 +19,8 b' Authors:'
19 19
20 20 from __future__ import print_function
21 21
22 import os
23
22 24 from .displaypub import (
23 25 publish_pretty, publish_html,
24 26 publish_latex, publish_svg,
@@ -29,6 +31,17 b' from .displaypub import ('
29 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 45 # Main functions
33 46 #-----------------------------------------------------------------------------
34 47
@@ -248,20 +261,26 b' class DisplayObject(object):'
248 261 Parameters
249 262 ----------
250 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 265 url : unicode
253 266 A URL to download the data from.
254 267 filename : unicode
255 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'):
258 self.url = data
259 self.filename = None
260 self.data = None
261 else:
262 self.data = data
263 self.url = url
264 self.filename = None if filename is None else unicode(filename)
270 if data is not None and isinstance(data, string_types):
271 if data.startswith('http') and url is None:
272 url = data
273 filename = None
274 data = None
275 elif _safe_exists(data) and filename is None:
276 url = None
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 284 self.reload()
266 285
267 286 def reload(self):
@@ -479,7 +498,9 b' class Image(DisplayObject):'
479 498 ext = self._find_ext(url)
480 499 elif data is None:
481 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 504 ext = self._find_ext(data)
484 505 else:
485 506 ext = None
General Comments 0
You need to be logged in to leave comments. Login now