-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add info box widgets with CSS stylesheets #623
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
80b5682
Add `static` folder for static files, e.g. stylesheets
edan-bainglass 4140194
Add css loader utility
edan-bainglass 52a6938
Load CSS stylesheets on `from aiidalab_widgets_base...` import
edan-bainglass a939355
Refactor default profile loading
edan-bainglass e7f5bdf
Simplify loader code
edan-bainglass f859695
Add notebook test for CSS loader
edan-bainglass e224fbd
Allow `str` paths for loading css stylesheets
edan-bainglass e106e01
Modify test notebook
edan-bainglass d16f596
Implement `InfoBox`, `InAppGuide`, and `FirstTimeUserMessage` widgets
edan-bainglass 93d2986
Update `InfoBox` API
edan-bainglass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
from __future__ import annotations | ||
|
||
import ipywidgets as ipw | ||
|
||
|
||
class InfoBox(ipw.VBox): | ||
"""The `InfoBox` component is used to provide additional info regarding a widget or an app.""" | ||
|
||
def __init__(self, classes: list[str] | None = None, **kwargs): | ||
"""`InfoBox` constructor. | ||
|
||
Parameters | ||
---------- | ||
`classes` : `list[str]`, optional | ||
One or more CSS classes. | ||
""" | ||
super().__init__(**kwargs) | ||
self.add_class("info-box") | ||
for custom_classes in classes or []: | ||
for custom_class in custom_classes.split(" "): | ||
if custom_class: | ||
self.add_class(custom_class) | ||
|
||
|
||
class InAppGuide(InfoBox): | ||
"""The `InfoAppGuide` is used to set up in-app guides that may be toggle in unison.""" | ||
|
||
def __init__(self, guide_id: str, classes: list[str] | None = None, **kwargs): | ||
"""`InAppGuide` constructor. | ||
|
||
Parameters | ||
---------- | ||
`guide_id` : `str` | ||
The unique identifier for the guide. | ||
`classes` : `list[str]`, optional | ||
One or more CSS classes. | ||
""" | ||
classes = ["in-app-guide", *(classes or []), guide_id] | ||
super().__init__(classes=classes, **kwargs) | ||
|
||
|
||
class FirstTimeUserMessage(ipw.VBox): | ||
"""The `FirstTimeUserMessage` is used to display a message to first time users.""" | ||
|
||
def __init__(self, message="", **kwargs): | ||
"""`FirstTimeUserMessage` constructor.""" | ||
|
||
self.close_button = ipw.Button( | ||
icon="times", | ||
tooltip="Close", | ||
) | ||
|
||
self.message_box = InfoBox( | ||
children=[ | ||
ipw.HTML(message), | ||
self.close_button, | ||
], | ||
) | ||
|
||
self.undo_button = ipw.Button( | ||
icon="undo", | ||
tooltip="Undo", | ||
description="undo", | ||
) | ||
|
||
self.closing_message = ipw.HBox( | ||
children=[ | ||
ipw.HTML("This message will not show next time"), | ||
self.undo_button, | ||
], | ||
) | ||
self.closing_message.add_class("closing-message") | ||
|
||
super().__init__( | ||
children=[ | ||
self.closing_message, | ||
self.message_box, | ||
], | ||
**kwargs, | ||
) | ||
|
||
self.add_class("first-time-users-infobox") | ||
|
||
self._check_if_first_time_user() | ||
|
||
self._set_event_listeners() | ||
|
||
def _check_if_first_time_user(self): | ||
"""Add a message for first-time users.""" | ||
try: | ||
with open(".app-user-config") as file: | ||
first_time_user = file.read().find("existing-user") == -1 | ||
except FileNotFoundError: | ||
first_time_user = True | ||
|
||
if first_time_user: | ||
self.layout.display = "flex" | ||
self.message_box.layout.display = "flex" | ||
self.closing_message.layout.display = "none" | ||
|
||
def _on_close(self, _=None): | ||
"""Hide the first time info box and write existing user token to file.""" | ||
self.message_box.layout.display = "none" | ||
self.closing_message.layout.display = "flex" | ||
self._write_existing_user_token_to_file() | ||
|
||
def _write_existing_user_token_to_file(self): | ||
"""Write a token to file marking the user as an existing user.""" | ||
with open(".app-user-config", "w") as file: | ||
file.write("existing-user") | ||
|
||
def _on_undo(self, _=None): | ||
"""Undo the action of closing the first time user message.""" | ||
from contextlib import suppress | ||
from pathlib import Path | ||
|
||
self.message_box.layout.display = "flex" | ||
self.closing_message.layout.display = "none" | ||
|
||
with suppress(FileNotFoundError): | ||
Path(".app-user-config").unlink() | ||
|
||
def _set_event_listeners(self): | ||
"""Set the event listeners.""" | ||
self.close_button.on_click(self._on_close) | ||
self.undo_button.on_click(self._on_undo) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Stylesheets for AiiDAlab Widgets Base | ||
|
||
This folder contains `.css` stylesheets, which are loaded on any import from the AiiDAlab widgets base package. |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
.info-box { | ||
display: none; | ||
margin: 2px; | ||
padding: 1em; | ||
border: 3px solid orangered; | ||
background-color: #ffedcc; | ||
border-radius: 1em; | ||
-webkit-border-radius: 1em; | ||
-moz-border-radius: 1em; | ||
-ms-border-radius: 1em; | ||
-o-border-radius: 1em; | ||
} | ||
.info-box p { | ||
line-height: 24px; | ||
} | ||
.info-box.in-app-guide.show { | ||
display: flex !important; | ||
} | ||
|
||
.first-time-users-infobox { | ||
display: none; | ||
max-width: 500px; | ||
margin: 2px auto !important; | ||
} | ||
.first-time-users-infobox button { | ||
width: fit-content; | ||
background: none; | ||
color: black; | ||
cursor: pointer !important; | ||
} | ||
.first-time-users-infobox button:hover, .first-time-users-infobox button:focus { | ||
outline: none !important; | ||
box-shadow: none !important; | ||
color: orangered; | ||
} | ||
.first-time-users-infobox button:active { | ||
background: none; | ||
color: #cc3700; | ||
} | ||
.first-time-users-infobox .info-box { | ||
display: none; | ||
} | ||
.first-time-users-infobox .info-box button { | ||
position: absolute; | ||
top: 0; | ||
right: 6px; | ||
padding: 0; | ||
font-size: large; | ||
} | ||
.first-time-users-infobox .info-box button:hover, .first-time-users-infobox .info-box button:focus { | ||
transform: scale(1.1); | ||
-webkit-transform: scale(1.1); | ||
-moz-transform: scale(1.1); | ||
-ms-transform: scale(1.1); | ||
-o-transform: scale(1.1); | ||
} | ||
.first-time-users-infobox .info-box .widget-html { | ||
padding-right: 20px; | ||
} | ||
.first-time-users-infobox .closing-message { | ||
display: none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from __future__ import annotations | ||
|
||
from importlib.resources import Package, files | ||
from pathlib import Path | ||
|
||
from IPython.display import Javascript, display | ||
|
||
|
||
def load_css_stylesheet( | ||
package: Package | None = None, | ||
css_path: str | Path = "", | ||
filename: str = "", | ||
): | ||
"""Load a CSS stylesheet from a package and inject it into the DOM. | ||
|
||
Parameters | ||
---------- | ||
`package` : `Package`, optional | ||
The package where the CSS file is located. | ||
`css_path` : `str` | `Path`, optional | ||
The path to the folder where the CSS file is located. | ||
`filename` : `str`, optional | ||
The name of the CSS file to load. | ||
If not provided, all CSS files in the package/folder will be loaded. | ||
""" | ||
if package: | ||
root = files(package) | ||
filenames = ( | ||
[root / filename] | ||
if filename | ||
else [ | ||
root / path.name | ||
for path in root.iterdir() | ||
if path.is_file() and path.name.endswith(".css") | ||
] | ||
) | ||
elif css_path: | ||
path = Path(css_path) | ||
filenames = [path / filename] if filename else [*path.glob("*.css")] | ||
else: | ||
raise ValueError("Either `package` or `path` must be provided.") | ||
|
||
for fn in filenames: | ||
stylesheet = fn.read_text() | ||
display( | ||
Javascript(f""" | ||
var style = document.createElement('style'); | ||
style.type = 'text/css'; | ||
style.innerHTML = `{stylesheet}`; | ||
document.head.appendChild(style); | ||
""") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import ipywidgets as ipw" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from aiida import load_profile\n", | ||
"\n", | ||
"load_profile();" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from aiidalab_widgets_base.utils.loaders import load_css_stylesheet\n", | ||
"\n", | ||
"load_css_stylesheet(css_path=\"../tests_notebooks/static/styles\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"label = ipw.Label(\"Testing\")\n", | ||
"label.add_class(\"red-text\")\n", | ||
"display(label)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if I have two
FirstTimeUserMessage
in my app? They will share the same config file; thus, there is a risk that they are in conflict.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. This isn't a great approach. Username would be better, or some other uniquely identifying property. This was just a proof of concept. I was hoping someone with more knowledge of AiiDAlab could provide a better solution :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this even be a backend logic? For example, our aiidalab deployment is currently shared in the group, so you can't really identify new user this way.
Probably more canonical solution is to store a cookie in user's browser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like how I've implemented it. Just needed a proof of concept. Open to suggestions. A cookie to the user's browser may be a good approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@giovannipizzi suggested we keep this as a local config file and had some ideas for its structure. I'll let him explain it.