Skip to content

Commit

Permalink
Add home app warning message auto-detection
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Dec 4, 2024
1 parent e05a322 commit cd366eb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
54 changes: 40 additions & 14 deletions home/start_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
from glob import glob
from os import path
from pathlib import Path

import ipywidgets as ipw
import traitlets
Expand All @@ -29,7 +30,7 @@ def create_app_widget_move_buttons(name):


class AiidaLabHome:
"""Class that mananges the appearance of the AiiDAlab home page."""
"""Class that manages the appearance of the AiiDAlab home page."""

def __init__(self):
self.config_fn = ".launcher.json"
Expand All @@ -40,16 +41,16 @@ def _create_app_widget(self, name):
"""Create the widget representing the app on the home screen."""
config = self.read_config()
app = AiidaLabApp(name, None, AIIDALAB_APPS)

if name == "home":
app_widget = AppWidget(app, allow_move=False, allow_manage=False)
else:
app_widget = CollapsableAppWidget(app, allow_move=True)
app_widget.hidden = name in config["hidden"]
app_widget.observe(self._on_app_widget_change_hidden, names=["hidden"])

app_widget = CollapsableAppWidget(app, allow_move=True)
app_widget.hidden = name in config["hidden"]
app_widget.observe(self._on_app_widget_change_hidden, names=["hidden"])
return app_widget

def _create_home_widget(self):
"""Create the home app widget."""
app = AiidaLabApp("home", None, AIIDALAB_APPS)
return AppWidget(app, allow_move=False, allow_manage=False)

def _on_app_widget_change_hidden(self, change):
"""Record whether a app widget is hidden on the home screen in the config file."""
config = self.read_config()
Expand All @@ -72,7 +73,17 @@ def read_config(self):
def render(self):
"""Rendering all apps."""

displayed_apps = []
home = self._create_home_widget()
displayed_apps = [home]

config_dir = Path.home() / ".aiidalab"
warning_md = config_dir / "home_app_warning.md"

if warning_md.exists():
content = warning_md.read_text()
warning = self._get_home_warning_widget(content)
displayed_apps.append(warning)

apps = self.load_apps()

for name in apps:
Expand All @@ -81,7 +92,9 @@ def render(self):
self._app_widgets[name] = self._create_app_widget(name)

displayed_apps.append(self._app_widgets[name])

self.output.children = displayed_apps

return self.output

def load_apps(self):
Expand All @@ -98,7 +111,7 @@ def load_apps(self):
apps.sort(key=lambda x: order.index(x) if x in order else -1)
config["order"] = apps
self.write_config(config)
return ["home", *apps]
return apps

def move_updown(self, name, delta):
"""Move the app up/down on the start page."""
Expand All @@ -111,6 +124,19 @@ def move_updown(self, name, delta):
config["order"] = order
self.write_config(config)

def _get_home_warning_widget(self, content):
from IPython.display import Markdown, display
from jinja2 import Environment

env = Environment()
warning = env.from_string(content).render()
output = ipw.Output()
warning_box = ipw.VBox(children=[output])
warning_box.add_class("home-warning")
with output:
display(Markdown(warning))
return warning_box


class AppWidget(ipw.VBox):
"""Widget that represents an app as part of the home page."""
Expand All @@ -119,7 +145,7 @@ def __init__(self, app, allow_move=False, allow_manage=True):
self.app = app

launcher = load_widget(app.name)
launcher.layout = ipw.Layout(width="900px")
launcher.layout.flex = "1" # fill available space

header_items = []
footer_items = []
Expand All @@ -128,7 +154,7 @@ def __init__(self, app, allow_move=False, allow_manage=True):
app_status_info = AppStatusInfoWidget()
for trait in ("detached", "compatible", "remote_update_status"):
ipw.dlink((app, trait), (app_status_info, trait))
app_status_info.layout.margin = "0px 0px 0px 800px"
app_status_info.layout.margin = "0px 0px 0px auto"
header_items.append(app_status_info)

footer_items.append(
Expand All @@ -150,7 +176,7 @@ def __init__(self, app, allow_move=False, allow_manage=True):

footer = ipw.HTML(" ".join(footer_items), layout={"width": "initial"})
footer.layout.margin = (
"0px 0px 0px 700px" if allow_manage else "0px 0px 20px 0px"
"0px 0px 0px auto" if allow_manage else "0px 0px 20px 0px"
)

super().__init__(children=[header, body, footer])
Expand Down
8 changes: 7 additions & 1 deletion start.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
" .output_subarea {\n",
" max-width: none !important;\n",
" }\n",
" .home-warning {\n",
" background-color: antiquewhite;\n",
" margin: 2px;\n",
" padding: 8px;\n",
" border: 1px solid red;\n",
" }\n",
"</style>\n"
]
},
Expand Down Expand Up @@ -64,7 +70,7 @@
" home.move_updown(parsed_url[\"move_up\"][0], -1)\n",
"elif \"move_down\" in parsed_url:\n",
" home.move_updown(parsed_url[\"move_down\"][0], +1)\n",
"display(home.render())"
"home.render()"
]
},
{
Expand Down

0 comments on commit cd366eb

Please sign in to comment.