|
1 |
| -import dash_core_components as dcc |
2 |
| -import dash_html_components as html |
3 |
| - |
4 |
| -from json import loads, dumps |
5 |
| -from monty.json import MSONable, MontyDecoder |
6 |
| - |
7 |
| -from mp_dash_components.layouts.structure import structure_layout |
8 |
| -from pymatgen import Structure |
9 |
| - |
10 |
| - |
11 |
| -def sanitize_input(msonable_object): |
12 |
| - |
13 |
| - # restrict to loading from supported classes to be slightly safer |
14 |
| - # tuple of (module, class) |
15 |
| - SUPPORTED_OBJECTS = [('pymatgen.core.structure', 'Structure')] |
16 |
| - |
17 |
| - if isinstance(msonable_object, MSONable): |
18 |
| - return msonable_object |
19 |
| - elif type(msonable_object) == str: |
20 |
| - msonable_object = loads(msonable_object) |
21 |
| - |
22 |
| - if (type(msonable_object) == dict) and ((msonable_object.get('@module', None), |
23 |
| - msonable_object.get('@class', None)) |
24 |
| - in SUPPORTED_OBJECTS): |
25 |
| - msonable_object = MontyDecoder(dumps(msonable_object)) |
26 |
| - else: |
27 |
| - # if it's not supported, convert back to string |
28 |
| - msonable_object = dumps(msonable_object, indent=4) |
29 |
| - |
30 |
| - return msonable_object |
31 |
| - |
32 |
| -# TODO: remove |
33 |
| -def mp_component(msonable_object, app, id=None, *args, **kwargs): |
34 |
| - """ |
35 |
| - :param msonable_object: an MSONable object, or the JSON string representation |
36 |
| - of an MSONable object, or the dictionary representation of an MSONable object |
37 |
| - :return: Dash layout |
38 |
| - """ |
39 |
| - |
40 |
| - msonable_object = sanitize_input(msonable_object) |
41 |
| - |
42 |
| - if isinstance(msonable_object, Structure): |
43 |
| - return structure_layout(msonable_object, app, structure_viewer_id=id, *args, **kwargs) |
44 |
| - elif isinstance(msonable_object, str): |
45 |
| - return dcc.SyntaxHighlighter(id=id, children="'''\n{}\n'''".format(msonable_object)) |
46 |
| - else: |
47 |
| - raise ValueError("Cannot generate a layout for this object.") |
0 commit comments