Skip to content

Commit d5c4caa

Browse files
committed
Merge branch 'develop'
2 parents 291a9b1 + f92516d commit d5c4caa

File tree

8 files changed

+297
-358
lines changed

8 files changed

+297
-358
lines changed

aiidalab_widgets_base/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
if not is_dbenv_loaded():
55
load_dbenv(profile=settings.AIIDADB_PROFILE)
66

7-
from .structures import StructureUploadWidget # noqa
8-
from .structures_multi import MultiStructureUploadWidget # noqa
97
from .codes import CodeDropdown, AiiDACodeSetup, extract_aiidacodesetup_arguments # noqa
108
from .computers import SshComputerSetup, extract_sshcomputersetup_arguments # noqa
119
from .computers import AiidaComputerSetup, extract_aiidacomputer_arguments # noqa
10+
from .databases import CodQueryWidget # noqa
1211
from .display import aiidalab_display # noqa
12+
from .structures import StructureUploadWidget # noqa
13+
from .structures_multi import MultiStructureUploadWidget # noqa
1314

14-
__version__ = "0.3.0b1"
15+
__version__ = "0.4.0b1"

aiidalab_widgets_base/codes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def __init__(self, input_plugin, text='Select code:', **kwargs):
4141
self.dropdown = ipw.Dropdown(description=text, disabled=True)
4242
self._btn_refresh = ipw.Button(description="Refresh", layout=ipw.Layout(width="70px"))
4343
self._btn_refresh.on_click(self.refresh)
44-
self._setup_another = ipw.HTML(value="""<a href=./setup_code.ipynb target="_blank">Setup new code</a>""")
44+
# TODO: use base_url here
45+
self._setup_another = ipw.HTML(value="""<a href=../aiidalab-widgets-base/setup_code.ipynb target="_blank">Setup new code</a>""")
4546
self.output = ipw.Output()
4647

4748
children = [ipw.HBox([self.dropdown, self._btn_refresh, self._setup_another]),

aiidalab_widgets_base/databases.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
from __future__ import print_function
2+
import ipywidgets as ipw
3+
4+
from aiida.tools.dbimporters.plugins.cod import CodDbImporter
5+
6+
class CodQueryWidget(ipw.VBox):
7+
'''Query structures in Crystallography Open Database (COD)
8+
Useful class members:
9+
:ivar has_structure: link to a method to be overloaded. It is called evey time when `self.drop_structure`
10+
widget has changed its name
11+
:vartype has_structure: function
12+
'''
13+
14+
def __init__(self, **kwargs):
15+
description = ipw.HTML("""<h3>Get crystal structures from
16+
<a href="http://www.crystallography.net">Crystallography Open Database</a></h3>
17+
<b>Queries by formula</b>
18+
<br>
19+
For the queries by formula please adhere to the Hill notation.
20+
The chemical symbol of an element should be followed by its number in the structure,
21+
except when there is only one atom.
22+
When the structure does NOT contain carbon atoms, all the elements are listed alphabetically.
23+
Example: <i>O2 Si</i>.
24+
<br>
25+
In case the structure DOES contain carbon atoms its number following the 'C' symbol is indicated first.
26+
If hydrogen is also present in the structure then 'H' symbol and its number is indicated second.
27+
The remaining elements are listed in the alphabetical order. Example: <i>C H4 N2 O</i>.
28+
<br>
29+
<b>Queries by the structure id number</b>
30+
<br>
31+
For the queries by structure id, plese provide the database id number. Example: <i>1008786</i>
32+
""")
33+
layout = ipw.Layout(width="400px")
34+
style = {"description_width":"initial"}
35+
self.inp_elements = ipw.Text(description="", value="", placeholder='e.g.: Ni Ti or id number', layout=layout, style=style)
36+
self.btn_query = ipw.Button(description='Query')
37+
self.query_message = ipw.HTML("Waiting for input...")
38+
self.drop_structure = ipw.Dropdown(description="", options=[("select structure",{"status":False})],
39+
style=style, layout=layout )
40+
self.link = ipw.HTML("Link to the web-page will appear here")
41+
self.structure_ase = None
42+
43+
self.btn_query.on_click(self._on_click_query)
44+
self.drop_structure.observe(self._on_select_structure, names=['value'])
45+
46+
children = [description,
47+
ipw.HBox([self.btn_query, self.inp_elements]),
48+
self.query_message,
49+
ipw.HBox([self.drop_structure, self.link])]
50+
super(CodQueryWidget, self).__init__(children=children, **kwargs)
51+
52+
def _query(self, idn=None,formula=None):
53+
importer = CodDbImporter()
54+
if idn is not None:
55+
return importer.query(id=idn)
56+
elif formula is not None:
57+
return importer.query(formula=formula)
58+
59+
def _on_click_query(self, change):
60+
structures = [("select structure", {"status":False})]
61+
idn = None
62+
formula = None
63+
self.query_message.value = "Quering the database ... "
64+
try:
65+
idn = int(self.inp_elements.value)
66+
except:
67+
formula = str(self.inp_elements.value)
68+
69+
for entry in self._query(idn=idn, formula=formula):
70+
try:
71+
entry_cif = entry.get_cif_node()
72+
formula = entry_cif.get_ase().get_chemical_formula()
73+
except:
74+
continue
75+
entry_add = ("{} (id: {})".format(formula, entry.source['id']),
76+
{
77+
"status": True,
78+
"cif": entry_cif,
79+
"url": entry.source['uri'],
80+
"id": entry.source['id'],
81+
}
82+
)
83+
structures.append(entry_add)
84+
85+
self.query_message.value += "{} structures found".format(len(structures)-1)
86+
self.drop_structure.options = structures
87+
88+
def _on_select_structure(self, change):
89+
selected = change['new']
90+
if selected['status'] is False:
91+
self.structure_ase = None
92+
return
93+
self.structure_ase = selected['cif'].get_ase()
94+
formula = self.structure_ase.get_chemical_formula()
95+
struct_url = selected['url'].split('.cif')[0]+'.html'
96+
self.link.value='<a href="{}" target="_blank">COD entry {}</a>'.format(struct_url, selected['id'])
97+
if not self.on_structure_selection is None:
98+
self.on_structure_selection(structure_ase=self.structure_ase, name=formula)
99+
100+
def on_structure_selection(self, structure_ase=None, name=None):
101+
pass

0 commit comments

Comments
 (0)