Skip to content

Commit

Permalink
Merge pull request #4727 from takluyver/nbconvert-template-magic
Browse files Browse the repository at this point in the history
Remove Nbconvert template loading magic based on module name.

A little more explicit now, plus a few traitlets cleaned up.
  • Loading branch information
minrk committed Jan 8, 2014
2 parents 7a3cfad + 12ea888 commit 902758e
Show file tree
Hide file tree
Showing 23 changed files with 53 additions and 62 deletions.
17 changes: 7 additions & 10 deletions IPython/nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Imports
#-----------------------------------------------------------------------------

from IPython.utils.traitlets import Unicode, List
import os

from IPython.nbconvert import preprocessors
from IPython.config import Config
Expand All @@ -31,17 +31,14 @@ class HTMLExporter(TemplateExporter):
filters, just change the 'template_file' config option.
"""

file_extension = Unicode(
'html', config=True,
help="Extension of the file that should be written to disk"
)
def _file_extension_default(self):
return 'html'

mime_type = Unicode('text/html', config=True,
help="MIME type of the result file, for HTTP response headers."
)
def _default_template_path_default(self):
return os.path.join("..", "templates", "html")

default_template = Unicode('full', config=True, help="""Flavor of the data
format to use. I.E. 'full' or 'basic'""")
def _template_file_default(self):
return 'full'

output_mimetype = 'text/html'

Expand Down
23 changes: 10 additions & 13 deletions IPython/nbconvert/exporters/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os

# IPython imports
from IPython.utils.traitlets import Unicode, List
from IPython.utils.traitlets import Unicode
from IPython.config import Config

from IPython.nbconvert import filters, preprocessors
Expand All @@ -35,22 +35,19 @@ class LatexExporter(TemplateExporter):
'template_file' config option. Place your template in the special "/latex"
subfolder of the "../templates" folder.
"""

file_extension = Unicode(
'tex', config=True,
help="Extension of the file that should be written to disk")

default_template = Unicode('article', config=True, help="""Template of the
data format to use. I.E. 'article' or 'report'""")
def _file_extension_default(self):
return 'tex'

def _template_file_default(self):
return 'article'

#Latex constants
default_template_path = Unicode(
os.path.join("..", "templates", "latex"), config=True,
help="Path where the template files are located.")
def _default_template_path_default(self):
return os.path.join("..", "templates", "latex")

template_skeleton_path = Unicode(
os.path.join("..", "templates", "latex", "skeleton"), config=True,
help="Path where the template skeleton files are located.")
def _template_skeleton_path_default(self):
return os.path.join("..", "templates", "latex", "skeleton")

#Special Jinja2 syntax that will not conflict when exporting latex.
jinja_comment_block_start = Unicode("((=", config=True)
Expand Down
9 changes: 5 additions & 4 deletions IPython/nbconvert/exporters/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#-----------------------------------------------------------------------------

from IPython.config import Config
from IPython.utils.traitlets import Unicode

from .templateexporter import TemplateExporter

Expand All @@ -26,9 +25,11 @@ class MarkdownExporter(TemplateExporter):
Exports to a markdown document (.md)
"""

file_extension = Unicode(
'md', config=True,
help="Extension of the file that should be written to disk")
def _file_extension_default(self):
return 'md'

def _template_file_default(self):
return 'markdown'

output_mimetype = 'text/markdown'

Expand Down
11 changes: 5 additions & 6 deletions IPython/nbconvert/exporters/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# Imports
#-----------------------------------------------------------------------------

from IPython.utils.traitlets import Unicode

from .templateexporter import TemplateExporter

#-----------------------------------------------------------------------------
Expand All @@ -24,9 +22,10 @@ class PythonExporter(TemplateExporter):
"""
Exports a Python code file.
"""

file_extension = Unicode(
'py', config=True,
help="Extension of the file that should be written to disk")
def _file_extension_default(self):
return 'py'

def _template_file_default(self):
return 'python'

output_mimetype = 'text/x-python'
9 changes: 5 additions & 4 deletions IPython/nbconvert/exporters/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# Imports
#-----------------------------------------------------------------------------

from IPython.utils.traitlets import Unicode
from IPython.config import Config

from .templateexporter import TemplateExporter
Expand All @@ -26,9 +25,11 @@ class RSTExporter(TemplateExporter):
Exports restructured text documents.
"""

file_extension = Unicode(
'rst', config=True,
help="Extension of the file that should be written to disk")
def _file_extension_default(self):
return 'rst'

def _template_file_default(self):
return 'rst'

output_mimetype = 'text/restructuredtext'

Expand Down
14 changes: 5 additions & 9 deletions IPython/nbconvert/exporters/slides.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# Imports
#-----------------------------------------------------------------------------

from IPython.utils.traitlets import Unicode

from IPython.nbconvert import preprocessors
from IPython.config import Config

Expand All @@ -26,15 +24,13 @@
class SlidesExporter(HTMLExporter):
"""Exports HTML slides with reveal.js"""

file_extension = Unicode(
'slides.html', config=True,
help="Extension of the file that should be written to disk"
)
def _file_extension_default(self):
return 'slides.html'

output_mimetype = 'text/html'
def _template_file_default(self):
return 'slides_reveal'

default_template = Unicode('reveal', config=True, help="""Template of the
data format to use. I.E. 'reveal'""")
output_mimetype = 'text/html'

@property
def default_config(self):
Expand Down
3 changes: 0 additions & 3 deletions IPython/nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,12 @@ def _load_template(self):
# template by name with extension added, then try loading the template
# as if the name is explicitly specified, then try the name as a
# 'flavor', and lastly just try to load the template by module name.
module_name = self.__module__.rsplit('.', 1)[-1]
try_names = []
if self.template_file:
try_names.extend([
self.template_file + self.template_extension,
self.template_file,
module_name + '_' + self.template_file + self.template_extension,
])
try_names.append(module_name + self.template_extension)
for try_name in try_names:
self.log.debug("Attempting to load template %s", try_name)
try:
Expand Down
2 changes: 1 addition & 1 deletion IPython/nbconvert/exporters/tests/test_slides.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ def test_export_reveal(self):
"""
Can a SlidesExporter export using the 'reveal' template?
"""
(output, resources) = SlidesExporter(template_file='reveal').from_filename(self._get_notebook())
(output, resources) = SlidesExporter(template_file='slides_reveal').from_filename(self._get_notebook())
assert len(output) > 0
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{%- extends 'html_basic.tpl' -%}
{%- extends 'basic.tpl' -%}


{%- block header -%}
Expand Down Expand Up @@ -67,4 +67,4 @@ init_mathjax();

{% block footer %}
</html>
{% endblock footer %}
{% endblock footer %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{%- extends 'html_basic.tpl' -%}
{%- extends 'basic.tpl' -%}


{%- block any_cell scoped -%}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion IPython/nbconvert/templates/latex/style_bw_ipython.tplx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
((= Black&white ipython input/output style =))

((*- extends 'latex_base.tplx' -*))
((*- extends 'base.tplx' -*))

%===============================================================================
% Input
Expand Down
2 changes: 1 addition & 1 deletion IPython/nbconvert/templates/latex/style_bw_python.tplx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
((= Black&white Python input/output style =))

((*- extends 'latex_base.tplx' -*))
((*- extends 'base.tplx' -*))

%===============================================================================
% Input
Expand Down
2 changes: 1 addition & 1 deletion IPython/nbconvert/templates/latex/style_ipython.tplx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
((= IPython input/output style =))

((*- extends 'latex_base.tplx' -*))
((*- extends 'base.tplx' -*))

% Custom definitions
((* block definitions *))
Expand Down
2 changes: 1 addition & 1 deletion IPython/nbconvert/templates/latex/style_python.tplx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
((= Python input/output style =))

((*- extends 'latex_base.tplx' -*))
((*- extends 'base.tplx' -*))

% Custom definitions
((* block definitions *))
Expand Down
2 changes: 1 addition & 1 deletion IPython/nbconvert/tests/test_nbconvertapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_template(self):
"""
with self.create_temp_cwd(['notebook2.ipynb']):
self.call('nbconvert --log-level 0 --to slides '
'notebook2.ipynb --template reveal')
'notebook2.ipynb')
assert os.path.isfile('notebook2.slides.html')
with open('notebook2.slides.html') as f:
assert '/reveal.css' in f.read()
Expand Down
11 changes: 7 additions & 4 deletions setupbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def find_package_data():

os.chdir(os.path.join('tests',))
js_tests = glob('casperjs/*.*') + glob('casperjs/*/*')

os.chdir(os.path.join(cwd, 'IPython', 'nbconvert'))
nbconvert_templates = [os.path.join(dirpath, '*.*')
for dirpath, _, _ in os.walk('templates')]

os.chdir(cwd)

package_data = {
Expand All @@ -157,10 +162,8 @@ def find_package_data():
'IPython.html' : ['templates/*'] + static_data,
'IPython.html.tests' : js_tests,
'IPython.qt.console' : ['resources/icon/*.svg'],
'IPython.nbconvert' : ['templates/*.tpl', 'templates/latex/*.tplx',
'templates/latex/skeleton/*.tplx', 'templates/skeleton/*',
'templates/reveal_internals/*.tpl', 'tests/files/*.*',
'exporters/tests/files/*.*'],
'IPython.nbconvert' : nbconvert_templates +
['tests/files/*.*', 'exporters/tests/files/*.*'],
'IPython.nbformat' : ['tests/*.ipynb']
}
return package_data
Expand Down

0 comments on commit 902758e

Please sign in to comment.