Skip to content

Commit 08d6c30

Browse files
author
Doug Blank
committed
Add jupyter-run command to jupyter_client collection
1 parent 40ed846 commit 08d6c30

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

jupyter_client/runapp.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
# Copyright (c) Jupyter Development Team.
3+
# Distributed under the terms of the Modified BSD License.
4+
5+
from __future__ import print_function
6+
7+
import logging
8+
import signal
9+
10+
from traitlets.config.application import Application
11+
from traitlets.config import catch_config_error
12+
from traitlets import (
13+
Instance, Dict, Unicode, Bool, List, CUnicode, Any
14+
)
15+
from jupyter_core.application import (
16+
JupyterApp, base_flags, base_aliases
17+
)
18+
19+
from . import __version__
20+
from .consoleapp import JupyterConsoleApp, app_aliases, app_flags
21+
22+
from jupyter_console.ptshell import ZMQTerminalInteractiveShell
23+
24+
# copy flags from mixin:
25+
flags = dict(base_flags)
26+
# start with mixin frontend flags:
27+
frontend_flags = dict(app_flags)
28+
# update full dict with frontend flags:
29+
flags.update(frontend_flags)
30+
31+
# copy flags from mixin
32+
aliases = dict(base_aliases)
33+
# start with mixin frontend flags
34+
frontend_aliases = dict(app_aliases)
35+
# load updated frontend flags into full dict
36+
aliases.update(frontend_aliases)
37+
38+
# get flags&aliases into sets, and remove a couple that
39+
# shouldn't be scrubbed from backend flags:
40+
frontend_aliases = set(frontend_aliases.keys())
41+
frontend_flags = set(frontend_flags.keys())
42+
43+
class RunApp(JupyterApp, JupyterConsoleApp):
44+
version = __version__
45+
name = "jupyter run"
46+
description = """Run Jupyter kernel code."""
47+
flags = Dict(flags)
48+
aliases = Dict(aliases)
49+
frontend_aliases = Any(frontend_aliases)
50+
frontend_flags = Any(frontend_flags)
51+
52+
def parse_command_line(self, argv=None):
53+
super(RunApp, self).parse_command_line(argv)
54+
self.build_kernel_argv(self.extra_args)
55+
self.filenames_to_run = self.extra_args[:]
56+
57+
@catch_config_error
58+
def initialize(self, argv=None):
59+
super(RunApp, self).initialize(argv)
60+
JupyterConsoleApp.initialize(self)
61+
signal.signal(signal.SIGINT, self.handle_sigint)
62+
self.shell = ZMQTerminalInteractiveShell.instance(parent=self,
63+
manager=self.kernel_manager,
64+
client=self.kernel_client,
65+
)
66+
self.shell.own_kernel = not self.existing
67+
68+
def handle_sigint(self, *args):
69+
if self.shell._executing:
70+
if self.kernel_manager:
71+
self.kernel_manager.interrupt_kernel()
72+
else:
73+
print("", file=sys.stderr)
74+
error("Cannot interrupt kernels we didn't start.\n")
75+
else:
76+
# raise the KeyboardInterrupt if we aren't waiting for execution,
77+
# so that the interact loop advances, and prompt is redrawn, etc.
78+
raise KeyboardInterrupt
79+
80+
def start(self):
81+
super(RunApp, self).start()
82+
for filename in self.filenames_to_run:
83+
cell = open(filename).read()
84+
self.shell.run_cell(cell, False)
85+
86+
main = launch_new_instance = RunApp.launch_instance
87+
88+
if __name__ == '__main__':
89+
main()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
setup_args['entry_points'] = {
9090
'console_scripts': [
9191
'jupyter-kernelspec = jupyter_client.kernelspecapp:KernelSpecApp.launch_instance',
92+
'jupyter-run = jupyter_client.runapp:RunApp.launch_instance',
9293
]
9394
}
9495
setup_args.pop('scripts', None)

0 commit comments

Comments
 (0)