Skip to content

Commit

Permalink
fixes ploomber#980
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Oct 7, 2022
1 parent 57b83ec commit 8f0e989
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
14 changes: 9 additions & 5 deletions src/ploomber/tasks/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ class NotebookConverter:
def __init__(self,
path_to_output,
exporter_name=None,
nbconvert_export_kwargs=None):
nbconvert_export_kwargs=None,
warn_on_ipynb=True):
self._warn_on_ipynb = warn_on_ipynb
self._suffix = Path(path_to_output).suffix

if exporter_name is None:
Expand Down Expand Up @@ -185,8 +187,8 @@ def __init__(self,
self._nbconvert_export_kwargs = nbconvert_export_kwargs or {}

def convert(self):

if self._exporter is None and self._nbconvert_export_kwargs:
if (self._warn_on_ipynb and self._exporter is None
and self._nbconvert_export_kwargs):
warnings.warn(
f'Output {self._path_to_output!r} is a '
'notebook file. nbconvert_export_kwargs '
Expand Down Expand Up @@ -738,8 +740,10 @@ def __init__(self,
exporter = nbconvert_exporter_name.get(
key) if nbconvert_exporter_name else None
self._converter.append(
NotebookConverter(product_nb, exporter,
nbconvert_export_kwargs))
NotebookConverter(product_nb,
exporter,
nbconvert_export_kwargs,
warn_on_ipynb=False))

@property
def debug_mode(self):
Expand Down
34 changes: 16 additions & 18 deletions tests/tasks/test_notebook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from pathlib import Path, PurePosixPath
from unittest.mock import ANY, Mock
import warnings

import jupytext
import nbconvert
Expand Down Expand Up @@ -488,36 +489,33 @@ def test_can_execute_when_product_is_metaproduct(tmp_directory):
dag.build()


# should not have warning when have multiple exports and
# nbconvert_export_kwargs set
@pytest.mark.parametrize('product, nb_product_key, nbconvert_exporter_name', [
({
'nb': File(Path('out.pdf')),
'file': File(Path('another', 'data', 'file.txt')),
}, 'nb', 'webpdf')
])
def test_multiple_nb_no_kwargs_warning(product, nb_product_key,
nbconvert_exporter_name):
def test_do_not_warn_on_nbconvert_export_kwargs_if_multiple_outputs(
tmp_directory):
dag = DAG()

code = """
# + tags=["parameters"]
var = None
upstream = None
# +
from pathlib import Path
Path(product['file']).touch()
1 + 1
"""

NotebookRunner(code,
product=product,
product={
'nb': File('out.ipynb'),
'report': File('out.html'),
},
dag=dag,
ext_in='py',
nbconvert_exporter_name=nbconvert_exporter_name,
nb_product_key=nb_product_key,
nb_product_key=['nb', 'report'],
nbconvert_export_kwargs=dict(exclude_input=True),
name='nb')
dag.build()

with warnings.catch_warnings():
# fail the test if this displays a warnings
warnings.simplefilter("error")
dag.build()


@pytest.mark.parametrize('product, nb_product_key, nbconvert_exporter_name', [
Expand Down Expand Up @@ -555,7 +553,7 @@ def test_multiple_nb_no_kwargs_warning(product, nb_product_key,
'file': File(Path('another', 'data', 'file.txt')),
}, 'nb', 'webpdf')
])
def test_multiple_nb_product_success(product, nb_product_key,
def test_multiple_nb_product_success(tmp_directory, product, nb_product_key,
nbconvert_exporter_name):
dag = DAG()

Expand Down

0 comments on commit 8f0e989

Please sign in to comment.