##// END OF EJS Templates
Add javascript library and css stylesheet loading to JS class.
Brian Granger -
Show More
@@ -344,11 +344,73 b' class JSON(DisplayObject):'
344 def _repr_json_(self):
344 def _repr_json_(self):
345 return self.data
345 return self.data
346
346
347 css_t = """$("head").append($("<link/>").attr({
348 rel: "stylesheet",
349 type: "text/css",
350 href: "%s"
351 }));
352 """
353
354 lib_t1 = """$.getScript("%s", function () {
355 """
356 lib_t2 = """});
357 """
347
358
348 class Javascript(DisplayObject):
359 class Javascript(DisplayObject):
349
360
361 def __init__(self, data=None, url=None, filename=None, lib=None, css=None):
362 """Create a Javascript display object given raw data.
363
364 When this object is returned by an expression or passed to the
365 display function, it will result in the data being displayed
366 in the frontend. If the data is a URL, the data will first be
367 downloaded and then displayed.
368
369 Parameters
370 ----------
371 data : unicode, str or bytes
372 The Javascript source code or a URL to download it from.
373 url : unicode
374 A URL to download the data from.
375 filename : unicode
376 Path to a local file to load the data from.
377 lib : list or str
378 A sequence of Javascript library URLs to load asynchronously before
379 running the source code. The full URLs of the libraries should
380 be given. A single Javascript library URL can also be given as a
381 string.
382 css: : list or str
383 A sequence of css files to load before running the source code.
384 The full URLs of the css files should be give. A single css URL
385 can also be given as a string.
386 """
387 if isinstance(lib, basestring):
388 lib = [lib]
389 elif lib is None:
390 lib = []
391 if isinstance(css, basestring):
392 css = [css]
393 elif css is None:
394 css = []
395 if not isinstance(lib, (list,tuple)):
396 raise TypeError('expected sequence, got: %r' % lib)
397 if not isinstance(css, (list,tuple)):
398 raise TypeError('expected sequence, got: %r' % css)
399 self.lib = lib
400 self.css = css
401 super(Javascript, self).__init__(data=data, url=url, filename=filename)
402
350 def _repr_javascript_(self):
403 def _repr_javascript_(self):
351 return self.data
404 r = ''
405 for c in self.css:
406 r += css_t % c
407 for l in self.lib:
408 r += lib_t1 % l
409 r += self.data
410 if self.lib:
411 for i in range(len(self.lib)):
412 r+= lib_t2
413 return r
352
414
353
415
354 class Image(DisplayObject):
416 class Image(DisplayObject):
General Comments 0
You need to be logged in to leave comments. Login now