Repository: ipython/ipython

Issue:
nbconvert: Ability to specify name of output file
Brought up in a conversation between @Carreau and I.  Maybe add a traitlet to the `FilesWriter` and add a convenience flag to the `nbconvertapp.py`.



Discussion:
Open for conversation

Makes sense - should be easy, right?

Does it make sense for it to be a configurable on FilesWriter?  Or not, because one writer can be used to write out many notebooks.

+1

Well, I think the FilesWriter is supposed to be stateless...  But I was **not** thinking about supporting the renaming individual outputs, I was more thinking of the ability to prefix/suffix each file written (not the extracted files).

Something like:

```
output_name = Unicode(u'{name}.{extension}', config=True)
```

Which would be useful if someone wanted to export reveal and html which doesn't work now since the output names would cause the later to overwrite the earlier.

@Carreau  ?

But the question is where to you specify that, such that it is configurable per-notebook in a multi-export run.  Perhaps in NbConvertApp, and only allow it when `len(notebooks) == 1`?

I personally was not thinking about putting it in _nbconvertapp.py_, I was just thinking about replacing [this line](https://github.com/ipython/ipython/blob/master/IPython/nbconvert/writers/files.py#L96) with a str.format(...)

And not even caring if the user doesn't include the {name} key (or whatever it will be called)...  

ok


Resolving patch:
diff --git a/IPython/nbconvert/nbconvertapp.py b/IPython/nbconvert/nbconvertapp.py
--- a/IPython/nbconvert/nbconvertapp.py
+++ b/IPython/nbconvert/nbconvertapp.py
@@ -28,6 +28,7 @@
     Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum,
 )
 from IPython.utils.importstring import import_item
+from IPython.utils.text import dedent
 
 from .exporters.export import export_by_name, get_export_names, ExporterNameError
 from IPython.nbconvert import exporters, transformers, writers, post_processors
@@ -58,7 +59,8 @@ def validate(self, obj, value):
     'template' : 'Exporter.template_file',
     'notebooks' : 'NbConvertApp.notebooks',
     'writer' : 'NbConvertApp.writer_class',
-    'post': 'NbConvertApp.post_processor_class'
+    'post': 'NbConvertApp.post_processor_class',
+    'output': 'NbConvertApp.output_base'
 })
 
 nbconvert_flags = {}
@@ -93,6 +95,10 @@ def _classes_default(self):
 
         WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""")
 
+    output_base = Unicode('', config=True, help='''overwrite base name use for output files.
+            can only  be use when converting one notebook at a time.
+            ''')
+
     examples = Unicode(u"""
         The simplest way to use nbconvert is
         
@@ -253,11 +259,21 @@ def convert_notebooks(self):
         """
         # Export each notebook
         conversion_success = 0
+
+        if self.output_base != '' and len(self.notebooks) > 1:
+            print(dedent(
+            """UsageError: --output flag or `NbConvertApp.output_base` config option
+            cannot be used when converting multiple notebooks.
+            """))
+            self.exit(1)
+
         for notebook_filename in self.notebooks:
 
             # Get a unique key for the notebook and set it in the resources object.
             basename = os.path.basename(notebook_filename)
             notebook_name = basename[:basename.rfind('.')]
+            if self.output_base:
+                notebook_name = self.output_base
             resources = {}
             resources['unique_key'] = notebook_name
             resources['output_files_dir'] = '%s_files' % notebook_name


Test patch: