@@ -60,13 +60,13 @@ def get_exporter(format, **kwargs):
60
60
from nbconvert .exporters .base import get_exporter
61
61
except ImportError as e :
62
62
raise web .HTTPError (500 , "Could not import nbconvert: %s" % e )
63
-
63
+
64
64
try :
65
65
Exporter = get_exporter (format )
66
66
except KeyError :
67
67
# should this be 400?
68
68
raise web .HTTPError (404 , u"No exporter for format: %s" % format )
69
-
69
+
70
70
try :
71
71
return Exporter (** kwargs )
72
72
except Exception as e :
@@ -76,40 +76,50 @@ def get_exporter(format, **kwargs):
76
76
class NbconvertFileHandler (IPythonHandler ):
77
77
78
78
SUPPORTED_METHODS = ('GET' ,)
79
-
79
+
80
80
@web .authenticated
81
81
def get (self , format , path ):
82
-
82
+
83
83
exporter = get_exporter (format , config = self .config , log = self .log )
84
-
84
+
85
85
path = path .strip ('/' )
86
86
# If the notebook relates to a real file (default contents manager),
87
87
# give its path to nbconvert.
88
88
if hasattr (self .contents_manager , '_get_os_path' ):
89
89
os_path = self .contents_manager ._get_os_path (path )
90
+ ext_resources_dir , basename = os .path .split (os_path )
90
91
else :
91
- os_path = ''
92
+ ext_resources_dir = None
92
93
93
94
model = self .contents_manager .get (path = path )
94
95
name = model ['name' ]
95
96
if model ['type' ] != 'notebook' :
96
97
# not a notebook, redirect to files
97
98
return FilesRedirectHandler .redirect_to_files (self , path )
98
99
100
+ nb = model ['content' ]
101
+
99
102
self .set_header ('Last-Modified' , model ['last_modified' ])
100
103
104
+ # create resources dictionary
105
+ mod_date = model ['last_modified' ].strftime (text .date_format )
106
+ nb_title = os .path .splitext (name )[0 ]
107
+
108
+ resource_dict = {
109
+ "metadata" : {
110
+ "name" : nb_title ,
111
+ "modified_date" : mod_date
112
+ },
113
+ "config_dir" : self .application .settings ['config_dir' ]
114
+ }
115
+
116
+ if ext_resources_dir :
117
+ resource_dict ['metadata' ]['path' ] = ext_resources_dir
118
+
101
119
try :
102
120
output , resources = exporter .from_notebook_node (
103
- model ['content' ],
104
- resources = {
105
- "metadata" : {
106
- "name" : name [:name .rfind ('.' )],
107
- "modified_date" : (model ['last_modified' ]
108
- .strftime (text .date_format )),
109
- "path" : os_path
110
- },
111
- "config_dir" : self .application .settings ['config_dir' ],
112
- }
121
+ nb ,
122
+ resources = resource_dict
113
123
)
114
124
except Exception as e :
115
125
self .log .exception ("nbconvert failed: %s" , e )
@@ -136,11 +146,11 @@ class NbconvertPostHandler(IPythonHandler):
136
146
@web .authenticated
137
147
def post (self , format ):
138
148
exporter = get_exporter (format , config = self .config )
139
-
149
+
140
150
model = self .get_json_body ()
141
151
name = model .get ('name' , 'notebook.ipynb' )
142
152
nbnode = from_dict (model ['content' ])
143
-
153
+
144
154
try :
145
155
output , resources = exporter .from_notebook_node (nbnode , resources = {
146
156
"metadata" : {"name" : name [:name .rfind ('.' )],},
0 commit comments