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

enable query parameter passing #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
55 changes: 39 additions & 16 deletions nbopen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import os.path
import sys
import threading
import warnings
import webbrowser

Expand All @@ -28,40 +29,62 @@ def find_best_server(filename, profile='default'):
return None


def nbopen(filename, profile='default'):
def nbopen(filename, profile='default', query=''):

def open_notebook(filename, profile):
filename = os.path.abspath(filename)
home_dir = os.path.expanduser('~')
server_inf = find_best_server(filename, profile)
if server_inf is not None:
print("Using existing server at", server_inf['notebook_dir'])
path = os.path.relpath(filename, start=server_inf['notebook_dir'])
url = url_path_join(server_inf['url'], 'notebooks', url_escape(path))
if query:
url = '{}?{}'.format(url, query)
na = notebookapp.NotebookApp.instance()
na.load_config_file()
browser = webbrowser.get(na.browser or None)
browser.open(url, new=2)
else:
raise ValueError('no server available')

filename = os.path.abspath(filename)
home_dir = os.path.expanduser('~')
server_inf = find_best_server(filename, profile)
if server_inf is not None:
print("Using existing server at", server_inf['notebook_dir'])
path = os.path.relpath(filename, start=server_inf['notebook_dir'])
url = url_path_join(server_inf['url'], 'notebooks', url_escape(path))
na = notebookapp.NotebookApp.instance()
na.load_config_file()
browser = webbrowser.get(na.browser or None)
browser.open(url, new=2)
else:

if server_inf is None:
# We could introduce a check, rather than a wait; though this adds
# code complication.
twait = 4
# Open the requested notebook in the server, once it has opened.
t = threading.Timer(twait, open_notebook, args=[filename, profile])
t.start()
print('z' * twait)

if filename.startswith(home_dir):
nbdir = home_dir
else:
nbdir = os.path.dirname(filename)

print("Starting new server")
notebookapp.launch_new_instance(file_to_run=os.path.abspath(filename),
notebook_dir=nbdir,
open_browser=True,
notebookapp.launch_new_instance(notebook_dir=nbdir,
open_browser=False,
argv=[], # Avoid it seeing our own argv
)
)
else:
open_notebook(filename, profile)


def main(argv=None):
ap = argparse.ArgumentParser()
ap.add_argument('-p', '--profile', default='default',
help=argparse.SUPPRESS)
ap.add_argument('filename', help='The notebook file to open')

ap.add_argument('-q', '--query', default='',
help='query string for the notebook url')
args = ap.parse_args(argv)

nbopen(args.filename, args.profile)
nbopen(args.filename, args.profile, args.query)

if __name__ == '__main__':
main()