Skip to content

Commit

Permalink
Remove leftover load profile (#481)
Browse files Browse the repository at this point in the history
The `load_profile()` in `__init__.py` to load the profile in module scope was introduced by PR #12. It is not needed anymore since the computer and code widgets are updated and can update the dropdown dynamically.

More problematically that this makes the profile loaded in the unit tests and overrides the test profile which makes it has to put the module load inside every test. There is also the problem that if the user want to test the widgets in a newly create python environment that doesn't have an AiiDA profile setup impossible.

In this commit, we detect if the profile is loaded by the notebook, if not, we load it for the user and raising a deprecated warning. This makes it possible to keep backward compatibility thus not breaking the app that doesn't load profile manually.
  • Loading branch information
unkcpz authored Aug 18, 2023
1 parent a6fcf06 commit 413c26c
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 89 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ name: continuous-integration

on: [push, pull_request]


# https://docs.github.com/en/actions/using-jobs/using-concurrency
concurrency:
# only cancel in-progress jobs or runs for the current workflow - matches against branch & tags
Expand Down
42 changes: 41 additions & 1 deletion aiidalab_widgets_base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
"""Reusable widgets for AiiDAlab applications."""
from aiida.manage import get_profile

_WARNING_TEMPLATE = """
<div style="background-color: #f7f7f7; border: 2px solid #e0e0e0; padding: 20px; border-radius: 5px;">
<p style="font-size: 16px; font-weight: bold; color: #ff5733;">Warning:</p>
<p style="font-size: 14px;">The default profile '<span style="font-style: italic;">{profile}</span>' was loaded automatically. This behavior will be removed in the <span style="font-style: italic;">{version}</span>. Please load the profile manually before loading modules from aiidalab-widgets-base by adding the following code at the beginning cell of the notebook:</p>
<pre style="background-color: #f0f0f0; padding: 10px; border: 1px solid #ccc; font-family: 'Courier New', monospace;">
from aiida import load_profile
load_profile();</pre>
</div>
"""


# We only detect profile and throw a warning if it is on the notebook
# It is not necessary to do this in the unit tests
def is_running_in_jupyter():
try:
from IPython import get_ipython

if get_ipython() is not None:
return True
else:
return False
except NameError:
return False


# load the default profile if no profile is loaded, and raise a deprecation warning
# this is a temporary solution to avoid breaking existing notebooks
# this will be removed in the next major release
if is_running_in_jupyter() and get_profile() is None:
# if no profile is loaded, load the default profile and raise a deprecation warning
from aiida import load_profile
from IPython.display import HTML, display

load_profile()

profile = get_profile()
assert profile is not None, "Failed to load the default profile"

load_profile()
# raise a deprecation warning
warning = HTML(_WARNING_TEMPLATE.format(profile=profile.name, version="v3.0.0"))
display(warning)

from .computational_resources import (
ComputationalResourcesWidget,
Expand Down
4 changes: 2 additions & 2 deletions notebooks/aiida_datatypes_viewers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -243,7 +243,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
12 changes: 12 additions & 0 deletions notebooks/computational_resources.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "864bd498",
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"\n",
"load_profile();"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
14 changes: 13 additions & 1 deletion notebooks/eln_configure.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc68ce6f",
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"\n",
"load_profile();"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -60,7 +72,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
14 changes: 13 additions & 1 deletion notebooks/eln_import.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea069627",
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"\n",
"load_profile();"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -117,7 +129,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
13 changes: 12 additions & 1 deletion notebooks/process.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"\n",
"load_profile();"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -99,7 +110,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "base",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand Down
13 changes: 12 additions & 1 deletion notebooks/process_list.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"\n",
"load_profile();"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -125,7 +136,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
11 changes: 11 additions & 0 deletions notebooks/structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"\n",
"load_profile();"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
14 changes: 13 additions & 1 deletion notebooks/wizard_apps.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
"Example illustrating apps that involve multiple stages"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5256919",
"metadata": {},
"outputs": [],
"source": [
"from aiida import load_profile\n",
"\n",
"load_profile();"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -329,7 +341,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
10 changes: 4 additions & 6 deletions tests/test_bug_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
import json
import zlib

from aiidalab_widgets_base import bug_report


def test_fingerprint_parser():
"""Test get_environment_fingerprint function and parse it out."""
from aiidalab_widgets_base.bug_report import (
get_environment_fingerprint,
parse_environment_fingerprint,
)

encoding = "utf-8"
fingerprint = get_environment_fingerprint(encoding)
fingerprint = bug_report.get_environment_fingerprint(encoding)

# Parse the fingerprint.
data = parse_environment_fingerprint(fingerprint)
data = bug_report.parse_environment_fingerprint(fingerprint)

# To test, manually generate the fingerprint and compare it to the output of the parser.
json_data = json.dumps(data, separators=(",", ":"))
Expand Down
29 changes: 10 additions & 19 deletions tests/test_computational_resources.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import pytest
from aiida import orm

from aiidalab_widgets_base import computational_resources


@pytest.mark.usefixtures("aiida_profile_clean")
def test_computaional_resources_widget(aiida_local_code_bash):
"""Test the ComputationalResourcesWidget."""
import aiidalab_widgets_base as awb

widget = awb.ComputationalResourcesWidget(default_calc_job_plugin="bash")
widget = computational_resources.ComputationalResourcesWidget(
default_calc_job_plugin="bash"
)

# Get the list of currently installed codes.
codes_dict = widget._get_codes()
Expand All @@ -16,9 +19,7 @@ def test_computaional_resources_widget(aiida_local_code_bash):
@pytest.mark.usefixtures("aiida_profile_clean")
def test_ssh_computer_setup_widget():
"""Test the SshComputerSetup."""
from aiidalab_widgets_base.computational_resources import SshComputerSetup

widget = SshComputerSetup()
widget = computational_resources.SshComputerSetup()

ssh_config = {
"hostname": "daint.cscs.ch",
Expand Down Expand Up @@ -63,11 +64,7 @@ def test_ssh_computer_setup_widget():
@pytest.mark.usefixtures("aiida_profile_clean")
def test_aiida_computer_setup_widget():
"""Test the AiidaComputerSetup."""
from aiida import orm

from aiidalab_widgets_base.computational_resources import AiidaComputerSetup

widget = AiidaComputerSetup()
widget = computational_resources.AiidaComputerSetup()

# At the beginning, the computer_name should be an empty string.
assert widget.label.value == ""
Expand Down Expand Up @@ -118,11 +115,7 @@ def test_aiida_computer_setup_widget():
@pytest.mark.usefixtures("aiida_profile_clean")
def test_aiida_code_setup(aiida_localhost):
"""Test the AiidaCodeSetup."""
from aiida import orm

from aiidalab_widgets_base.computational_resources import AiidaCodeSetup

widget = AiidaCodeSetup()
widget = computational_resources.AiidaCodeSetup()

# At the beginning, the code_name should be an empty string.
assert widget.label.value == ""
Expand Down Expand Up @@ -164,9 +157,7 @@ def test_aiida_code_setup(aiida_localhost):
@pytest.mark.usefixtures("aiida_profile_clean")
def test_computer_dropdown_widget(aiida_localhost):
"""Test the ComputerDropdownWidget."""
import aiidalab_widgets_base as awb

widget = awb.ComputerDropdownWidget()
widget = computational_resources.ComputerDropdownWidget()

assert "localhost" in widget.computers

Expand Down
Loading

0 comments on commit 413c26c

Please sign in to comment.