Skip to content

Commit

Permalink
Merge pull request #33 from the-virtual-brain/VT-35
Browse files Browse the repository at this point in the history
VT-35: create user settings in jupyterlab
  • Loading branch information
romina1601 authored Jan 10, 2025
2 parents 1642c07 + 70e8694 commit 6bfb9af
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"files": [
"lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
"style/**/*.{css,js,eot,gif,html,jpg,json,png,svg,woff2,ttf}",
"style/index.js"
"style/index.js",
"schema/*.json"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -122,7 +123,8 @@
}
},
"extension": true,
"outputDir": "tvb_ext_unicore/labextension"
"outputDir": "tvb_ext_unicore/labextension",
"schemaDir": "schema"
},
"jupyter-releaser": {
"hooks": {
Expand Down
15 changes: 15 additions & 0 deletions schema/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"title": "Registry Setting",
"description": "Settings to define the registry you want to connect with the help of tvb-ext-unicore.",
"type": "object",
"properties": {
"registry": {
"type": "string",
"title": "Registry",
"description": "Provide a registry url.",
"default": "https://unicore.fz-juelich.de/HBP/rest/registries/default_registry"
}
},
"additionalProperties": false,
"type": "object"
}
22 changes: 20 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { NO_SITE, getJobCode } from './constants';
import logoUnicore from '../style/icons/logo-unicore.svg';
import { SideButton } from './components/SideButton';
import { LabIcon } from '@jupyterlab/ui-components';
import { ISettingRegistry } from '@jupyterlab/settingregistry';

async function cancelJob(resource_url: string): Promise<any> {
const dataToSend = { resource_url: resource_url };
Expand Down Expand Up @@ -59,7 +60,8 @@ const plugin: JupyterFrontEndPlugin<void> = {
IConsoleTracker,
ILabShell,
INotebookTracker,
IFileBrowserFactory
IFileBrowserFactory,
ISettingRegistry
],
activate: async (
app: JupyterFrontEnd,
Expand All @@ -68,12 +70,28 @@ const plugin: JupyterFrontEndPlugin<void> = {
consoleTracker: IConsoleTracker,
labShell: ILabShell,
notebookTracker: INotebookTracker,
factory: IFileBrowserFactory
factory: IFileBrowserFactory,
settingRegistry: ISettingRegistry
) => {
console.log('JupyterLab extension tvb-ext-unicore is activated!');
let widget: MainAreaWidget<PyunicoreWidget>;
const columns = ['id', 'name', 'owner', 'site', 'status', 'start_time'];
const command = 'tvb_ext_unicore:open';

// Load settings
console.log('SettingRegistry:', settingRegistry);
await settingRegistry
.load('tvb-ext-unicore:settings')
.then(settings => {
console.log('Settings loaded:', settings.composite);

const registry = settings.get('registry').composite as string;
console.log(`Registry: ${registry}`);
})
.catch(reason => {
console.error('Failed to load settings:', reason);
});

app.commands.addCommand(command, {
label: 'PyUnicore Task Stream',
execute: async (args = { defaultSite: NO_SITE }): Promise<any> => {
Expand Down
35 changes: 23 additions & 12 deletions tvb_ext_unicore/unicore_wrapper/unicore_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
#
# (c) 2022-2024, TVB Widgets Team
#

import json
import os
from pyunicore.client import _HBP_REGISTRY_URL
import requests
import pyunicore.client as unicore_client
from pyunicore.credentials import OIDCToken
from requests.exceptions import ConnectionError
from tvb_ext_unicore.exceptions import TVBExtUnicoreException, ClientAuthException, SitesDownException
from tvb_ext_unicore.exceptions import FileNotExistsException, JobRunningException
from tvb_ext_unicore.logger.builder import get_logger
from tvb_ext_unicore.unicore_wrapper.job_dto import JobDTO
from tvb_ext_unicore.utils import get_registry

LOGGER = get_logger(__name__)
DOWNLOAD_MESSAGE = 'Downloaded successfully!'
Expand All @@ -25,23 +26,33 @@ def __init__(self):
token = self.__retrieve_token_str()
token = OIDCToken(token)
self.transport = self.__build_transport(token)
self.registry = unicore_client.Registry(self.transport, _HBP_REGISTRY_URL)
self.registry = unicore_client.Registry(self.transport, get_registry())

def __retrieve_token_str(self):
try:
# for ebrains lab
from clb_nb_utils import oauth as clb_oauth
token = clb_oauth.get_token()
except (ModuleNotFoundError, ConnectionError) as e:
LOGGER.warning(f"Could not connect to EBRAINS to retrieve an auth token: {e}")
LOGGER.info("Will try to use the auth token defined by environment variable CLB_AUTH...")

token = os.environ.get('CLB_AUTH')
if token is None:
LOGGER.error("No auth token defined as environment variable CLB_AUTH! Please define one!")
raise TVBExtUnicoreException("Cannot connect to EBRAINS HPC without an auth token! Either run this on "
"Collab, or define the CLB_AUTH environment variable!")

LOGGER.info("Successfully retrieved the auth token from environment variable CLB_AUTH!")
try:
# for juelich's lab
api_url = os.getenv("JUPYTERHUB_API_URL")
user_api_url = f"{api_url}/user_oauth"
headers = {"Authorization": "token {}".format(os.getenv("JUPYTERHUB_API_TOKEN"))}
r = requests.get(user_api_url, headers=headers)
response = json.loads(r.content.decode("utf-8"))
token = response["auth_state"]["access_token"]
except Exception as e:
LOGGER.warning(f"Could not connect to Juelich's JupyterLab to retrieve an auth token: {e}")
LOGGER.info("Will try to use the auth token defined by environment variable CLB_AUTH...")
token = os.environ.get('CLB_AUTH')
if token is None:
LOGGER.error("No auth token defined as environment variable CLB_AUTH! Please define one!")
raise TVBExtUnicoreException("Cannot connect to EBRAINS HPC without an auth token! Either run "
"this on Collab, or define the CLB_AUTH environment variable!")

LOGGER.info("Successfully retrieved the auth token from environment variable CLB_AUTH!")
return token

def __build_transport(self, token):
Expand Down
23 changes: 23 additions & 0 deletions tvb_ext_unicore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

import json
import enum
import os

from jupyter_core.paths import jupyter_config_dir
from tvb_ext_unicore.logger.builder import get_logger
from pyunicore.client import _HBP_REGISTRY_URL

LOGGER = get_logger(__name__)

Expand All @@ -21,6 +25,25 @@ class DownloadStatus(str, enum.Enum): # inherit str for json serialization
SUCCESS = 'success'


def get_user_settings():
data_dir = jupyter_config_dir() # path to jupyter configs folder; usually it's $HOME/.jupyter
# path to user-settings for this extension
settings_path = os.path.join(data_dir, 'lab', 'user-settings', 'tvb-ext-unicore', 'settings.jupyterlab-settings')
if os.path.exists(settings_path):
with open(settings_path, 'r', encoding='utf-8') as f:
settings = json.load(f)
else:
settings = {}

return settings


def get_registry():
user_settings = get_user_settings()
registry = user_settings.get('registry', _HBP_REGISTRY_URL) # if registry is not set, use a default value
return registry


def build_response(status, message):
# type: (DownloadStatus, str) -> str
"""
Expand Down

0 comments on commit 6bfb9af

Please sign in to comment.