-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add nbexec test and enable nbconv to infer exporter type
- Loading branch information
Showing
4 changed files
with
51 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
#!/usr/bin/env python | ||
from pathlib import Path | ||
from typing import List, Union | ||
from typing import Tuple | ||
|
||
from nbconvert.exporters.base import get_exporter | ||
|
||
|
||
def nbconv(in_file: Union[str, Path], exporter: str = "python") -> List[str]: | ||
def nbconv(in_file: str, exporter: str = None) -> Tuple[str, str]: | ||
"""Convert a notebook into various formats using ``nbformat`` exporters. | ||
:param in_file: The name of the input notebook file. | ||
:param exporter: The exporter that determines the output file type. | ||
:return: A 2-tuple of the output file's 1) name and 2) contents. | ||
:note: The exporter type must be 'asciidoc', 'pdf', 'html', 'latex', | ||
'markdown', 'python', 'rst', 'script', or 'slides'. | ||
pdf requires latex, 'notebook' does nothing, | ||
slides need to served (not self-contained). | ||
All formats except 'HTML' require pandoc. | ||
Exporting to pdf requires latex. | ||
""" | ||
if isinstance(in_file, Path): | ||
contents, resources = get_exporter(exporter)().from_file(in_file.open()) | ||
elif isinstance(in_file, str): | ||
contents, resources = get_exporter(exporter)().from_filename(in_file) | ||
else: | ||
print("The in_file argument must be a string or pathlib Path object.") | ||
out_name = Path(in_file).stem + resources.get("output_extension", ".txt") | ||
in_file_path = Path(in_file) | ||
if not exporter: | ||
ext_exp_dict = { | ||
'.asciidoc': 'asciidoc', | ||
'.adoc': 'asciidoc', | ||
'.asc': 'asciidoc', | ||
'.pdf': 'pdf', | ||
'.html': 'html', | ||
'.tex': 'latex', | ||
'.md': 'markdown', | ||
'.py': 'python', | ||
'.R': 'script', | ||
'.rst': 'rst' | ||
} | ||
if in_file_path.suffix in ext_exp_dict: | ||
exporter = ext_exp_dict[in_file_path.suffix] | ||
else: | ||
return "Unable to infer exporter type!" | ||
contents, resources = get_exporter(exporter)().from_filename(in_file) | ||
out_name = in_file_path.stem + resources.get("output_extension", ".txt") | ||
return [out_name, contents] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters