-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
205 lines (176 loc) · 6.43 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
BlueSky Graphical User Interface.
Built using Dash - https://dash.plotly.com/.
Created on Wed Sept 19 2024 by Adam Heisey
"""
import dash
from dash import dcc, html, Input, Output, State
import dash_bootstrap_components as dbc
import toml
import base64
import io
import ast
import subprocess
from pathlib import Path
from datetime import datetime
from definitions import PROJECT_ROOT
import os
import tomli
import tomlkit
import sys
import shlex
# Initialize the Dash app
app = dash.Dash(
__name__,
prevent_initial_callbacks=True,
external_stylesheets=[dbc.themes.BOOTSTRAP],
assets_folder='docs/images/',
)
app.title = 'BlueSky Model Runner'
docs_dir = os.path.abspath('docs/build/html')
# use the current python interpreter to run the html docs in the background
with open(os.devnull, 'w') as devnull:
http_server_process = subprocess.Popen(
[sys.executable, '-m', 'http.server', '8000', '--directory', docs_dir],
stdout=devnull,
stderr=devnull,
)
# blusesky image in assets folder
image_src = app.get_asset_url('ProjectBlueSkywebheaderimageblack.jpg')
# Define layout
app.layout = dbc.Container(
[
dbc.Row(
dbc.Col(
dbc.Button(
'Code Documentation',
href='http://localhost:8000/index.html',
color='info',
className='mt-3',
target='_blank',
),
width='auto',
className='text-left',
),
justify='start',
),
html.H1('BlueSky Model Runner', className='text-center'),
html.Img(src=image_src),
html.H2(id='status', className='text-center', style={'color': 'red'}),
html.H3(id='output-state'),
dbc.Label('Select Mode to Run:'),
dcc.RadioItems(
id='mode-selector',
options=[
{'label': mode, 'value': mode}
for mode in ['unified-combo', 'gs-combo', 'standalone']
],
value='standalone',
),
dbc.Button('Run', id='run-button', color='primary', className='mt-2'),
dcc.Loading(dbc.Progress(id='progress', value=0, max=100, style={'height': '30px'})),
# Section for uploading and editing TOML config file
html.Hr(),
html.H4('Edit Configuration Settings'),
# dcc.Upload(id='upload-toml', children=html.Button('Upload TOML'), multiple=False),
html.Div(id='config-editor'),
dbc.Button('Save Changes', id='save-toml-button', className='mt-2', disabled=False),
],
fluid=True,
)
# Auto load the template
@app.callback(
Output('config-editor', 'children'),
Output('save-toml-button', 'disabled'),
Input('config-editor', 'id'),
prevent_initial_call=False,
)
def auto_load_toml(selected_mode):
config_file_path = os.path.join('src/integrator', 'run_config_template.toml')
if os.path.exists(config_file_path):
# read run config with comments
with open(config_file_path, 'rb') as f:
config_content = tomli.load(f)
# Dynamically create input fields for the TOML content
inputs = []
for key, value in config_content.items():
inputs.append(
html.Div(
[
dbc.Label(f'{key}:'),
dbc.Input(
id={'type': 'config-input', 'index': key},
value=str(value),
debounce=True,
),
],
style={'margin-bottom': '10px'},
)
)
return inputs, False # Enable the Save button
return [], True # Disable the Save button if no file is uploaded
# Save the modified TOML file with comments
@app.callback(
Output('output-state', 'children'),
Input('save-toml-button', 'n_clicks'),
State({'type': 'config-input', 'index': dash.ALL}, 'value'),
State({'type': 'config-input', 'index': dash.ALL}, 'id'),
prevent_initial_call=True,
)
def save_toml(n_clicks, input_values, input_ids):
config_template = os.path.join('src/integrator', 'run_config_template.toml')
config_file_path = os.path.join('src/integrator', 'run_config.toml')
if n_clicks:
# Load the original file to preserve its structure and comments
with open(config_template, 'r') as f:
config_doc = tomlkit.parse(f.read())
# Update the config_doc with new values
for item, value in zip(input_ids, input_values):
config_doc[item['index']] = convert_value(value)
# Write the updated content back, preserving comments
with open(config_file_path, 'w') as f:
f.write(tomlkit.dumps(config_doc))
return f"Configuration settings saved successfully as 'run_config.toml'."
return ''
# Function to convert values back to original types
def convert_value(value):
"""Function to maintain the original type for config values"""
# handle boolean speficially
if isinstance(value, str):
if value.lower() == 'true':
return True
elif value.lower() == 'false':
return False
# now other types
try:
return ast.literal_eval(value)
except (ValueError, SyntaxError):
return value
# Callback to handle run button click and show progress
@app.callback(
Output('status', 'children'),
Output('progress', 'value'),
Input('run-button', 'n_clicks'),
State('mode-selector', 'value'),
prevent_initial_call=True,
)
def run_mode(n_clicks, selected_mode):
# define modes allowed - sanitize user input
modes_available = {'unified-combo', 'gs-combo', 'standalone'}
if selected_mode not in modes_available:
return f"Error: '{selected_mode}' is not a valide mode.", 0
try:
selected_mode = shlex.quote(selected_mode)
subprocess.run(['python', 'main.py', '--mode', selected_mode], check=True)
return (
f"{selected_mode.capitalize()} mode has finished running. See results in output/'{selected_mode}'.",
100,
)
except subprocess.CalledProcessError as e:
error_msg = f'Error, not able to run {selected_mode}. Please check the log script/terminal, exit out of browser, and restart.'
return error_msg, 0
if __name__ == '__main__':
try:
app.run_server(debug=True, port=8080)
finally:
http_server_process.terminate()