##// END OF EJS Templates
Merge pull request #3734 from jdfreder/file_subdir...
Matthias Bussonnier -
r11642:321025c8 merge
parent child Browse files
Show More
@@ -131,7 +131,7 b' class Exporter(Configurable):'
131
131
132 default_transformers = List([nbtransformers.coalesce_streams,
132 default_transformers = List([nbtransformers.coalesce_streams,
133 nbtransformers.SVG2PDFTransformer,
133 nbtransformers.SVG2PDFTransformer,
134 nbtransformers.ExtractFigureTransformer,
134 nbtransformers.ExtractOutputTransformer,
135 nbtransformers.CSSHTMLHeaderTransformer,
135 nbtransformers.CSSHTMLHeaderTransformer,
136 nbtransformers.RevealHelpTransformer,
136 nbtransformers.RevealHelpTransformer,
137 nbtransformers.LatexTransformer,
137 nbtransformers.LatexTransformer,
@@ -88,7 +88,7 b' class LatexExporter(Exporter):'
88 'NbConvertBase': {
88 'NbConvertBase': {
89 'display_data_priority' : ['latex', 'pdf', 'png', 'jpg', 'svg', 'jpeg', 'text']
89 'display_data_priority' : ['latex', 'pdf', 'png', 'jpg', 'svg', 'jpeg', 'text']
90 },
90 },
91 'ExtractFigureTransformer': {
91 'ExtractOutputTransformer': {
92 'enabled':True
92 'enabled':True
93 },
93 },
94 'SVG2PDFTransformer': {
94 'SVG2PDFTransformer': {
@@ -37,6 +37,6 b' class RSTExporter(Exporter):'
37
37
38 @property
38 @property
39 def default_config(self):
39 def default_config(self):
40 c = Config({'ExtractFigureTransformer':{'enabled':True}})
40 c = Config({'ExtractOutputTransformer':{'enabled':True}})
41 c.merge(super(RSTExporter,self).default_config)
41 c.merge(super(RSTExporter,self).default_config)
42 return c
42 return c
@@ -45,16 +45,16 b' class TestExporter(ExportersTestsBase):'
45 assert len(output) > 0
45 assert len(output) > 0
46
46
47
47
48 def test_extract_figures(self):
48 def test_extract_outputs(self):
49 """
49 """
50 If the ExtractFigureTransformer is enabled, are figures extracted?
50 If the ExtractOutputTransformer is enabled, are outputs extracted?
51 """
51 """
52 config = Config({'ExtractFigureTransformer': {'enabled': True}})
52 config = Config({'ExtractOutputTransformer': {'enabled': True}})
53 exporter = self._make_exporter(config=config)
53 exporter = self._make_exporter(config=config)
54 (output, resources) = exporter.from_filename(self._get_notebook())
54 (output, resources) = exporter.from_filename(self._get_notebook())
55 assert resources is not None
55 assert resources is not None
56 assert 'figures' in resources
56 assert 'outputs' in resources
57 assert len(resources['figures']) > 0
57 assert len(resources['outputs']) > 0
58
58
59
59
60 def test_transformer_class(self):
60 def test_transformer_class(self):
@@ -188,6 +188,7 b' class NbConvertApp(BaseIPythonApplication):'
188 notebook_name = basename[:basename.rfind('.')]
188 notebook_name = basename[:basename.rfind('.')]
189 resources = {}
189 resources = {}
190 resources['unique_key'] = notebook_name
190 resources['unique_key'] = notebook_name
191 resources['output_files_dir'] = '%s_files' % notebook_name
191
192
192 #Try to export
193 # Try to export
193 try:
194 try:
@@ -53,8 +53,8 b' class TestNbConvertApp(TestsBase):'
53 with self.create_temp_cwd(['notebook*.ipynb']):
53 with self.create_temp_cwd(['notebook*.ipynb']):
54 assert not 'error' in self.call([IPYTHON, 'nbconvert',
54 assert not 'error' in self.call([IPYTHON, 'nbconvert',
55 '--format="python"', '--notebooks=["*.ipynb"]']).lower()
55 '--format="python"', '--notebooks=["*.ipynb"]']).lower()
56 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook1.py'))
56 assert os.path.isfile('notebook1.py')
57 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook2.py'))
57 assert os.path.isfile('notebook2.py')
58
58
59
59
60 def test_glob_subdir(self):
60 def test_glob_subdir(self):
@@ -65,8 +65,8 b' class TestNbConvertApp(TestsBase):'
65 self.copy_files_to(['notebook*.ipynb'], 'subdir/')
65 self.copy_files_to(['notebook*.ipynb'], 'subdir/')
66 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
66 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
67 '--notebooks=["%s"]' % os.path.join('subdir', '*.ipynb')]).lower()
67 '--notebooks=["%s"]' % os.path.join('subdir', '*.ipynb')]).lower()
68 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook1.py'))
68 assert os.path.isfile('notebook1.py')
69 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook2.py'))
69 assert os.path.isfile('notebook2.py')
70
70
71
71
72 def test_explicit(self):
72 def test_explicit(self):
@@ -76,8 +76,8 b' class TestNbConvertApp(TestsBase):'
76 with self.create_temp_cwd(['notebook*.ipynb']):
76 with self.create_temp_cwd(['notebook*.ipynb']):
77 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
77 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
78 '--notebooks=["notebook2.ipynb"]']).lower()
78 '--notebooks=["notebook2.ipynb"]']).lower()
79 assert not os.path.isfile(os.path.join('nbconvert_build', 'notebook1.py'))
79 assert not os.path.isfile('notebook1.py')
80 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook2.py'))
80 assert os.path.isfile('notebook2.py')
81
81
82
82
83 def test_glob_explicit(self):
83 def test_glob_explicit(self):
@@ -87,8 +87,8 b' class TestNbConvertApp(TestsBase):'
87 with self.create_temp_cwd(['notebook*.ipynb']):
87 with self.create_temp_cwd(['notebook*.ipynb']):
88 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
88 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
89 '--notebooks=["*.ipynb", "notebook1.ipynb", "notebook2.ipynb"]']).lower()
89 '--notebooks=["*.ipynb", "notebook1.ipynb", "notebook2.ipynb"]']).lower()
90 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook1.py'))
90 assert os.path.isfile('notebook1.py')
91 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook2.py'))
91 assert os.path.isfile('notebook2.py')
92
92
93
93
94 def test_explicit_glob(self):
94 def test_explicit_glob(self):
@@ -98,8 +98,8 b' class TestNbConvertApp(TestsBase):'
98 with self.create_temp_cwd(['notebook*.ipynb']):
98 with self.create_temp_cwd(['notebook*.ipynb']):
99 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
99 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--format="python"',
100 '--notebooks=["notebook1.ipynb", "notebook2.ipynb", "*.ipynb"]']).lower()
100 '--notebooks=["notebook1.ipynb", "notebook2.ipynb", "*.ipynb"]']).lower()
101 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook1.py'))
101 assert os.path.isfile('notebook1.py')
102 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook2.py'))
102 assert os.path.isfile('notebook2.py')
103
103
104
104
105 def test_default_config(self):
105 def test_default_config(self):
@@ -108,8 +108,8 b' class TestNbConvertApp(TestsBase):'
108 """
108 """
109 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']):
109 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']):
110 assert not 'error' in self.call([IPYTHON, 'nbconvert']).lower()
110 assert not 'error' in self.call([IPYTHON, 'nbconvert']).lower()
111 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook1.py'))
111 assert os.path.isfile('notebook1.py')
112 assert not os.path.isfile(os.path.join('nbconvert_build', 'notebook2.py'))
112 assert not os.path.isfile('notebook2.py')
113
113
114
114
115 def test_override_config(self):
115 def test_override_config(self):
@@ -119,5 +119,5 b' class TestNbConvertApp(TestsBase):'
119 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py',
119 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py',
120 'override.py']):
120 'override.py']):
121 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--config="override.py"']).lower()
121 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--config="override.py"']).lower()
122 assert not os.path.isfile(os.path.join('nbconvert_build', 'notebook1.py'))
122 assert not os.path.isfile('notebook1.py')
123 assert os.path.isfile(os.path.join('nbconvert_build', 'notebook2.py'))
123 assert os.path.isfile('notebook2.py')
@@ -2,7 +2,7 b''
2 from .base import Transformer
2 from .base import Transformer
3 from .convertfigures import ConvertFiguresTransformer
3 from .convertfigures import ConvertFiguresTransformer
4 from .svg2pdf import SVG2PDFTransformer
4 from .svg2pdf import SVG2PDFTransformer
5 from .extractfigure import ExtractFigureTransformer
5 from .extractoutput import ExtractOutputTransformer
6 from .revealhelp import RevealHelpTransformer
6 from .revealhelp import RevealHelpTransformer
7 from .latex import LatexTransformer
7 from .latex import LatexTransformer
8 from .sphinx import SphinxTransformer
8 from .sphinx import SphinxTransformer
@@ -49,7 +49,7 b' class ConvertFiguresTransformer(Transformer):'
49 #Loop through all of the datatypes of the outputs in the cell.
49 # Loop through all of the datatypes of the outputs in the cell.
50 for index, cell_out in enumerate(cell.get('outputs', [])):
50 for index, cell_out in enumerate(cell.get('outputs', [])):
51 for data_type, data in cell_out.items():
51 for data_type, data in cell_out.items():
52 # this must run *before* extract figures,
52 # this must run *before* extract outputs,
53 # so figure_name and filename do not exist
53 # so figure_name and filename do not exist
54 self._convert_figure(cell_out, resources, data_type, data)
54 self._convert_figure(cell_out, resources, data_type, data)
55 return cell, resources
55 return cell, resources
@@ -1,5 +1,5 b''
1 """Module containing a transformer that extracts all of the figures from the
1 """Module containing a transformer that extracts all of the outputs from the
2 notebook file. The extracted figures are returned in the 'resources' dictionary.
2 notebook file. The extracted outputs are returned in the 'resources' dictionary.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2013, the IPython Development Team.
5 # Copyright (c) 2013, the IPython Development Team.
@@ -15,6 +15,7 b" notebook file. The extracted figures are returned in the 'resources' dictionary"
15
15
16 import base64
16 import base64
17 import sys
17 import sys
18 import os
18
19
19 from IPython.utils.traitlets import Unicode
20 from IPython.utils.traitlets import Unicode
20 from .base import Transformer
21 from .base import Transformer
@@ -24,10 +25,10 b' from IPython.utils import py3compat'
24 # Classes
25 # Classes
25 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
26
27
27 class ExtractFigureTransformer(Transformer):
28 class ExtractOutputTransformer(Transformer):
28 """
29 """
29 Extracts all of the figures from the notebook file. The extracted
30 Extracts all of the outputs from the notebook file. The extracted
30 figures are returned in the 'resources' dictionary.
31 outputs are returned in the 'resources' dictionary.
31 """
32 """
32
33
33 figure_filename_template = Unicode(
34 figure_filename_template = Unicode(
@@ -50,12 +51,14 b' class ExtractFigureTransformer(Transformer):'
50 """
51 """
51
52
52 #Get the unique key from the resource dict if it exists. If it does not
53 #Get the unique key from the resource dict if it exists. If it does not
53 #exist, use 'figure' as the default.
54 #exist, use 'figure' as the default. Also, get files directory if it
55 #has been specified
54 unique_key = resources.get('unique_key', 'figure')
56 unique_key = resources.get('unique_key', 'figure')
57 output_files_dir = resources.get('output_files_dir', None)
55
58
56 #Make sure figures key exists
59 #Make sure outputs key exists
57 if not 'figures' in resources:
60 if not 'outputs' in resources:
58 resources['figures'] = {}
61 resources['outputs'] = {}
59
62
60 #Loop through all of the outputs in the cell
63 #Loop through all of the outputs in the cell
61 for index, out in enumerate(cell.get('outputs', [])):
64 for index, out in enumerate(cell.get('outputs', [])):
@@ -77,7 +80,7 b' class ExtractFigureTransformer(Transformer):'
77 data = data.encode("UTF-8")
80 data = data.encode("UTF-8")
78
81
79 #Build a figure name
82 #Build a figure name
80 figure_name = self.figure_filename_template.format(
83 filename = self.figure_filename_template.format(
81 unique_key=unique_key,
84 unique_key=unique_key,
82 cell_index=cell_index,
85 cell_index=cell_index,
83 index=index,
86 index=index,
@@ -87,10 +90,12 b' class ExtractFigureTransformer(Transformer):'
87 # cell.outputs[i].svg_filename ... etc (svg in example)
90 # cell.outputs[i].svg_filename ... etc (svg in example)
88 # Where
91 # Where
89 # cell.outputs[i].svg contains the data
92 # cell.outputs[i].svg contains the data
90 out[out_type + '_filename'] = figure_name
93 if output_files_dir is not None:
94 filename = os.path.join(output_files_dir, filename)
95 out[out_type + '_filename'] = filename
91
96
92 #In the resources, make the figure available via
97 #In the resources, make the figure available via
93 # resources['figures']['filename'] = data
98 # resources['outputs']['filename'] = data
94 resources['figures'][figure_name] = data
99 resources['outputs'][filename] = data
95
100
96 return cell, resources
101 return cell, resources
@@ -34,10 +34,10 b' class DebugWriter(WriterBase):'
34 See base for more...
34 See base for more...
35 """
35 """
36
36
37 if 'figures' in resources:
37 if 'outputs' in resources:
38 print("Figures extracted from %s" % notebook_name)
38 print("outputs extracted from %s" % notebook_name)
39 print('-' * 80)
39 print('-' * 80)
40 pprint.pprint(resources['figures'], indent=2, width=70)
40 pprint.pprint(resources['outputs'], indent=2, width=70)
41 else:
41 else:
42 print("No figures extracted from %s" % notebook_name)
42 print("No outputs extracted from %s" % notebook_name)
43 print('=' * 80)
43 print('=' * 80)
@@ -31,7 +31,7 b' class FilesWriter(WriterBase):'
31 """Consumes nbconvert output and produces files."""
31 """Consumes nbconvert output and produces files."""
32
32
33
33
34 build_directory = Unicode("nbconvert_build", config=True,
34 build_directory = Unicode(".", config=True,
35 help="""Directory to write output to. Leave blank
35 help="""Directory to write output to. Leave blank
36 to output to the current directory""")
36 to output to the current directory""")
37
37
@@ -57,16 +57,19 b' class FilesWriter(WriterBase):'
57 See base for more...
57 See base for more...
58 """
58 """
59
59
60 #Pull the extension from the resources dict.
60 # Pull the extension and subdir from the resources dict.
61 output_extension = resources['output_extension']
61 output_extension = resources['output_extension']
62
62
63 #Write all of the extracted resources to the destination directory.
63 # Write all of the extracted resources to the destination directory.
64 #NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
64 # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
65 #TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
65 # TRANSFORMER SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
66 for filename, data in resources.get('figures', {}).items():
66 for filename, data in resources.get('outputs', {}).items():
67
67
68 #Determine where to write the file to
68 # Determine where to write the file to
69 dest = os.path.join(self.build_directory, filename)
69 dest = os.path.join(self.build_directory, filename)
70 path = os.path.dirname(dest)
71 if not os.path.isdir(path):
72 os.makedirs(path)
70
73
71 #Write file
74 # Write file
72 with io.open(dest, 'wb') as f:
75 with io.open(dest, 'wb') as f:
@@ -85,7 +88,8 b' class FilesWriter(WriterBase):'
85 if not os.path.isdir(path):
88 if not os.path.isdir(path):
86 os.makedirs(path)
89 os.makedirs(path)
87
90
88 #Copy
91 # Copy if destination is different.
92 if not os.path.normpath(dest) == os.path.normpath(matching_filename):
89 shutil.copyfile(matching_filename, dest)
93 shutil.copyfile(matching_filename, dest)
90
94
91 #Determine where to write conversion results.
95 # Determine where to write conversion results.
@@ -22,9 +22,7 b' from .base import WriterBase'
22
22
23 class StdoutWriter(WriterBase):
23 class StdoutWriter(WriterBase):
24 """Consumes output from nbconvert export...() methods and writes to the
24 """Consumes output from nbconvert export...() methods and writes to the
25 stdout stream. Allows for quick debuging of nbconvert output. Using the
25 stdout stream."""
26 debug flag makes the writer pretty-print the figures contained within the
27 notebook."""
28
26
29
27
30 def write(self, output, resources, **kw):
28 def write(self, output, resources, **kw):
General Comments 0
You need to be logged in to leave comments. Login now