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

Add CLI flag to exit on exception #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion pweave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
__version__ = '0.30.2'

def weave(file, doctype=None, informat=None, kernel="python3", plot=True,
docmode=False, cache=False,
docmode=False, cache=False, exceptionexit=False,
figdir='figures', cachedir='cache',
figformat=None, listformats=False,
output=None, mimetype=None,):
Expand All @@ -28,6 +28,7 @@ def weave(file, doctype=None, informat=None, kernel="python3", plot=True,
:param plot: ``bool`` use matplotlib
:param docmode: ``bool`` use documentation mode, chunk code and results will be loaded from cache and inline code will be hidden
:param cache: ``bool`` Cache results to disk for documentation mode
:param exitonexception: ``bool`` Exit when an exception is raised in a code chunk
:param figdir: ``string`` directory path for figures
:param cachedir: ``string`` directory path for cached results used in documentation mode
:param figformat: ``string`` format for saved figures (e.g. '.png'), if None then the default for each format is used
Expand Down Expand Up @@ -55,6 +56,7 @@ def weave(file, doctype=None, informat=None, kernel="python3", plot=True,
rcParams["usematplotlib"] = plot
rcParams["cachedir"] = cachedir
rcParams["storeresults"] = cache
rcParams["exceptionexit"] = exceptionexit

doc.weave()

Expand Down
15 changes: 15 additions & 0 deletions pweave/processors/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from jupyter_client import KernelManager
from nbformat.v4 import output_from_msg
import os
import sys

from .. import config
from .base import PwebProcessorBase
Expand Down Expand Up @@ -54,6 +55,14 @@ def close(self):

def run_cell(self, src):
cell = {}

# Clear the last unhandled exception, if it is set
try:
if sys.last_value:
sys.last_value == None
except AttributeError:
pass

cell["source"] = src.lstrip()
msg_id = self.kc.execute(src.lstrip(), store_history=False)

Expand Down Expand Up @@ -128,6 +137,12 @@ def run_cell(self, src):
else:
outs.append(out)

if config.rcParams["exceptionexit"]:
try:
raise sys.last_value
except AttributeError:
pass

return outs

def loadstring(self, code_str, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions pweave/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def weave():
parser.add_option("-c", "--cache-results", dest="cache",
action="store_true", default=False,
help="Cache results to disk for documentation mode")
parser.add_option("-e", "--exit-on-exception", action="store_true", dest="exceptionexit", default=False,
help="Exit when an exception is encountered in a code chunk. This is only supported by python3 in a Jupyter kernel")
parser.add_option("-F", "--figure-directory", dest="figdir", default='figures',
help="Directory path for matplolib graphics: Default 'figures'")
parser.add_option("--cache-directory", dest="cachedir", default='cache',
Expand Down