Skip to content

Commit

Permalink
Jupyter URLs: add workaround for dynamic NERSC Shibboleth username
Browse files Browse the repository at this point in the history
Signed-off-by: Lance-Drane <[email protected]>
  • Loading branch information
Lance-Drane committed Oct 23, 2024
1 parent 23b6e95 commit 6a2de79
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
8 changes: 7 additions & 1 deletion ipsportal/run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Tuple
from urllib3.util import parse_url

from flask import Blueprint, render_template
from ipsportal.db import get_run, get_runid, get_data_information

Expand All @@ -21,4 +23,8 @@ def run(runid: int) -> Tuple[str, int]:
else:
run['parent_runid'] = None
data_info, jupyter_urls = get_data_information(run['portal_runid'])
return render_template("events.html", run=run, data_info=data_info, jupyter_urls=jupyter_urls), 200
if jupyter_urls:
resolved_jupyter_urls = [[jupyter_url, parse_url(jupyter_url).host] for jupyter_url in jupyter_urls]
else:
resolved_jupyter_urls = None
return render_template("events.html", run=run, data_info=data_info, jupyter_urls=resolved_jupyter_urls), 200
10 changes: 10 additions & 0 deletions ipsportal/static/events.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ details[open] summary {
ol {
padding-left: 4rem;
}

.jupyter-url-btn {
font-size: 100%;
font-family: inherit;
border: 0;
padding: 0;
text-decoration: underline;
background-color: inherit;
color: rgba(var(--bs-link-color-rgb),var(--bs-link-opacity,1));
}
22 changes: 22 additions & 0 deletions ipsportal/static/jupyter_url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// in a URL for jupyter.nersc.gov , users must provide NERSC Shibboleth (auth manager) credentials in the URL, but this will only work for a single user.
// We can dynamically create this URL on the frontend.

const NERSC_URL_HELPER_STORAGE_KEY = "username__jupyter.nersc.gov";

function onJupyterUrlBtnClick() {
const nersc_username = prompt(
"Enter the username you authenticate with on NERSC Shibboleth.",
window.localStorage.getItem(NERSC_URL_HELPER_STORAGE_KEY) || ""
);
if (nersc_username != null) {
window.localStorage.setItem(NERSC_URL_HELPER_STORAGE_KEY, nersc_username);
const result = jupyterUrlBtn
.getAttribute("data-original-url")
.replace(new RegExp("/user/[^/]+/"), `/user/${nersc_username}/`);
window.location.href = result;
}
}

for (jupyterUrlBtn of document.getElementsByClassName("jupyter-url-btn")) {
jupyterUrlBtn.addEventListener("click", onJupyterUrlBtnClick);
}
17 changes: 11 additions & 6 deletions ipsportal/templates/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

{% block head %}
<link rel="stylesheet" href="{{ url_for('static', filename='events.css') }}" />
<script src="{{ url_for('static', filename='jupyter_url.js') }}" defer></script>
<script src="{{ url_for('static', filename='event-table.js') }}" defer></script>
<script src="{{ url_for('static', filename='child-runs-table.js') }}" defer></script>
{% endblock %}
Expand Down Expand Up @@ -65,7 +66,7 @@ <h4>{% block title %}Run - {{ run.runid }}{% endblock %}</h4>
<tr>
<th>Monitor</th>
{% if run.vizurl %}
<td><a href=https://{{ run.vizurl }}>monitor<a></td>
<td><a href="https://{{ run.vizurl }}">monitor<a></td>
{% else %}
<td></td>
{% endif %}
Expand Down Expand Up @@ -104,11 +105,15 @@ <h4>{% block title %}Run - {{ run.runid }}{% endblock %}</h4>
<tr>
<th>Associated JupyterHub Notebooks</th>
<td><ul>
{% for url in jupyter_urls %}
{% set notebook_name = url.rpartition('/')[2] %}
<li>
<a href="{{url}}">{{notebook_name}}</a>
</li>
{% for url, host in jupyter_urls %}
<li>
{% set notebook_name = url.rpartition('/')[2] %}
{% if host == "jupyter.nersc.gov" %}
<button class="jupyter-url-btn" data-original-url="{{url}}">{{notebook_name}}</button>
{% else %}
<a href="{{url}}">{{notebook_name}}</a>
{% endif %}
</li>
{% endfor %}
</ul></td>
</tr>
Expand Down

0 comments on commit 6a2de79

Please sign in to comment.