Skip to content
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

Bug fixes and testing #386

Merged
merged 11 commits into from
May 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion crystal_toolkit/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2023.11.03"
__version__ = "2023.11.4.dev18"
2 changes: 1 addition & 1 deletion crystal_toolkit/apps/examples/tests/test_bandstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def test_bs(dash_duo: DashDuo) -> None:
# dash_duo.percy_snapshot("example_bsdos_projection_index_2")

logs = dash_duo.get_logs()
assert logs == [], f"Unexpected browser {logs=}"
assert logs is None, f"Unexpected browser {logs=}"
6 changes: 3 additions & 3 deletions crystal_toolkit/apps/examples/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_hello_scientist(dash_duo: DashDuo):
dash_duo.percy_snapshot("hello_scientist")

logs = dash_duo.get_logs()
assert logs == [], f"Unexpected browser {logs=}"
assert logs is None, f"Unexpected browser {logs=}"


def test_hello_structure(dash_duo: DashDuo) -> None:
Expand All @@ -33,7 +33,7 @@ def test_hello_structure(dash_duo: DashDuo) -> None:
dash_duo.percy_snapshot("hello_structure")

logs = dash_duo.get_logs()
assert logs == [], f"Unexpected browser {logs=}"
assert logs is None, f"Unexpected browser {logs=}"


def test_hello_structure_interactive(dash_duo: DashDuo) -> None:
Expand All @@ -47,4 +47,4 @@ def test_hello_structure_interactive(dash_duo: DashDuo) -> None:
dash_duo.percy_snapshot("hello_structure_interactive_on_click")

logs = dash_duo.get_logs()
assert logs == [], f"Unexpected browser {logs=}"
assert logs is None, f"Unexpected browser {logs=}"
2 changes: 1 addition & 1 deletion crystal_toolkit/apps/examples/tests/test_diffraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def test_diffraction(dash_duo: DashDuo) -> None:
input_nodes[0].send_keys(Keys.ARROW_UP)

logs = dash_duo.get_logs()
assert logs == [], f"Unexpected browser {logs=}"
assert logs is None, f"Unexpected browser {logs=}"
6 changes: 3 additions & 3 deletions crystal_toolkit/apps/examples/tests/test_fermi_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from typing import TYPE_CHECKING

from crystal_toolkit.apps.examples.fermi_surface import app

if TYPE_CHECKING:
from crystal_toolkit.apps.examples.tests.typing import DashDuo


def test_diffraction(dash_duo: DashDuo) -> None:
from crystal_toolkit.apps.examples.fermi_surface import app

dash_duo.start_server(app)
dash_duo.clear_storage()

Expand All @@ -17,4 +17,4 @@ def test_diffraction(dash_duo: DashDuo) -> None:
assert "dash-graph" in node.get_attribute("class")

logs = dash_duo.get_logs()
assert logs == [], f"Unexpected browser {logs=}"
assert logs is None, f"Unexpected browser {logs=}"
2 changes: 1 addition & 1 deletion crystal_toolkit/apps/examples/tests/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def test_structure(dash_duo: DashDuo) -> None:
sleep(1)

logs = dash_duo.get_logs()
assert logs == [], f"Unexpected browser {logs=}"
assert logs is None, f"Unexpected browser {logs=}"
6 changes: 3 additions & 3 deletions crystal_toolkit/apps/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import time
from typing import TYPE_CHECKING

from crystal_toolkit.apps.main import app

if TYPE_CHECKING:
from crystal_toolkit.apps.examples.tests.typing import DashDuo


def test_main_app_startup(dash_duo: DashDuo):
from crystal_toolkit.apps.main import app

dash_duo.start_server(app)
# dash_duo.clear_storage()

Expand All @@ -20,4 +20,4 @@ def test_main_app_startup(dash_duo: DashDuo):
dash_duo.take_snapshot("main_app_startup-layout")

logs = dash_duo.get_logs()
assert logs == [], f"Unexpected browser {logs=}"
assert logs is None, f"Unexpected browser {logs=}"
8 changes: 4 additions & 4 deletions crystal_toolkit/apps/tests/test_pourbaix.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from playwright.sync_api import Page

from crystal_toolkit.apps.examples.pourbaix import app

thread = threading.Thread(target=app.run)
thread.start()
def test_pourbaix(page: Page):
from crystal_toolkit.apps.examples.pourbaix import app

thread = threading.Thread(target=app.run)
thread.start()

def test_pourbaix(page: Page):
# select 1st structure
page.locator(".react-select__input-container").click()
page.get_by_text("Fe₃H (mp-1184287-GGA)", exact=True).click()
Expand Down
8 changes: 4 additions & 4 deletions crystal_toolkit/apps/tests/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

from playwright.sync_api import Page, expect

from crystal_toolkit.apps.examples.structure import app

thread = threading.Thread(target=app.run)
thread.start()
def test_structure(page: Page):
from crystal_toolkit.apps.examples.structure import app

thread = threading.Thread(target=app.run)
thread.start()

def test_structure(page: Page):
expect(page).to_have_title("Crystal Toolkit")
h1_text = page.text_content("h1")
assert h1_text == "StructureMoleculeComponent Example"
Expand Down
9 changes: 5 additions & 4 deletions crystal_toolkit/core/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from warnings import warn

from dash import Dash
from monty.json import jsanitize
from pymatgen.analysis.graphs import MoleculeGraph, StructureGraph
from pymatgen.core.structure import SiteCollection

Expand Down Expand Up @@ -117,20 +118,20 @@ def _ipython_display_(self):
# and also check all .parameters .kind.name have no POSITIONAL_ONLY
# in practice, fairly unlikely this will cause issues without strict checking.
# TODO: This can be removed once a central registry of renderable objects is implemented.
if self.get_scene:
if hasattr(self, "get_scene"):
display_data = {
"application/vnd.mp.ctk+json": self.get_scene().to_json(),
"text/plain": repr(self),
}
elif self.get_plot:
elif hasattr(self, "get_plot"):
display_data = {
"application/vnd.plotly.v1+json": self.get_plot().to_plotly_json(),
"application/json": self.as_dict(),
"application/json": jsanitize(self.as_dict()),
"text/plain": repr(self),
}
else:
display_data = {
"application/json": self.as_dict(),
"application/json": jsanitize(self.as_dict()),
"text/plain": repr(self),
}

Expand Down
6 changes: 5 additions & 1 deletion crystal_toolkit/core/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ def get_color(self, sp: Species | Element, site: Site | None = None) -> str:
Returns: Color
"""
# allow manual override by user
if site and "display_color" in site.properties:
if (
site
and "display_color" in site.properties
and site.properties["display_color"] is not None
):
color = site.properties["display_color"]
# TODO: next two lines due to change in API, will be removed
if isinstance(color, list) and isinstance(color[0], str):
Expand Down
1 change: 0 additions & 1 deletion crystal_toolkit/core/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def _repr_mimebundle_(self, include=None, exclude=None):
"""Render Scenes using crystaltoolkit-extension for Jupyter Lab."""
return {
"application/vnd.mp.ctk+json": self.to_json(),
"application/json": self.as_dict(),
"text/plain": repr(self),
}

Expand Down
File renamed without changes.
35 changes: 35 additions & 0 deletions tests/asy_test/core/test_jupyter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import plotly.express as px
from monty.json import MSONable

from crystal_toolkit.core.jupyter import patch_msonable
from crystal_toolkit.core.scene import Scene


def test_patch_msonable():
patch_msonable()

class GetSceneClass(MSONable):
def get_scene(self):
return Scene(name="test_scene")

class GetPlotClass(MSONable):
def get_plot(self):
"""Dummy plotly object"""
return px.scatter(x=[1, 2, 3], y=[1, 2, 3])

class AsDictClass(MSONable):
def __init__(self, a: int) -> None:
self.a = a

# The output of _ipython_display_ is None
# However, the logic for the creating the different output
# dictionaries should be executed so the following tests
# are still valuable.
as_dict_class = AsDictClass(1)
assert as_dict_class._ipython_display_() is None

get_scene_class = GetSceneClass()
assert get_scene_class._ipython_display_() is None

get_plot_class = GetPlotClass()
assert get_plot_class._ipython_display_() is None
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ def test_get_color(self):
assert color == "#b30326"

color = legend.get_color(self.sp1, site=self.site1)
assert color == "#dddcdb"
assert color == "#000000"

color = legend.get_color(self.sp2, site=self.site2)
assert color == "#7b9ef8"

assert legend.get_legend()["colors"] == {
"#7b9ef8": "-3.00",
"#b30326": "5.00",
"#dddcdb": "0.00",
"#000000": "0.00",
}

# test accessible
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_get_color(self):
legend = Legend(self.struct_manual)

assert legend.get_legend()["colors"] == {
"#0000ff": "O2-",
"#0000ff": "O²⁻",
"#00ff00": "In",
"#ff0000": "H",
}
Expand Down
Loading