0
|
1 # -*- coding: utf-8 -*-
|
|
2
|
3
|
3 #from galaxy.datatypes.json_datatyp import Json as JsonClass
|
|
4 from galaxy.datatypes.data import Text
|
0
|
5 from galaxy.datatypes.data import get_file_peek
|
3
|
6 from galaxy import util
|
0
|
7 import subprocess
|
|
8 import tempfile
|
3
|
9 import logging
|
0
|
10 import json
|
|
11 import os
|
|
12
|
3
|
13 log = logging.getLogger(__name__)
|
|
14
|
|
15 #class Ipynb( JsonClass ):
|
|
16 class Ipynb( Text ):
|
0
|
17 file_ext = "ipynb"
|
|
18
|
|
19 def set_peek( self, dataset, is_multi_byte=False ):
|
|
20 if not dataset.dataset.purged:
|
|
21 dataset.peek = get_file_peek( dataset.file_name, is_multi_byte=is_multi_byte )
|
|
22 dataset.blurb = "IPython Notebook"
|
|
23 else:
|
|
24 dataset.peek = 'file does not exist'
|
|
25 dataset.blurb = 'file purged from disc'
|
|
26
|
|
27 def sniff( self, filename ):
|
|
28 """
|
|
29 Try to load the string with the json module. If successful it's a json file.
|
|
30 """
|
|
31 try:
|
1
|
32 ipynb = json.load( open(filename) )
|
|
33 if ipynb.get('nbformat', False) != False and ipynb.get('metadata', False):
|
0
|
34 return True
|
|
35 else:
|
|
36 return False
|
|
37 except:
|
|
38 return False
|
|
39
|
|
40 def display_data(self, trans, dataset, preview=False, filename=None, to_ext=None, chunk=None, **kwd):
|
|
41 preview = util.string_as_bool( preview )
|
|
42 if chunk:
|
|
43 return self.get_chunk(trans, dataset, chunk)
|
|
44 elif to_ext or not preview:
|
|
45 return self._serve_raw(trans, dataset, to_ext)
|
|
46 else:
|
|
47 ofile_handle = tempfile.NamedTemporaryFile(delete=False)
|
3
|
48 ofilename = ofile_handle.name
|
0
|
49 ofile_handle.close()
|
|
50 try:
|
3
|
51 cmd = 'ipython nbconvert --to html --template basic %s --output %s' % (dataset.file_name, ofilename)
|
|
52 subprocess.call(cmd, shell=True)
|
0
|
53 ofilename = '%s.html' % ofilename
|
|
54 except:
|
|
55 ofilename = dataset.file_name
|
|
56 log.exception( 'Command "%s" failed. Could not convert the IPython Notebook to HTML, defaulting to plain text.' % cmd )
|
|
57 return open( ofilename )
|
|
58
|
|
59 def set_meta( self, dataset, **kwd ):
|
|
60 """
|
|
61 Set the number of models in dataset.
|
|
62 """
|
|
63 pass
|
|
64
|
|
65
|