8000 Show the optional AiiDAlab assistant on Home by cpignedoli · Pull Request #282 · aiidalab/aiidalab-home · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions assistant.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"schema_version": 1,
"title": "AiiDAlab Home",
"notebook": "start.ipynb",
"capabilities": ["find installed applications", "find calculations", "inspect AiiDA process reports", "explain failed or paused calculations"],
"assistant": {"version": 1}
}
12 changes: 11 additions & 1 deletion start.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from importlib import import_module

import ipywidgets as ipw
from nbclassic import nbclassic_path

Expand Down Expand Up @@ -84,4 +86,12 @@ def get_start_widget(appbase, jupbase):
"""

html = template.format(appbase=appbase, classicbase=classicbase)
return ipw.HTML(html)
links = ipw.HTML(html)
try:
assistant = import_module("aiidalab_ai_assistant")
except ImportError:
return links
make_panel = getattr(assistant, "AssistantPanel", None)
if make_panel is None:
return links
return ipw.VBox([make_panel(apps_url="../"), links])
42 changes: 35 additions & 7 deletions tests/test_home_navigation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""The Home shortcuts must use the classic UI and preserve Hub prefixes."""

import importlib.util
import sys
from html.parser import HTMLParser
from pathlib import Path
from types import SimpleNamespace
from urllib.parse import urljoin

import ipywidgets as ipw
import nbclassic
import pytest

Expand All @@ -21,20 +24,25 @@ def handle_starttag(self, tag, attrs):
self.urls[attrs["title"]] = attrs["href"]


@pytest.mark.parametrize("prefix", ["", "/user/test-user"])
@pytest.mark.parametrize("classic_path", ["", "/nbclassic"])
def test_classic_navigation_preserves_server_base_url(
monkeypatch, prefix, classic_path
):
monkeypatch.setattr(nbclassic, "nbclassic_path", lambda: classic_path)
def load_start_widget():
spec = importlib.util.spec_from_file_location(
"home_shortcuts", Path(__file__).parents[1] / "start.py"
)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
widget = module.get_start_widget("../home", "../../..")
return module.get_start_widget("../home", "../../..")


@pytest.mark.parametrize("prefix", ["", "/user/test-user"])
@pytest.mark.parametrize("classic_path", ["", "/nbclassic"])
def test_classic_navigation_preserves_server_base_url(
monkeypatch, prefix, classic_path
):
monkeypatch.setattr(nbclassic, "nbclassic_path", lambda: classic_path)
monkeypatch.setitem(sys.modules, "aiidalab_ai_assistant", None)
widget = load_start_widget()
try:
links = Links(widget.value).urls
page_url = f"http://localhost{prefix}/apps/apps/home/start.ipynb"
Expand All @@ -49,3 +57,23 @@ def test_classic_navigation_preserves_server_base_url(
)
finally:
widget.close()


def test_home_adds_the_panel_when_the_assistant_is_installed(monkeypatch):
assistant = ipw.Label("Test assistant panel")

def panel(*, apps_url):
assert apps_url == "../"
return assistant

monkeypatch.setitem(
sys.modules, "aiidalab_ai_assistant", SimpleNamespace(AssistantPanel=panel)
)
widget = load_start_widget()
try:
assert widget.children[0] is assistant
assert "Open Terminal" in Links(widget.children[1].value).urls
finally:
for child in widget.children:
child.close()
widget.close()
0