question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Programmatically exit a notebook early when match some condition

See original GitHub issue

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I’m always frustrated when […]

Programmatically exit a notebook early .

Describe the solution you’d like

I have below code use to skip a notebook node:

class StopExecution(Exception):
    def _render_traceback_(self):
        pass

def exit_notebook(): raise StopExecution

if vstore.exists(Config.model_name):
    exit_notebook()

But would got error when run in pipeline : Failed validating 'type' in notebook['properties']['traceback']:

Describe alternatives you’ve considered

I don’t have a clear solution now. Since elyra doesn’t provide a way to avoid redundant execution, I tried to make it myself, but failed by avoid redundant execution. Do you have a way to walk around ?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ptitzlercommented, Aug 19, 2022

If your ultimate goal is to skip execution of some cells without causing papermill to return an error you might be able to accomplish that using

import sys
sys.exit(0)

If this code is somewhere in the first notebook and triggered then the second notebook will still run.

image

I’ve tested this using local execution and Kubeflow Pipelines.

You can combine this with the following code to assure that this only happens when you run the notebook in a pipeline and not when you run it interactively:

if os.environ.get("ELYRA_RUNTIME_ENV") is not None:
    sys.exit(0)

The proprietary ELYRA_RUNTIME_ENV environment variable is only defined when Elyra executes a notebook or script.

You do need to be aware of that stopping the interpreter might have unintended consequences.

1reaction
ptitzlercommented, Aug 15, 2022

I don’t know about elyra-examples-kfp-catalog , is it safe to remove ?

Yes, absolutely. This optional package is installed by default, when you specify pip install elyra[all]. Since you are using the local runtime it’s functionality does not apply.

Error message from elyra alert:

Traceback (most recent call last):
  File "C:\Users\ufo\anaconda3\lib\site-packages\elyra\pipeline\local\processor_local.py", line 234, in process
    papermill.execute_notebook(filepath, filepath, **additional_kwargs)
  File "C:\Users\ufo\anaconda3\lib\site-packages\papermill\execute.py", line 107, in execute_notebook
    nb = papermill_engines.execute_notebook_with_engine(
  File "C:\Users\ufo\anaconda3\lib\site-packages\papermill\engines.py", line 49, in execute_notebook_with_engine
    return self.get_engine(engine_name).execute_notebook(nb, kernel_name, **kwargs)
  ...
  File "C:\Users\ufo\anaconda3\lib\site-packages\nbformat\v4\nbbase.py", line 68, in new_output
    validate(output, output_type)
  File "C:\Users\ufo\anaconda3\lib\site-packages\nbformat\v4\nbbase.py", line 40, in validate
    return validate_orig(node, ref=ref, version=nbformat)
  File "C:\Users\ufo\anaconda3\lib\site-packages\nbformat\validator.py", line 302, in validate
    raise error
nbformat.validator.NotebookValidationError: None is not of type 'array'

Failed validating 'type' in notebook['properties']['traceback']:

The stacktrace clarifies the following:

  • the local runtime is used, which doesn’t support custom components (hence my earlier The only reference to the error message you've... question does not apply)
  • the problem is caused by the 1.1.0 clean_data.ipynb Jupyter notebook

Two follow-up questions:

  • When you open the notebook (after it was executed), does it contain any error messages? (Papermill inserts them)?
  • Does the notebook work as-is when you run it in JupyterLab (Run > Run all cells)?
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - IPython Notebook - early exit from cell - Stack Overflow
Save this question. Show activity on this post. I'd like to programmatically exit a cell early in IPython Notebook. exit(0) , however, kills...
Read more >
How to programmatically stop a program in Jupyter Notebook?
Hi all, I just want the program to stop/end when it comes across this line. First I tried quit() and that didn't work...
Read more >
How can I exit from a Notebook which is used as a job?
The simple way to terminate execution based on a condition is to throw an exception; doing so will cause the run to terminate...
Read more >
Can a Jupyter notebook programmatically halt itself? #1880
Yes, a Jupyter notebook can programmatically halt itself. Use a cell at the end of an iPython notebook like this: %%javascript Jupyter.notebook.
Read more >
Basics of Jupyter Notebook and Python | Packt Hub
The Jupyter Notebook is a more recent and more sophisticated alternative to the console ... You can exit the IPython console by typing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found