1
1
"""Widgets to work with processes."""
2
2
3
3
import os
4
- from ipywidgets import Textarea , VBox
4
+ from ipywidgets import Button , HTML , IntProgress , Layout , Textarea , VBox
5
+
6
+ # AiiDA imports
7
+ from aiida .engine import submit
8
+ from aiida .orm import CalcJobNode , ProcessNode , WorkChainNode
5
9
6
10
7
11
def get_running_calcs (process ):
8
12
"""Takes a process and returns a list of running calculations. The running calculations
9
13
can be either the process itself or its running children."""
10
14
11
- from aiida .orm import CalcJobNode , ProcessNode , WorkChainNode
12
-
13
15
# If a process is a running calculation - returning it
14
16
if issubclass (type (process ), CalcJobNode ) and not process .is_sealed :
15
17
return [process ]
@@ -28,19 +30,20 @@ def get_running_calcs(process):
28
30
class SubmitButtonWidget (VBox ):
29
31
"""Submit button class that creates submit button jupyter widget."""
30
32
31
- def __init__ (self , process , widgets_values ):
33
+ def __init__ (self , process , input_dictionary_function , description = "Submit" ):
32
34
"""Submit Button
33
35
:process: work chain to run
34
36
:param_funtion: the function that generates input parameters dictionary
35
37
"""
36
- from ipywidgets import Button , Output
37
38
38
39
self .process = None
39
40
self ._process_class = process
40
- self .widgets_values = widgets_values
41
- self .btn_submit = Button (description = "Submit" , disabled = False )
41
+ self ._run_after_submitted = []
42
+
43
+ self .input_dictionary_function = input_dictionary_function
44
+ self .btn_submit = Button (description = description , disabled = False )
42
45
self .btn_submit .on_click (self .on_btn_submit_press )
43
- self .submit_out = Output ( )
46
+ self .submit_out = HTML ( '' )
44
47
children = [
45
48
self .btn_submit ,
46
49
self .submit_out ,
@@ -50,30 +53,33 @@ def __init__(self, process, widgets_values):
50
53
def on_click (self , function ):
51
54
self .btn_submit .on_click (function )
52
55
53
- def on_btn_submit_press (self , _ ):
56
+ def on_btn_submit_press (self , _ = None ):
54
57
"""When submit button is pressed."""
55
- from IPython . display import clear_output
56
- from aiida . engine import submit
57
-
58
- with self .submit_out :
59
- clear_output ()
58
+ self . submit_out . value = ''
59
+ input_dict = self . input_dictionary_function ()
60
+ if input_dict is None :
61
+ self .submit_out . value = "SubmitButtonWidget: did not recieve input dictionary."
62
+ else :
60
63
self .btn_submit .disabled = True
61
- input_dict = self .widgets_values ()
62
64
self .process = submit (self ._process_class , ** input_dict )
63
- print ("Submitted process {}" .format (self .process ))
64
- return
65
+ self .submit_out .value = "Submitted process {}" .format (self .process )
66
+ for func in self ._run_after_submitted :
67
+ func (self .process )
68
+
69
+ def on_submitted (self , function ):
70
+ """Run functions after a process has been submitted sucesfully."""
71
+ self ._run_after_submitted .append (function )
65
72
66
73
67
74
class ProcessFollowerWidget (VBox ):
68
75
"""A Widget that follows a process until finished."""
69
76
70
77
def __init__ (self , process , followers = None , update_interval = 0.1 , ** kwargs ):
71
78
"""Initiate all the followers."""
72
- from aiida .orm import ProcessNode
73
-
74
79
if not isinstance (process , ProcessNode ):
75
80
raise TypeError ("Expecting an object of type {}, got {}" .format (ProcessNode , type (process )))
76
81
self .process = process
82
+ self ._run_after_completed = []
77
83
self .update_interval = update_interval
78
84
self .followers = []
79
85
if followers is not None :
@@ -102,13 +108,18 @@ def follow(self, detach=False):
102
108
update_state .start ()
103
109
else :
104
110
self ._follow ()
111
+ for func in self ._run_after_completed :
112
+ func (self .process )
113
+
114
+ def on_completed (self , function ):
115
+ """Run functions after a process has been completed."""
116
+ self ._run_after_completed .append (function )
105
117
106
118
107
119
class ProgressBarWidget (VBox ):
108
120
"""A bar showing the proggress of a process."""
109
121
110
122
def __init__ (self , process , ** kwargs ):
111
- from ipywidgets import HTML , IntProgress , Layout
112
123
113
124
self .process = process
114
125
self .correspondance = {
0 commit comments