Skip to content

Commit ef64ec5

Browse files
authored
Merge pull request #1254 from MridulS/example_api
Update binder API example to include file/urlpath
2 parents 81660eb + 96940cf commit ef64ec5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

examples/binder-api.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717

1818
def build_binder(repo,
19-
ref='master',
20-
filepath=None,
19+
ref,
2120
*,
2221
binder_url='https://mybinder.org'):
2322
"""Launch a binder
@@ -38,9 +37,12 @@ def build_binder(repo,
3837
parser = argparse.ArgumentParser(description=__doc__)
3938
parser.add_argument('repo', type=str, help="The GitHub repo to build")
4039
parser.add_argument(
41-
'--ref', default='master', help="The ref of the repo to build")
42-
parser.add_argument(
40+
'--ref', default='HEAD', help="The ref of the repo to build")
41+
file_or_url = parser.add_mutually_exclusive_group()
42+
file_or_url.add_argument(
4343
'--filepath', type=str, help="The file to open, if any.")
44+
file_or_url.add_argument(
45+
'--urlpath', type=str, help="The url to open, if any.")
4446
parser.add_argument(
4547
'--binder',
4648
default='https://mybinder.org',
@@ -53,15 +55,19 @@ def build_binder(repo,
5355
for evt in build_binder(
5456
opts.repo,
5557
ref=opts.ref,
56-
filepath=opts.filepath,
5758
binder_url=opts.binder):
5859
if 'message' in evt:
5960
print("[{phase}] {message}".format(
6061
phase=evt.get('phase', ''),
6162
message=evt['message'].rstrip(),
6263
))
6364
if evt.get('phase') == 'ready':
64-
url = "{url}?token={token}".format(**evt)
65+
if opts.filepath:
66+
url = "{url}notebooks/{filepath}?token={token}".format(**evt, filepath=opts.filepath)
67+
elif opts.urlpath:
68+
url = "{url}{urlpath}?token={token}".format(**evt, urlpath=opts.urlpath)
69+
else:
70+
url = "{url}?token={token}".format(**evt)
6571
print("Opening %s" % url)
6672
webbrowser.open(url)
6773
break

0 commit comments

Comments
 (0)