|
| 1 | +import json |
| 2 | +import os as _os |
| 3 | +from collections import defaultdict |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +# pleasant hack to support MSONable objects in Dash callbacks natively |
| 7 | +from monty.json import MSONable |
| 8 | + |
| 9 | +from crystal_toolkit.renderables import * |
| 10 | + |
| 11 | +__version__ = "2021.04.29" |
| 12 | + |
| 13 | +MODULE_PATH = Path(__file__).parents[0] |
| 14 | + |
| 15 | + |
| 16 | +def to_plotly_json(self): |
| 17 | + return self.as_dict() |
| 18 | + |
| 19 | + |
| 20 | +MSONable.to_plotly_json = to_plotly_json |
| 21 | + |
| 22 | + |
| 23 | +# Populate the default values from the JSON file |
| 24 | +_DEFAULTS = defaultdict(lambda: None) |
| 25 | +default_js = _os.path.join( |
| 26 | + _os.path.join(_os.path.dirname(_os.path.abspath(__file__))), "./", "defaults.json" |
| 27 | +) |
| 28 | + |
| 29 | +with open(default_js) as handle: |
| 30 | + _DEFAULTS.update(json.loads(handle.read())) |
| 31 | + |
| 32 | + |
| 33 | +def _repr_mimebundle_(self, include=None, exclude=None): |
| 34 | + """ |
| 35 | + Render Scenes using crystaltoolkit-extension for Jupyter Lab. |
| 36 | + """ |
| 37 | + # TODO: add Plotly, application/vnd.plotly.v1+json |
| 38 | + |
| 39 | + help_text_ct = """If you see this text, the Crystal Toolkit Jupyter Lab \n |
| 40 | +extension is not installed. You can install it by running \n |
| 41 | +\"pip install crystaltoolkit-extension\" \n |
| 42 | +from the same environment you run \"jupyter lab\". \n |
| 43 | +This only works in Jupyter Lab 3.x or above.\n\n |
| 44 | +""" |
| 45 | + |
| 46 | + help_text_plotly = """If you see this text, the Plotly Jupyter Lab extension |
| 47 | +is not installed, please consult Plotly documentation for information on how to |
| 48 | +install. |
| 49 | +""" |
| 50 | + |
| 51 | + # TODO: to be strict here, we could use inspect.signature |
| 52 | + # and .return_annotation is either a Scene or a go.Figure respectively |
| 53 | + # and also check all .parameters .kind.name have no POSITIONAL_ONLY |
| 54 | + # in practice, fairly unlikely this will cause issues without strict checking |
| 55 | + |
| 56 | + if hasattr(self, "get_scene"): |
| 57 | + return { |
| 58 | + "application/vnd.mp.ctk+json": self.get_scene().to_json(), |
| 59 | + "text/plain": help_text_ct + self.__repr__(), |
| 60 | + } |
| 61 | + elif hasattr(self, "get_plot"): |
| 62 | + return { |
| 63 | + "application/vnd.plotly.v1+json": self.get_plot().to_plotly_json(), |
| 64 | + "text/plain": help_text_plotly + self.__repr__(), |
| 65 | + } |
| 66 | + else: |
| 67 | + return {"application/json": self.as_dict(), "text/plain": self.__repr__()} |
| 68 | + |
| 69 | + |
| 70 | +MSONable._repr_mimebundle_ = _repr_mimebundle_ |
| 71 | + |
| 72 | + |
| 73 | +def show_json(self): |
| 74 | + from IPython.display import display_json |
| 75 | + |
| 76 | + return display_json(self.as_dict(), raw=True) |
| 77 | + |
| 78 | + |
| 79 | +MSONable.show_json = show_json |
| 80 | + |
| 81 | + |
| 82 | +def _ipython_display_(self): |
| 83 | + """ |
| 84 | + Render Scenes using crystaltoolkit-extension for Jupyter Lab. |
| 85 | +
|
| 86 | + This function ensures that objects are also printed in string format |
| 87 | + as previously. |
| 88 | + """ |
| 89 | + from IPython.display import publish_display_data |
| 90 | + |
| 91 | + publish_display_data(self._repr_mimebundle_()) |
| 92 | + |
| 93 | + |
| 94 | +MSONable._ipython_display_ = _ipython_display_ |
0 commit comments