4
4
5
5
import ipywidgets as ipw
6
6
from IPython .display import clear_output
7
- from traitlets import Dict , Instance , Unicode , Union , link , validate
7
+ from traitlets import Bool , Dict , Instance , Unicode , Union , link , validate
8
8
9
9
from aiida .orm import Code , QueryBuilder , User
10
10
@@ -30,35 +30,43 @@ class CodeDropdown(ipw.VBox):
30
30
codes(Dict): Trait that contains a dictionary (label => Code instance) for all
31
31
codes found in the AiiDA database for the selected plugin. It is linked
32
32
to the 'options' trait of the `self.dropdown` widget.
33
+
34
+ allow_hidden_codes(Bool): Trait that defines whether to show hidden codes or not.
35
+
36
+ allow_disabled_computers(Bool): Trait that defines whether to show codes on disabled
37
+ computers.
33
38
"""
34
39
selected_code = Union ([Unicode (), Instance (Code )], allow_none = True )
35
40
codes = Dict (allow_none = True )
41
+ allow_hidden_codes = Bool (False )
42
+ allow_disabled_computers = Bool (False )
36
43
37
- def __init__ (self , input_plugin , text = 'Select code:' , path_to_root = '../' , ** kwargs ):
44
+ def __init__ (self , input_plugin , description = 'Select code:' , path_to_root = '../' , ** kwargs ):
38
45
"""Dropdown for Codes for one input plugin.
39
46
40
- :param input_plugin: Input plugin of codes to show
41
- :type input_plugin: str
42
- :param text: Text to display before dropdown
43
- :type text: str
47
+ input_plugin (str): Input plugin of codes to show.
48
+
49
+ description (str): Description to display before the dropdown.
44
50
"""
51
+ self .output = ipw .Output ()
45
52
46
53
self .input_plugin = input_plugin
47
- self .output = ipw .Output ()
48
54
49
- self .dropdown = ipw .Dropdown (optionsdescription = text , disabled = True , value = None )
55
+ self .dropdown = ipw .Dropdown (description = description , disabled = True , value = None )
50
56
link ((self , 'codes' ), (self .dropdown , 'options' ))
51
57
link ((self .dropdown , 'value' ), (self , 'selected_code' ))
52
58
53
- self ._btn_refresh = ipw .Button (description = "Refresh" , layout = ipw .Layout (width = "70px" ))
54
- self ._btn_refresh .on_click (self .refresh )
59
+ btn_refresh = ipw .Button (description = "Refresh" , layout = ipw .Layout (width = "70px" ))
60
+ btn_refresh .on_click (self .refresh )
61
+
62
+ self .observe (self .refresh , names = ['allow_disabled_computers' , 'allow_hidden_codes' ])
55
63
56
64
# FOR LATER: use base_url here, when it will be implemented in the appmode.
57
65
self ._setup_another = ipw .HTML (value = """<a href={path_to_root}aiidalab-widgets-base/setup_code.ipynb?
58
66
label={label}&plugin={plugin} target="_blank">Setup new code</a>""" .format (
59
67
path_to_root = path_to_root , label = input_plugin , plugin = input_plugin ))
60
68
61
- children = [ipw .HBox ([self .dropdown , self . _btn_refresh , self ._setup_another ]), self .output ]
69
+ children = [ipw .HBox ([self .dropdown , btn_refresh , self ._setup_another ]), self .output ]
62
70
63
71
super ().__init__ (children = children , ** kwargs )
64
72
@@ -67,23 +75,15 @@ def __init__(self, input_plugin, text='Select code:', path_to_root='../', **kwar
67
75
def _get_codes (self ):
68
76
"""Query the list of available codes."""
69
77
70
- querybuild = QueryBuilder ()
71
- querybuild .append (Code ,
72
- filters = {
73
- 'attributes.input_plugin' : {
74
- '==' : self .input_plugin
75
- },
76
- 'extras.hidden' : {
77
- "~==" : True
78
- }
79
- },
80
- project = ['*' ])
81
-
82
- # Only codes on computers configured for the current user.
78
+ user = User .objects .get_default ()
79
+
83
80
return {
84
81
self ._full_code_label (c [0 ]): c [0 ]
85
- for c in querybuild .all ()
86
- if c [0 ].computer .is_user_configured (User .objects .get_default ())
82
+ for c in QueryBuilder ().append (Code , filters = {
83
+ 'attributes.input_plugin' : self .input_plugin
84
+ }).all ()
85
+ if c [0 ].computer .is_user_configured (user ) and (self .allow_hidden_codes or not c [0 ].hidden ) and
86
+ (self .allow_disabled_computers or c [0 ].computer .is_user_enabled (user ))
87
87
}
88
88
89
89
@staticmethod
0 commit comments