Skip to content

Commit

Permalink
added option jupyter_timeout for the first code chunk in a session (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoore committed Jun 9, 2020
1 parent f4b98af commit 14d6c52
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Change Log

## v0.5.0 (2020-05-??)
## v0.5.0 (dev)

* Added option `jupyter_timeout` for the first code chunk in a session (#30).
* Fixed Pandoc 2.8+ compatibility by using `-raw_attribute` in intermediate
Markdown. Code output in raw format (interpreted as Markdown) is no longer
lost when converting to document formats other than Markdown (#26).
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ session is in use).
kernel. Except when otherwise specified, Jupyter kernels should be usable
just like the built-in code execution system.

* `jupyter_timeout`={int} — Jupyter kernel timeout per code chunk in seconds.
The default is 60.


#### Execution

Expand Down
6 changes: 4 additions & 2 deletions codebraid/codeprocessors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def __init__(self, session_key):
self.lang_def = None
self.executable = None
self.jupyter_kernel = None
self.jupyter_timeout = None

self.code_options = None
self.code_chunks = []
Expand Down Expand Up @@ -170,6 +171,7 @@ def append(self, code_chunk):
jupyter_kernel = code_chunk.options['first_chunk_options'].get('jupyter_kernel')
if jupyter_kernel is not None:
self.jupyter_kernel = jupyter_kernel
self.jupyter_timeout = code_chunk.options['first_chunk_options'].get('jupyter_timeout')
else:
self.lang_def = self.code_processor.language_definitions[self.lang]
self.executable = code_chunk.options['first_chunk_options'].get('executable')
Expand Down Expand Up @@ -1081,7 +1083,7 @@ def _run_jupyter(self, session):

# https://jupyter-client.readthedocs.io/en/stable/api/client.html
# https://jupyter-client.readthedocs.io/en/stable/messaging.html#messages-on-the-iopub-pub-sub-channel
kernel_name = session.code_chunks[0].options['first_chunk_options']['jupyter_kernel']
kernel_name = session.jupyter_kernel
try:
import jupyter_client
except ImportError:
Expand Down Expand Up @@ -1130,7 +1132,7 @@ def shutdown_kernel():
external_file_mime_types = set(['image/png', 'image/jpeg', 'image/svg+xml', 'application/pdf'])
while True:
try:
msg = jupyter_client.iopub_channel.get_msg(timeout=60)
msg = jupyter_client.iopub_channel.get_msg(timeout=session.jupyter_timeout or 60)
except queue.Empty:
chunk_runtime_source_error_dict[cc.session_output_index].append('Jupyter kernel "{0}" timed out during execution"'.format(kernel_name))
errors = True
Expand Down
10 changes: 9 additions & 1 deletion codebraid/converters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ def finalize_after_copy(self):
for kw in ('first_number', 'line_numbers', 'rewrap_lines', 'rewrap_width', 'expand_tabs', 'tab_size')])
_first_chunk_execute_keywords = set(['executable', 'jupyter_kernel'])
_first_chunk_save_keywords = set(['save', 'save_as'])
_first_chunk_keywords = _first_chunk_execute_keywords | _first_chunk_save_keywords
_first_chunk_other_keywords = set(['jupyter_timeout'])
_first_chunk_keywords = _first_chunk_execute_keywords | _first_chunk_save_keywords | _first_chunk_other_keywords

keywords = _base_keywords | _layout_keywords | _first_chunk_keywords

Expand Down Expand Up @@ -420,8 +421,15 @@ def _option_first_chunk_string_error(self, key, value):
else:
first_chunk_options[key] = value

def _option_first_chunk_int_warning(self, key, value):
if not isinstance(value, int):
self.code_chunk.source_warnings.append('Invalid "{0}" value "{1}"'.format(key, value))
else:
self['first_chunk_options'][key] = value

_option_executable = _option_first_chunk_string_error
_option_jupyter_kernel = _option_first_chunk_string_error
_option_jupyter_timeout = _option_first_chunk_int_warning
_option_save = _option_first_chunk_bool_error
_option_save_as = _option_first_chunk_string_error

Expand Down
1 change: 1 addition & 0 deletions codebraid/converters/pandoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def keyval_namespace(code_chunk, options, key, value):
**expand_tabs,
**first_number,
**include,
'jupyter_timeout': keyval_int,
**line_anchors,
**line_numbers,
'name': keyval_generic,
Expand Down
2 changes: 1 addition & 1 deletion codebraid/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-

from .fmtversion import get_version_plus_info
__version__, __version_info__ = get_version_plus_info(0, 5, 0, 'dev', 3)
__version__, __version_info__ = get_version_plus_info(0, 5, 0, 'dev', 4)

0 comments on commit 14d6c52

Please sign in to comment.