Skip to content

Commit a5e55bb

Browse files
committed
Allow batchspawner to be used with different notebook app
Instead of creating a batchspawner notebook app, batchspawner-singleuser is now a wrapper that finds a port and add the port number to the command-line argument of the singleuser app. This allows the usage of batchspawner with jupyterlab for example.
1 parent 383e8a3 commit a5e55bb

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

batchspawner/batchspawner.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ class BatchSpawnerBase(Spawner):
7474
state_gethost
7575
"""
7676

77-
# override default since will need to set the listening port using the api
78-
cmd = Command(['batchspawner-singleuser'], allow_none=True).tag(config=True)
79-
8077
# override default since batch systems typically need longer
8178
start_timeout = Integer(300).tag(config=True)
8279

@@ -190,7 +187,7 @@ def parse_job_id(self, output):
190187
return output
191188

192189
def cmd_formatted_for_batch(self):
193-
return ' '.join(self.cmd + self.get_args())
190+
return ' '.join(['batchspawner-singleuser'] + self.cmd + self.get_args())
194191

195192
@gen.coroutine
196193
def run_command(self, cmd, input=None, env=None):

batchspawner/singleuser.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
from jupyterhub.singleuser import SingleUserNotebookApp
2-
from jupyterhub.utils import random_port, url_path_join
3-
from traitlets import default
1+
import sys
42

5-
class BatchSingleUserNotebookApp(SingleUserNotebookApp):
6-
@default('port')
7-
def _port(self):
8-
return random_port()
3+
from runpy import run_path
4+
from shutil import which
95

10-
def start(self):
11-
# Send Notebook app's port number to remote Spawner
12-
self.hub_auth._api_request(method='POST',
13-
url=url_path_join(self.hub_api_url, 'batchspawner'),
14-
json={'port' : self.port})
15-
super().start()
6+
from jupyterhub.utils import random_port, url_path_join
7+
from jupyterhub.services.auth import HubAuth
168

179
def main(argv=None):
18-
return BatchSingleUserNotebookApp.launch_instance(argv)
10+
port = random_port()
11+
hub_auth = HubAuth()
12+
hub_auth._api_request(method='POST',
13+
url=url_path_join(hub_auth.api_url, 'batchspawner'),
14+
json={'port' : port})
15+
16+
cmd_path = which(sys.argv[1])
17+
sys.argv = sys.argv[1:] + ['--port={}'.format(port)]
18+
run_path(cmd_path, run_name="__main__")
1919

2020
if __name__ == "__main__":
2121
main()

0 commit comments

Comments
 (0)