-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
190 lines (152 loc) · 5.62 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
import base64
import hashlib
import os
import pickle
import subprocess
import fmpy
from pathlib import Path
from dash import Dash, dcc, html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
workdir = os.path.join(os.path.dirname(__file__), 'work')
Path(workdir).mkdir(exist_ok=True)
app = Dash(__name__, external_stylesheets=[
dbc.themes.BOOTSTRAP,
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css'
])
app.title = "FMU Check"
server = app.server
app.layout = dbc.Container([
dcc.Store(id='submitted-store'),
dcc.Store(id='finished-store'),
dcc.Interval(id='update-interval', interval=500),
dbc.Container(
[
html.H2(
[
"FMU Check",
html.Span([html.I(className='fas fa-flask me-2'), "beta"], className='badge badge-secondary ml-3'),
]
, className='bd-title'
),
html.P("Validate an FMU and get the meta information from the model description", className='bd-lead'),
html.P(
html.A([html.I(className='fas fa-info-circle me-2'), "What is being checked?"], id="what-is-validated-link", style={'color': '#007bff', 'cursor': 'pointer'})
),
dbc.Collapse(
[
html.P("FMU Check performs a static analysis of the FMU that validates the following aspects:"),
html.Ul(
[
html.Li("validity of the modelDescription.xml w.r.t. the XML schema"),
html.Li("uniqueness and validity of the variable names"),
html.Li("completeness and integrity of the Model Structure"),
html.Li("availability of the required start values"),
html.Li("combinations of causality and variability"),
html.Li("definition of units"),
]
),
html.P("It does not check the following aspects:"),
html.Ul(
[
html.Li("validity of the binaries"),
html.Li("validity of the sources"),
html.Li("any non-standard files inside the FMU"),
]
)
],
id="what-is-validated-paragraph"
),
dbc.InputGroup(
[
dbc.Input(id='fmu-input', placeholder="Select an FMU", disabled=True),
dcc.Upload(dbc.Button("Select",
color='primary',
style={'border-top-left-radius': 0, 'border-bottom-left-radius': 0}),
id='fmu-upload', accept='.fmu'),
]
),
]
),
dbc.Container(
[
dbc.Alert(
[
html.I(className="fas fa-thumbs-up me-3"),
"By uploading a file you agree that it is stored and processed on our server.",
],
id='disclaimer', color='info', className='mt-3', dismissable=True
)
],
id='model-info-container'
),
dbc.Container(
[
html.P(
[
"Fork ", html.A(f"FMU Check", href='https://github.com/modelica/fmu-check', className='link-secondary'), " on GitHub. ",
"Powered by ", html.A(f"FMPy {fmpy.__version__}", href='https://github.com/CATIA-Systems/FMPy', className='link-secondary'), "."
],
className='text-body-secondary mt-4'
)
],
)
])
@app.callback(
Output("what-is-validated-paragraph", "is_open"),
[Input("what-is-validated-link", "n_clicks")],
[State("what-is-validated-paragraph", "is_open")],
)
def toggle_collapse(n, is_open):
if n:
return not is_open
return is_open
@app.callback(
[
Output('model-info-container', 'children'),
Output("finished-store", "data"),
],
[Input('update-interval', 'n_intervals')],
[State('submitted-store', 'data')],
)
def retrieve_output(n_intervals, fmu_hash):
if fmu_hash is None:
raise PreventUpdate
pickle_file = os.path.join(workdir, fmu_hash + '.p')
if os.path.isfile(pickle_file):
with open(pickle_file, 'rb') as f:
components = pickle.load(f)
return components, fmu_hash
else:
return dbc.Container([dbc.Spinner(color='secondary')], className='mt-5 text-center'), None
@app.callback(
[
Output('fmu-input', 'value'),
Output("submitted-store", "data"),
],
[Input('fmu-upload', 'contents')],
[State('fmu-upload', 'filename')]
)
def submit(contents, filename):
if contents is None:
raise PreventUpdate
data = contents.encode('utf8').split(b';base64,')[1]
bytes = base64.decodebytes(data)
hash = hashlib.sha256(bytes).hexdigest()
fmu_filename = os.path.join(workdir, hash + '.fmu')
with open(fmu_filename, "wb") as fp:
fp.write(bytes)
subprocess.Popen(['python', 'process.py', fmu_filename])
return filename, hash
@app.callback(
Output("update-interval", "disabled"),
[
Input("submitted-store", "data"),
Input("finished-store", "data")
],
)
def disable_interval(submitted, finished):
return not submitted or finished == submitted
if __name__ == '__main__':
app.run_server(debug=True)