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

Clean up link_generator notebook / app #50

Merged
merged 1 commit into from
Jul 23, 2018
Merged
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
56 changes: 39 additions & 17 deletions binder/link_generator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
"source": [
"# Generate `nbgitpuller` links for your JupyterHub\n",
"\n",
"Below is an interactive widget that lets you compose interact links\n",
"with `nbgitpuller`. In the form below, input your JupyterHub\n",
"URL as well as the repository\n",
"URL and information for the content you wish to share, and your interact\n",
"link will be printed below."
"When users click an `nbgitpuller` link pointing to your JupyterHub,\n",
"\n",
"1. They are asked to log in to the JupyterHub if they have not already\n",
"2. The git repository referred to in the nbgitpuller link is made up to date in their home directory (keeping local changes if there are merge conflicts)\n",
"3. They are shown the specific notebook / directory referred to in the nbgitpuller link.\n",
"\n",
"This is a great way to distribute materials to students."
]
},
{
Expand All @@ -20,21 +22,41 @@
"outputs": [],
"source": [
"from ipywidgets import interact\n",
"from urllib.parse import urlunparse, urlparse, urlencode, parse_qs\n",
"from IPython.display import Markdown\n",
"\n",
"@interact\n",
"def make_nbgitpuller_link(hub_url='', repo_url='', branch='', subPath='', app=''):\n",
"def make_nbgitpuller_link(hub_url='', repo_url='', branch='', filepath='', app=['notebook', 'lab']):\n",
" \"\"\"Generate an ipywidget form that creates an interact link.\"\"\"\n",
" hub_url = hub_url.strip()\n",
" kwargs = {}\n",
" for name, val in [('repo', repo_url), ('branch', branch), ('subPath', subPath), ('app', app)]:\n",
" if len(val) > 1:\n",
" kwargs[name] = val.strip()\n",
" params = f'&'.join(['{}={}'.format(nm, val) for nm, val in kwargs.items()])\n",
" url = f'{hub_url}/hub/user-redirect/git-pull?{params}'\n",
" if len(app) > 0 and app.strip() not in ['notebook', 'lab']:\n",
" print('app must be one of [\"notebook\", \"lab\"]')\n",
" elif (len(hub_url) > 0) and (len(repo_url) > 0):\n",
" print(url)"
" \n",
" # Don't do anything if we don't have a hub_url or repo_url\n",
" if not (len(hub_url) > 0 and len(repo_url) > 0):\n",
" return\n",
" \n",
" # Parse the query to its constituent parts\n",
" \n",
" scheme, netloc, path, params, query_str, fragment = urlparse(hub_url.strip())\n",
" \n",
" # nbgitpuller takes arguments via query parameters.\n",
" # However, your hub might already require query parameters to function (for example, to pick a profile to launch in)\n",
" # So we preserve the parameters we get & amend them to include the git-pull info we want\n",
" query = parse_qs(query_str, keep_blank_values=True)\n",
" \n",
" \n",
" # Make sure the path doesn't contain multiple slashes\n",
" if not path.endswith('/'):\n",
" path += '/'\n",
" path += 'hub/user-redirect/git-pull'\n",
" \n",
" # Construct query parameters from \n",
" for name, val in [('repo', repo_url), ('branch', branch), ('subPath', filepath), ('app', app)]:\n",
" if len(val) > 0:\n",
" query[name] = val.strip()\n",
" \n",
" url = urlunparse((scheme, netloc, path, params, urlencode(query, doseq=True), fragment))\n",
" \n",
" # FIXME: Have this return something instead of print so we can unit test\n",
" print(url)"
]
}
],
Expand Down