Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d99b6cf

Browse files
committed
Merge pull request #3583 from ivanov/nb-small-things
Merged
2 parents 1442128 + 8e0ecc1 commit d99b6cf

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

IPython/nbconvert/exporters/export.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def decorator(*args, **kwargs):
8484
'export_python',
8585
'export_reveal',
8686
'export_rst',
87-
'export_by_name'
87+
'export_by_name',
88+
'get_export_names'
8889
]
8990

9091
@DocDecorator
@@ -211,5 +212,12 @@ def export_by_name(template_name, nb, config=None, transformers=None, filters=No
211212
if function_name in globals():
212213
return globals()[function_name](nb, config, transformers, filters)
213214
else:
214-
raise NameError("template not found")
215-
215+
raise NameError("template for `%s` not found" % function_name)
216+
217+
def get_export_names():
218+
"Return a list of the currently supported export targets"
219+
# grab everything after 'export_'
220+
l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
221+
# filter out the one method that is not a template
222+
l = [x for x in l if 'by_name' not in x]
223+
return sorted(l)

IPython/nbconvert/nbconvertapp.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
#From IPython
2626
from IPython.config.application import Application
27-
from IPython.utils.traitlets import Bool
27+
from IPython.utils.traitlets import Bool, Unicode
2828

29-
from .exporters.export import export_by_name
29+
from .exporters.export import export_by_name, get_export_names
3030
from .exporters.exporter import Exporter
3131
from .transformers import extractfigure
3232
from .utils.config import GlobalConfigurable
@@ -39,18 +39,35 @@
3939
KEYS_PROMPT_HEAD = "====================== Keys in Resources =================================="
4040
KEYS_PROMPT_BODY = """
4141
===========================================================================
42-
You are responsible for writting these files into the appropriate
43-
directorie(s) if need be. If you do not want to see this message, enable
42+
You are responsible for writing these files into the appropriate
43+
directory(ies) if need be. If you do not want to see this message, enable
4444
the 'write' (boolean) flag of the converter.
4545
===========================================================================
4646
"""
4747

48+
_examples = """
49+
ipython nbconvert rst Untitled0.ipynb # convert ipynb to ReStructured Text
50+
ipython nbconvert latex Untitled0.ipynb # convert ipynb to LaTeX
51+
ipython nbconvert reveal Untitled0.ipynb # convert to Reveal (HTML/JS) slideshow
52+
"""
53+
54+
4855
#-----------------------------------------------------------------------------
4956
#Classes and functions
5057
#-----------------------------------------------------------------------------
5158

5259
class NbConvertApp(Application):
53-
"""Application used to convert to and from notebook file type (*.ipynb)"""
60+
__doc__ = """IPython notebook conversion utility
61+
62+
Convert to and from notebook file type (*.ipynb)
63+
64+
ipython nbconvert TARGET FILENAME
65+
66+
Supported export TARGETs are: %s
67+
""" % (" ".join(get_export_names()))
68+
description = Unicode(__doc__)
69+
70+
examples = _examples
5471

5572
stdout = Bool(
5673
False, config=True,
@@ -118,10 +135,15 @@ def start(self, argv=None):
118135
ipynb_file = (self.extra_args)[2]
119136

120137
#Export
121-
return_value = export_by_name(export_type, ipynb_file)
122-
if return_value is None:
123-
print("Error: '%s' template not found." % export_type)
124-
return
138+
try:
139+
return_value = export_by_name(export_type, ipynb_file)
140+
except NameError as e:
141+
print("Error: '%s' exporter not found." % export_type,
142+
file=sys.stderr)
143+
print("Known exporters are:",
144+
"\n\t" + "\n\t".join(get_export_names()),
145+
file=sys.stderr)
146+
sys.exit(-1)
125147
else:
126148
(output, resources, exporter) = return_value
127149

0 commit comments

Comments
 (0)