diff --git a/IPython/nbconvert/nbconvertapp.py b/IPython/nbconvert/nbconvertapp.py index 652a1a4..a8adf9a 100755 --- a/IPython/nbconvert/nbconvertapp.py +++ b/IPython/nbconvert/nbconvertapp.py @@ -188,6 +188,7 @@ class NbConvertApp(BaseIPythonApplication): notebook_name = basename[:basename.rfind('.')] resources = {} resources['unique_key'] = notebook_name + resources['output_files_dir'] = '%s_files' % notebook_name #Try to export try: diff --git a/IPython/nbconvert/transformers/extractfigure.py b/IPython/nbconvert/transformers/extractfigure.py index adef378..6abde79 100755 --- a/IPython/nbconvert/transformers/extractfigure.py +++ b/IPython/nbconvert/transformers/extractfigure.py @@ -15,6 +15,7 @@ notebook file. The extracted figures are returned in the 'resources' dictionary import base64 import sys +import os from IPython.utils.traitlets import Unicode from .base import Transformer @@ -50,8 +51,10 @@ class ExtractFigureTransformer(Transformer): """ #Get the unique key from the resource dict if it exists. If it does not - #exist, use 'figure' as the default. + #exist, use 'figure' as the default. Also, get files directory if it + #has been specified unique_key = resources.get('unique_key', 'figure') + output_files_dir = resources.get('output_files_dir', None) #Make sure figures key exists if not 'figures' in resources: @@ -77,7 +80,7 @@ class ExtractFigureTransformer(Transformer): data = data.encode("UTF-8") #Build a figure name - figure_name = self.figure_filename_template.format( + filename = self.figure_filename_template.format( unique_key=unique_key, cell_index=cell_index, index=index, @@ -87,10 +90,12 @@ class ExtractFigureTransformer(Transformer): # cell.outputs[i].svg_filename ... etc (svg in example) # Where # cell.outputs[i].svg contains the data - out[out_type + '_filename'] = figure_name + if output_files_dir is not None: + filename = os.path.join(output_files_dir, filename) + out[out_type + '_filename'] = filename #In the resources, make the figure available via # resources['figures']['filename'] = data - resources['figures'][figure_name] = data + resources['figures'][filename] = data return cell, resources