Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added skip feature #142

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions testbook/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,23 @@ def execute_cell(self, cell, **kwargs) -> Union[Dict, List[Dict]]:

return executed_cells[0] if len(executed_cells) == 1 else executed_cells

def execute(self) -> None:
def execute(self, skip: Optional[Union[slice, list, int]] = None) -> None:
"""
Executes all cells
Executes all cells besides the ones who were asked to be skipped
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Executes all cells besides the ones who were asked to be skipped
Executes all cells.
If skip is passed, we will remove those cell(s) from the list of cells to be executed.

"""

for index, cell in enumerate(self.nb.cells):
execute_cells = enumerate(self.nb.cells)

if skip is not None:
if isinstance(skip, int):
skip = [skip]

Comment on lines +148 to +150
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would also want to accept cell tag(s) as input for the skip argument for users who wouldn't want to concern themselves with knowing which index each cell is at.

Suggested change
if isinstance(skip, int):
skip = [skip]
if isinstance(skip, int) or isinstance(skip, str):
skip = [skip]
if all(isinstance(x, str) for x in skip):
skip = [self._cell_index(tag) for tag in skip]

if not all(isinstance(x, int) for x in skip):
raise TestbookError('skip list should contain only integers')

# Leave out the unwanted cells
execute_cells = [(index, cell) for index, cell in execute_cells if index not in skip]

for index, cell in execute_cells:
super().execute_cell(cell, index)

def cell_output_text(self, cell) -> str:
Expand Down
5 changes: 3 additions & 2 deletions testbook/testbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class testbook:
attribute_name = None

def __init__(
self, nb, execute=None, timeout=60, kernel_name='python3', allow_errors=False, **kwargs
self, nb, execute=None, skip=None, timeout=60, kernel_name='python3', allow_errors=False, **kwargs
):
self.execute = execute
self.skip = skip
self.client = TestbookNotebookClient(
nbformat.read(nb, as_version=4) if not isinstance(nb, nbformat.NotebookNode) else nb,
timeout=timeout,
Expand All @@ -43,7 +44,7 @@ def __init__(

def _prepare(self):
if self.execute is True:
self.client.execute()
self.client.execute(skip=self.skip)
elif self.execute not in [None, False]:
self.client.execute_cell(self.execute)

Expand Down