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

Launch jlab with asynchronous=True #4

Merged
merged 6 commits into from
Jul 20, 2020
Merged
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
30 changes: 29 additions & 1 deletion jupyter_forward/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import random
from collections import namedtuple

import invoke
import typer
from fabric import Connection

random.seed(42)


app = typer.Typer(help='Jupyter Lab Port Forwarding Utility')


Expand Down Expand Up @@ -83,8 +85,34 @@ def start(
"""
password = getpass.getpass()
session = Connection(host, connect_kwargs={'password': password})

# jupyter lab will pipe output to logfile, which should not exist prior to running
# Logfile will be in $TMPDIR if defined on the remote machine, otherwise in $HOME
tmpdir = session.run('echo $TMPDIR', hide=True).stdout.strip()
if len(tmpdir) == 0:
tmpdir = session.run('echo $HOME', hide=True).stdout.strip()
if len(tmpdir) == 0:
tmpdir = '~'
logfile = f'{tmpdir}/.jforward.{port}'
_ = session.run(f'rm -f {logfile}')

# start jupyter lab on remote machine
command = f'conda activate {conda_env} && jupyter lab --no-browser --ip=`hostname` --port={port} --notebook-dir={notebook_dir}'
_ = session.run(command)
jlab_exe = session.run(f'{command} > {logfile} 2>&1', asynchronous=True)
print(f'DEBUG: jlab_exe is of type {type(jlab_exe)}')

# wait for logfile to contain access info, then write it to screen
condition = True
pattern = 'To access the notebook, open this file in a browser:'
while condition:
try:
result = session.run(f'tail {logfile}', hide='out')
if pattern in result.stdout:
condition = False
print(result.stdout)
except invoke.exceptions.UnexpectedExit:
print(f'Trying to access {logfile} on {host} again...')
pass


@app.command()
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ select = B,C,E,F,W,T4,B9

[isort]
known_first_party=jupyter_forward
known_third_party=fabric,pkg_resources,pytest,setuptools,typer
known_third_party=fabric,invoke,pkg_resources,pytest,setuptools,typer
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
Expand Down