Skip to content

Commit

Permalink
Merge pull request #61 from the-virtual-brain/VT-40
Browse files Browse the repository at this point in the history
VT-40. Create user settings file to set base folders for open viewer&script functionalities
  • Loading branch information
915-Misan-Teodora authored Dec 17, 2024
2 parents 2ae795e + ef4bed3 commit 163996e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 16 deletions.
3 changes: 2 additions & 1 deletion 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,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
21 changes: 21 additions & 0 deletions schema/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"title": "Base Directory Setting",
"description": "Setting to define the base directory.",
"type": "object",
"properties": {
"baseDirectoryWeb": {
"type": "string",
"title": "Base Directory Web",
"description": "for web",
"default": "."
},
"baseDirectoryKernel": {
"type": "string",
"title": "Base Directory Kernel",
"description": "for kernel",
"default": "."
}
},
"additionalProperties": false,
"type": "object"
}
18 changes: 18 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import type { Signal } from "@lumino/signaling";
import { commandIDs } from "./commands/CommandIDs";
import { IEditorTracker } from '@jupyterlab/fileeditor';
import { IMainMenu } from '@jupyterlab/mainmenu';
import { ISettingRegistry } from '@jupyterlab/settingregistry';

const FACTORY = 'Xircuits editor';

Expand All @@ -59,6 +60,7 @@ const xircuits: JupyterFrontEndPlugin<void> = {
ILayoutRestorer,
IRenderMimeRegistry,
IDocumentManager,
ISettingRegistry,
IMainMenu,
ITranslator,
IEditorTracker,
Expand All @@ -71,6 +73,7 @@ const xircuits: JupyterFrontEndPlugin<void> = {
restorer: ILayoutRestorer,
rendermime: IRenderMimeRegistry,
docmanager: IDocumentManager,
settingRegistry: ISettingRegistry,
mainMenu?: IMainMenu,
translator?: ITranslator,
editorTracker?: IEditorTracker,
Expand Down Expand Up @@ -125,6 +128,21 @@ const xircuits: JupyterFrontEndPlugin<void> = {
icon: xircuitsIcon
});

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

const baseDirWeb = settings.get('baseDirectoryWeb').composite as string;
const baseDirKernel = settings.get('baseDirectoryKernel').composite as string;
console.log(`Base Directory Web: ${baseDirWeb} & Base Directory Kernel: ${baseDirKernel}`);
})
.catch(reason => {
console.error('Failed to load settings:', reason);
});

// Registering the widget factory
app.docRegistry.addWidgetFactory(widgetFactory);

Expand Down
18 changes: 14 additions & 4 deletions tvbextxircuits/handlers/user_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import json
from pathlib import Path
import tornado
from jupyter_server.base.handlers import APIHandler
import os

from tvbextxircuits.logger.builder import get_logger
from tvbextxircuits.nb_generator import IS_WINDOWS
from tvbextxircuits.utils import get_base_dir_web, get_base_dir_kernel

LOGGER = get_logger(__name__)


class HomeDirectoryHandler(APIHandler):
Expand All @@ -17,14 +23,18 @@ def post(self):
input_data = self.get_json_body()

try:
# TODO: temporary hack for debug purposes on juwels, also handle the other operating systems(not just
# windows)
path = input_data["node_path"]
LOGGER.info(f'Component script path is: {path}')

if IS_WINDOWS:
self.finish(json.dumps({"homeDirectory": path}))
else:
home_directory = os.path.expanduser(os.path.join('~', path))
self.finish(json.dumps({"homeDirectory": home_directory}))
base_dir_web = get_base_dir_web()
home_directory = os.path.join(base_dir_web, path)
base_dir_kernel = get_base_dir_kernel()
return_path = str(Path(home_directory).relative_to(Path(base_dir_kernel)))
LOGGER.info(f'Opening component script from path: {return_path}')
self.finish(json.dumps({"homeDirectory": return_path}))
except KeyError:
data = {"error_msg": "Could not determine path from POST params!"}
self.finish(json.dumps(data))
29 changes: 18 additions & 11 deletions tvbextxircuits/nb_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
import importlib
from tvb.simulator.integrators import HeunDeterministic
from tvb.simulator.models.oscillator import Generic2dOscillator

from tvbextxircuits.utils import get_base_dir_web, get_base_dir_kernel
from xai_components.base_tvb import ComponentWithWidget

from xai_components.logger.builder import get_logger
from pathlib import Path

LOGGER = get_logger(__name__)
IS_WINDOWS = sys.platform.startswith('win')
Expand Down Expand Up @@ -101,25 +104,29 @@ def get_notebook_for_component(component_name, component_id, component_path, com
def store(notebook, component_name, xircuits_id):
file_name = f'{component_name}_widget.ipynb'

notebook_dir = os.path.join(NOTEBOOKS_DIR, xircuits_id)
base_dir_web = get_base_dir_web()

notebook_dir = os.path.join(base_dir_web, NOTEBOOKS_DIR, xircuits_id)
if not os.path.exists(notebook_dir):
os.mkdir(notebook_dir)
os.makedirs(notebook_dir, exist_ok=True)

path = os.path.join(notebook_dir, file_name)

with open(path, 'w') as f:
nbformat.write(notebook, f)

LOGGER.info(f'Writing notebook complete at path: {path}')

base_dir_kernel = get_base_dir_kernel()

return_path = str(Path(path).relative_to(Path(base_dir_kernel)))

if IS_WINDOWS:
return NOTEBOOKS_DIR + '/' + xircuits_id + '/' + file_name

x = os.path.join(notebook_dir, file_name)
# TODO: temporary hack for debug purposes on juwels
# if juwels:
# return os.relativeto(os.abspath(x), 'juwels')
return os.path.expanduser(os.path.join('~', x))
# else:
# return x
windows_expanded_path = return_path.replace("\\", "/")
LOGGER.info(f'Storing notebook on Windows at path: {windows_expanded_path}')
return windows_expanded_path

return return_path


class NotebookGenerator(object):
Expand Down
36 changes: 36 additions & 0 deletions tvbextxircuits/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import json
from pathlib import Path

from jupyter_core.paths import jupyter_config_dir

from tvbextxircuits.logger.builder import get_logger

STORAGE_CONFIG_FILE = 'storage_config.json' # To be used only for HPC runs
COLLAB_NAME_KEY = 'collab_name' # Used only for HPC runs
BUCKET_NAME_KEY = 'bucket_name' # Used only for HPC runs
FOLDER_PATH_KEY = 'folder_path' # Used only for HPC runs
STORE_RESULTS_DIR = 'results' # Used by component that takes care of storing data and stage-out from HPC
DIR_TIME_STAMP_FRMT = '%Y.%m.%d_%H_%M_%S'

LOGGER = get_logger(__name__)

import os
import urllib.parse
import shutil
Expand Down Expand Up @@ -39,3 +46,32 @@ def copy_from_installed_wheel(package_name, resource="", dest_path=None, version
if config_path.exists():
shutil.rmtree(config_path)
shutil.copytree(resource_path, dest_path)


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-xircuits', '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_base_dir_web():
user_settings = get_user_settings()
base_dir = user_settings.get('baseDirectoryWeb', '.') # if baseDirectory is not set, use a default value
LOGGER.info(f'Base directory Web in user settings is: {base_dir}')
base_dir = os.path.abspath(os.path.expanduser(base_dir))
return base_dir


def get_base_dir_kernel():
user_settings = get_user_settings()
base_dir = user_settings.get('baseDirectoryKernel', '.') # if baseDirectory is not set, use a default value
LOGGER.info(f'Base directory Kernel in user settings is: {base_dir}')
base_dir = os.path.abspath(os.path.expanduser(base_dir))
return base_dir

0 comments on commit 163996e

Please sign in to comment.