Rimjhim Mittal
commited on
Commit
β’
b11aca7
1
Parent(s):
97cf187
added two files
Browse files- app.py +111 -108
- {pages β other_files}/mvc +0 -0
- {pages β other_files}/old_MDF_Simulator.py +1 -1
- pages/YAML_editor_testing.py +173 -0
app.py
CHANGED
@@ -1,39 +1,12 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
import os
|
4 |
from modeci_mdf.mdf import Model, Graph, Node, Parameter, OutputPort
|
5 |
from modeci_mdf.utils import load_mdf_json, load_mdf, load_mdf_yaml
|
6 |
from modeci_mdf.execution_engine import EvaluableGraph
|
7 |
-
|
8 |
-
|
9 |
st.set_page_config(layout="wide")
|
10 |
st.title("π MDF Simulator")
|
11 |
-
|
12 |
-
|
13 |
-
theme = "default"
|
14 |
-
shortcuts = "vscode"
|
15 |
-
focus = False
|
16 |
-
wrap = True
|
17 |
-
editor_btns = [{
|
18 |
-
"name": "Run",
|
19 |
-
"feather": "Play",
|
20 |
-
"primary": True,
|
21 |
-
"hasText": True,
|
22 |
-
"showWithIcon": True,
|
23 |
-
"commands": ["submit"],
|
24 |
-
"style": {"bottom": "0.44rem", "right": "0.4rem"}
|
25 |
-
}]
|
26 |
-
|
27 |
-
def mdf_model_to_paramters_dictionary(param_inputs, mdf_model):
|
28 |
-
mod_graph = mdf_model.graphs[0]
|
29 |
-
nodes = mod_graph.nodes[0]
|
30 |
-
parameters = nodes.parameters
|
31 |
-
for param in parameters:
|
32 |
-
if isinstance(param.value, str) or param.value is None:
|
33 |
-
continue
|
34 |
-
st.session_state.param_inputs[param.id] = param.value
|
35 |
-
return st.session_state.param_inputs
|
36 |
-
|
37 |
def run_simulation(param_inputs, mdf_model):
|
38 |
mod_graph = mdf_model.graphs[0]
|
39 |
nodes = mod_graph.nodes[0]
|
@@ -61,101 +34,130 @@ def run_simulation(param_inputs, mdf_model):
|
|
61 |
chart_data.set_index('Time', inplace=True)
|
62 |
return chart_data
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
def update_view_tabs(mdf_model, param_inputs):
|
84 |
-
tab1, tab2, tab3, tab4 = st.tabs(["Simulation Results", "MDF Graph", "Json Output", "Yaml Editor"])
|
85 |
with tab1:
|
86 |
-
|
|
|
87 |
with tab2:
|
88 |
-
show_mdf_graph()
|
89 |
with tab3:
|
90 |
-
show_json_output()
|
91 |
-
with tab4:
|
92 |
-
changes_in_yaml_to_update_model()
|
93 |
|
94 |
-
def parameter_form_to_update_model_and_view(mdf_model, param_inputs):
|
95 |
-
mod_graph = mdf_model.graphs[0]
|
96 |
-
nodes = mod_graph.nodes[0]
|
97 |
-
parameters = nodes.parameters
|
98 |
form = st.form(key="parameter_form")
|
|
|
|
|
99 |
for i, param in enumerate(parameters):
|
100 |
if isinstance(param.value, str) or param.value is None:
|
101 |
continue
|
102 |
key = f"{param.id}_{i}"
|
103 |
if mdf_model.metadata:
|
104 |
-
|
105 |
else:
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
run_button = form.form_submit_button("Run Simulation")
|
111 |
if run_button:
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
st.
|
|
|
119 |
|
120 |
def upload_file_and_load_to_model():
|
121 |
-
|
122 |
-
|
123 |
-
filename = uploaded_file.name
|
124 |
-
with open(filename, "wb") as f:
|
125 |
-
f.write(uploaded_file.getbuffer())
|
126 |
-
mdf_model = load_mdf(filename)
|
127 |
-
st.session_state.original_mdf_model = mdf_model # Save the original model
|
128 |
-
st.session_state.mdf_model_yaml = mdf_model # Save the current model state
|
129 |
-
return mdf_model
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
st.
|
143 |
-
|
144 |
-
st.session_state.mdf_model_json = mdf_model.to_json()
|
145 |
-
st.session_state.update_button_clicked = True
|
146 |
-
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
|
150 |
def main():
|
151 |
-
|
152 |
-
|
153 |
if mdf_model:
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
if 'update_button_clicked' not in st.session_state:
|
158 |
-
st.session_state.update_button_clicked = False
|
159 |
param_inputs = {}
|
160 |
if mdf_model.metadata:
|
161 |
preferred_duration = float(mdf_model.metadata.get("preferred_duration", 10))
|
@@ -165,9 +167,10 @@ def main():
|
|
165 |
preferred_dt = 0.1
|
166 |
param_inputs["Simulation Duration (s)"] = preferred_duration
|
167 |
param_inputs["Time Step (s)"] = preferred_dt
|
168 |
-
|
169 |
-
|
170 |
-
update_view_tabs(mdf_model, param_inputs)
|
171 |
-
|
172 |
if __name__ == "__main__":
|
173 |
main()
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st, pandas as pd, os, io
|
|
|
|
|
2 |
from modeci_mdf.mdf import Model, Graph, Node, Parameter, OutputPort
|
3 |
from modeci_mdf.utils import load_mdf_json, load_mdf, load_mdf_yaml
|
4 |
from modeci_mdf.execution_engine import EvaluableGraph
|
5 |
+
import json
|
|
|
6 |
st.set_page_config(layout="wide")
|
7 |
st.title("π MDF Simulator")
|
8 |
+
import requests
|
9 |
+
# models: Purpose: To store the state of the model and update the model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def run_simulation(param_inputs, mdf_model):
|
11 |
mod_graph = mdf_model.graphs[0]
|
12 |
nodes = mod_graph.nodes[0]
|
|
|
34 |
chart_data.set_index('Time', inplace=True)
|
35 |
return chart_data
|
36 |
|
37 |
+
# views: Purpose: To display the state of the model and update the view
|
38 |
+
def show_simulation_results(chart_data):
|
39 |
+
st.line_chart(chart_data, use_container_width=True, height=400)
|
40 |
+
st.write("Output Values")
|
41 |
+
st.write(chart_data)
|
42 |
+
|
43 |
+
def show_mdf_graph(mdf_model):
|
44 |
+
st.subheader("MDF Graph")
|
45 |
+
mdf_model.to_graph_image(engine="dot", output_format="png", view_on_render=False, level=3, filename_root=mdf_model.id, only_warn_on_fail=(os.name == "nt"))
|
46 |
+
image_path = mdf_model.id + ".png"
|
47 |
+
st.image(image_path, caption="Model Graph Visualization")
|
48 |
+
|
49 |
+
def show_json_output(mdf_model):
|
50 |
+
st.subheader("JSON Output")
|
51 |
+
st.json(mdf_model.to_json())
|
52 |
+
|
53 |
+
def view_tabs(mdf_model, param_inputs): # view
|
54 |
+
tab1, tab2, tab3 = st.tabs(["Simulation Results", "MDF Graph", "Json Output"])
|
|
|
|
|
|
|
55 |
with tab1:
|
56 |
+
chart_data = run_simulation(param_inputs, mdf_model) # model
|
57 |
+
show_simulation_results(chart_data) # view
|
58 |
with tab2:
|
59 |
+
show_mdf_graph(mdf_model) # view
|
60 |
with tab3:
|
61 |
+
show_json_output(mdf_model) # view
|
|
|
|
|
62 |
|
63 |
+
def parameter_form_to_update_model_and_view(mdf_model, parameters, param_inputs, mod_graph, nodes):
|
|
|
|
|
|
|
64 |
form = st.form(key="parameter_form")
|
65 |
+
valid_inputs = True
|
66 |
+
|
67 |
for i, param in enumerate(parameters):
|
68 |
if isinstance(param.value, str) or param.value is None:
|
69 |
continue
|
70 |
key = f"{param.id}_{i}"
|
71 |
if mdf_model.metadata:
|
72 |
+
value = form.text_input(f"{param.metadata.get('description', param.id)} ({param.id})", value=str(param.value), key=key)
|
73 |
else:
|
74 |
+
value = form.text_input(f"{param.id}", value=str(param.value), key=key)
|
75 |
+
|
76 |
+
try:
|
77 |
+
param_inputs[param.id] = float(value)
|
78 |
+
except ValueError:
|
79 |
+
st.error(f"Invalid input for {param.id}. Please enter a valid number.")
|
80 |
+
valid_inputs = False
|
81 |
+
|
82 |
+
sim_duration = form.text_input("Simulation Duration (s)", value=str(param_inputs["Simulation Duration (s)"]), key="sim_duration")
|
83 |
+
time_step = form.text_input("Time Step (s)", value=str(param_inputs["Time Step (s)"]), key="time_step")
|
84 |
+
|
85 |
+
try:
|
86 |
+
param_inputs["Simulation Duration (s)"] = float(sim_duration)
|
87 |
+
except ValueError:
|
88 |
+
st.error("Invalid input for Simulation Duration. Please enter a valid number.")
|
89 |
+
valid_inputs = False
|
90 |
+
|
91 |
+
try:
|
92 |
+
param_inputs["Time Step (s)"] = float(time_step)
|
93 |
+
except ValueError:
|
94 |
+
st.error("Invalid input for Time Step. Please enter a valid number.")
|
95 |
+
valid_inputs = False
|
96 |
+
|
97 |
run_button = form.form_submit_button("Run Simulation")
|
98 |
if run_button:
|
99 |
+
if valid_inputs:
|
100 |
+
for param in parameters:
|
101 |
+
if param.id in param_inputs:
|
102 |
+
param.value = param_inputs[param.id]
|
103 |
+
view_tabs(mdf_model, param_inputs)
|
104 |
+
# else:
|
105 |
+
# st.error("Please correct the invalid inputs before running the simulation.")
|
106 |
+
|
107 |
|
108 |
def upload_file_and_load_to_model():
|
109 |
+
st.write("Choose how to load the model:")
|
110 |
+
load_option = st.radio("", ("Upload File", "GitHub URL"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
+
if load_option == "Upload File":
|
113 |
+
uploaded_file = st.file_uploader("Choose a JSON/YAML/BSON file", type=["json", "yaml", "bson"])
|
114 |
+
if uploaded_file is not None:
|
115 |
+
file_content = uploaded_file.getvalue()
|
116 |
+
file_extension = uploaded_file.name.split('.')[-1].lower()
|
117 |
+
return load_model_from_content(file_content, file_extension)
|
118 |
+
else:
|
119 |
+
st.write("sample_github_url = https://raw.githubusercontent.com/ModECI/MDF/development/examples/MDF/NewtonCoolingModel.json")
|
120 |
+
github_url = st.text_input("Enter GitHub raw file URL:", placeholder="Enter GitHub raw file URL")
|
121 |
+
if github_url:
|
122 |
+
try:
|
123 |
+
response = requests.get(github_url)
|
124 |
+
response.raise_for_status()
|
125 |
+
file_content = response.content
|
126 |
+
# print(file_content)
|
127 |
+
file_extension = github_url.split('.')[-1].lower()
|
128 |
+
return load_model_from_content(file_content, file_extension)
|
129 |
+
except requests.RequestException as e:
|
130 |
+
st.error(f"Error loading file from GitHub: {e}")
|
131 |
+
return None
|
132 |
|
133 |
+
return None
|
134 |
+
|
135 |
+
def load_model_from_content(file_content, file_extension):
|
136 |
+
try:
|
137 |
+
if file_extension == 'json':
|
138 |
+
json_data = json.loads(file_content)
|
139 |
+
mdf_model = Model.from_dict(json_data)
|
140 |
+
elif file_extension in ['yaml', 'yml']:
|
141 |
+
mdf_model = load_mdf_yaml(io.BytesIO(file_content))
|
142 |
+
else:
|
143 |
+
st.error("Unsupported file format. Please use JSON or YAML files.")
|
144 |
+
return None
|
|
|
|
|
|
|
145 |
|
146 |
+
st.session_state.original_mdf_model = mdf_model # Save the original model
|
147 |
+
st.session_state.mdf_model_yaml = mdf_model # Save the current model state
|
148 |
+
return mdf_model
|
149 |
+
except Exception as e:
|
150 |
+
st.error(f"Error loading model: {e}")
|
151 |
+
return None
|
152 |
|
153 |
|
154 |
def main():
|
155 |
+
st.write("Text box changed to input. Github URL is allowed. Added some warnings eg. on adding text in input fields. Now working on multiple parameters allow.")
|
156 |
+
mdf_model = upload_file_and_load_to_model() # controller
|
157 |
if mdf_model:
|
158 |
+
mod_graph = mdf_model.graphs[0]
|
159 |
+
nodes = mod_graph.nodes[0]
|
160 |
+
parameters = nodes.parameters
|
|
|
|
|
161 |
param_inputs = {}
|
162 |
if mdf_model.metadata:
|
163 |
preferred_duration = float(mdf_model.metadata.get("preferred_duration", 10))
|
|
|
167 |
preferred_dt = 0.1
|
168 |
param_inputs["Simulation Duration (s)"] = preferred_duration
|
169 |
param_inputs["Time Step (s)"] = preferred_dt
|
170 |
+
parameter_form_to_update_model_and_view(mdf_model, parameters, param_inputs, mod_graph, nodes)
|
171 |
+
|
|
|
|
|
172 |
if __name__ == "__main__":
|
173 |
main()
|
174 |
+
|
175 |
+
|
176 |
+
|
{pages β other_files}/mvc
RENAMED
File without changes
|
{pages β other_files}/old_MDF_Simulator.py
RENAMED
@@ -119,7 +119,7 @@ def main():
|
|
119 |
st.json(mdf_model.to_json())
|
120 |
else:
|
121 |
#Download the files to be uploaded from here:
|
122 |
-
files=["switched_rlc_circuit.json",
|
123 |
for filename in files:
|
124 |
with open(filename, 'rb') as f:
|
125 |
file_data = f.read()
|
|
|
119 |
st.json(mdf_model.to_json())
|
120 |
else:
|
121 |
#Download the files to be uploaded from here:
|
122 |
+
files=["switched_rlc_circuit.json",]
|
123 |
for filename in files:
|
124 |
with open(filename, 'rb') as f:
|
125 |
file_data = f.read()
|
pages/YAML_editor_testing.py
ADDED
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import os
|
4 |
+
from modeci_mdf.mdf import Model, Graph, Node, Parameter, OutputPort
|
5 |
+
from modeci_mdf.utils import load_mdf_json, load_mdf, load_mdf_yaml
|
6 |
+
from modeci_mdf.execution_engine import EvaluableGraph
|
7 |
+
from code_editor import code_editor
|
8 |
+
|
9 |
+
st.set_page_config(layout="wide")
|
10 |
+
st.title("π MDF Simulator")
|
11 |
+
|
12 |
+
height = [19, 22]
|
13 |
+
theme = "default"
|
14 |
+
shortcuts = "vscode"
|
15 |
+
focus = False
|
16 |
+
wrap = True
|
17 |
+
editor_btns = [{
|
18 |
+
"name": "Run",
|
19 |
+
"feather": "Play",
|
20 |
+
"primary": True,
|
21 |
+
"hasText": True,
|
22 |
+
"showWithIcon": True,
|
23 |
+
"commands": ["submit"],
|
24 |
+
"style": {"bottom": "0.44rem", "right": "0.4rem"}
|
25 |
+
}]
|
26 |
+
|
27 |
+
def mdf_model_to_paramters_dictionary(param_inputs, mdf_model):
|
28 |
+
mod_graph = mdf_model.graphs[0]
|
29 |
+
nodes = mod_graph.nodes[0]
|
30 |
+
parameters = nodes.parameters
|
31 |
+
for param in parameters:
|
32 |
+
if isinstance(param.value, str) or param.value is None:
|
33 |
+
continue
|
34 |
+
st.session_state.param_inputs[param.id] = param.value
|
35 |
+
return st.session_state.param_inputs
|
36 |
+
|
37 |
+
def run_simulation(param_inputs, mdf_model):
|
38 |
+
mod_graph = mdf_model.graphs[0]
|
39 |
+
nodes = mod_graph.nodes[0]
|
40 |
+
parameters = nodes.parameters
|
41 |
+
outputs = nodes.output_ports
|
42 |
+
eg = EvaluableGraph(mod_graph, verbose=False)
|
43 |
+
duration = param_inputs["Simulation Duration (s)"]
|
44 |
+
dt = param_inputs["Time Step (s)"]
|
45 |
+
t = 0
|
46 |
+
times = []
|
47 |
+
output_values = {op.value: [] for op in outputs}
|
48 |
+
while t <= duration:
|
49 |
+
times.append(t)
|
50 |
+
if t == 0:
|
51 |
+
eg.evaluate()
|
52 |
+
else:
|
53 |
+
eg.evaluate(time_increment=dt)
|
54 |
+
|
55 |
+
for param in output_values:
|
56 |
+
eval_param = eg.enodes[nodes.id].evaluable_parameters[param]
|
57 |
+
output_values[param].append(eval_param.curr_value)
|
58 |
+
t += dt
|
59 |
+
chart_data = pd.DataFrame(output_values)
|
60 |
+
chart_data['Time'] = times
|
61 |
+
chart_data.set_index('Time', inplace=True)
|
62 |
+
return chart_data
|
63 |
+
|
64 |
+
def show_simulation_results():
|
65 |
+
if 'chart_data' in st.session_state:
|
66 |
+
st.line_chart(st.session_state.chart_data, use_container_width=True, height=400)
|
67 |
+
st.write("Input Values")
|
68 |
+
st.write(st.session_state.param_inputs)
|
69 |
+
st.write("Output Values")
|
70 |
+
st.write(st.session_state.chart_data)
|
71 |
+
|
72 |
+
def show_mdf_graph():
|
73 |
+
if 'mdf_model_graph' in st.session_state:
|
74 |
+
st.subheader("MDF Graph")
|
75 |
+
image_path = st.session_state.mdf_model.id + ".png"
|
76 |
+
st.image(image_path, caption="Model Graph Visualization")
|
77 |
+
|
78 |
+
def show_json_output():
|
79 |
+
if 'mdf_model_json' in st.session_state:
|
80 |
+
st.subheader("JSON Output")
|
81 |
+
st.json(st.session_state.mdf_model_json)
|
82 |
+
|
83 |
+
def update_view_tabs(mdf_model, param_inputs):
|
84 |
+
tab1, tab2, tab3, tab4 = st.tabs(["Simulation Results", "MDF Graph", "Json Output", "Yaml Editor"])
|
85 |
+
with tab1:
|
86 |
+
show_simulation_results()
|
87 |
+
with tab2:
|
88 |
+
show_mdf_graph()
|
89 |
+
with tab3:
|
90 |
+
show_json_output()
|
91 |
+
with tab4:
|
92 |
+
changes_in_yaml_to_update_model()
|
93 |
+
|
94 |
+
def parameter_form_to_update_model_and_view(mdf_model, param_inputs):
|
95 |
+
mod_graph = mdf_model.graphs[0]
|
96 |
+
nodes = mod_graph.nodes[0]
|
97 |
+
parameters = nodes.parameters
|
98 |
+
form = st.form(key="parameter_form")
|
99 |
+
for i, param in enumerate(parameters):
|
100 |
+
if isinstance(param.value, str) or param.value is None:
|
101 |
+
continue
|
102 |
+
key = f"{param.id}_{i}"
|
103 |
+
if mdf_model.metadata:
|
104 |
+
param_inputs[param.id] = float(form.text_input(f"{param.metadata.get('description', param.id)} ({param.id})", value=param.value, key=key))
|
105 |
+
else:
|
106 |
+
param_inputs[param.id] = float(form.text_input(f"{param.id}", value=param.value, key=key))
|
107 |
+
param_inputs["Simulation Duration (s)"] = float(form.text_input("Simulation Duration (s)", value=st.session_state.param_inputs.get("Simulation Duration (s)", 10), key="sim_duration"))
|
108 |
+
param_inputs["Time Step (s)"] = float(form.text_input("Time Step (s)", value=st.session_state.param_inputs.get("Time Step (s)", 0.1), key="time_step"))
|
109 |
+
|
110 |
+
run_button = form.form_submit_button("Run Simulation")
|
111 |
+
if run_button:
|
112 |
+
st.session_state.run_button_clicked = True
|
113 |
+
for i in param_inputs:
|
114 |
+
st.session_state.param_inputs[i] = float(param_inputs[i])
|
115 |
+
st.session_state.chart_data = run_simulation(param_inputs, mdf_model)
|
116 |
+
st.session_state.mdf_model_graph = mdf_model.to_graph_image(engine="dot", output_format="png", view_on_render=False, level=3, filename_root=mdf_model.id, only_warn_on_fail=(os.name == "nt"))
|
117 |
+
st.session_state.mdf_model_json = mdf_model.to_json()
|
118 |
+
st.session_state.update_button_clicked = False
|
119 |
+
|
120 |
+
def upload_file_and_load_to_model():
|
121 |
+
uploaded_file = st.file_uploader("Choose a JSON/YAML/BSON file", type=["json", "yaml", "bson"])
|
122 |
+
if uploaded_file is not None:
|
123 |
+
filename = uploaded_file.name
|
124 |
+
with open(filename, "wb") as f:
|
125 |
+
f.write(uploaded_file.getbuffer())
|
126 |
+
mdf_model = load_mdf(filename)
|
127 |
+
st.session_state.original_mdf_model = mdf_model # Save the original model
|
128 |
+
st.session_state.mdf_model_yaml = mdf_model # Save the current model state
|
129 |
+
return mdf_model
|
130 |
+
|
131 |
+
|
132 |
+
def changes_in_yaml_to_update_model():
|
133 |
+
response = code_editor(st.session_state.mdf_model_yaml.to_yaml(), height=height, lang="yaml", theme=theme, shortcuts=shortcuts, focus=focus, buttons=editor_btns, options={"wrap": wrap})
|
134 |
+
st.write("First, make changes in the YAML code above, press Ctrl + Enter (or Run button inside code editor) and then click on the 'Update Model' button below to update the model. Sometimes you have to follow these steps twice. Currently, the plot, mdf_graph and json output get updtes, but parameter input form needs to be worked on")
|
135 |
+
if len(response['id']) != 0 and (response['type'] == "selection" or response['type'] == "submit"):
|
136 |
+
code_text = response['text']
|
137 |
+
update_button = st.button("Update Model")
|
138 |
+
if update_button:
|
139 |
+
mdf_model = Model.from_yaml(code_text)
|
140 |
+
mdf_model_to_paramters_dictionary(st.session_state.param_inputs, mdf_model)
|
141 |
+
st.session_state.mdf_model_yaml = mdf_model
|
142 |
+
st.session_state.chart_data = run_simulation(st.session_state.param_inputs, mdf_model)
|
143 |
+
st.session_state.mdf_model_graph = mdf_model.to_graph_image(engine="dot", output_format="png", view_on_render=False, level=3, filename_root=mdf_model.id, only_warn_on_fail=(os.name == "nt"))
|
144 |
+
st.session_state.mdf_model_json = mdf_model.to_json()
|
145 |
+
st.session_state.update_button_clicked = True
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
|
150 |
+
def main():
|
151 |
+
mdf_model = upload_file_and_load_to_model()
|
152 |
+
st.write("Working on making YAML editor.")
|
153 |
+
if mdf_model:
|
154 |
+
st.session_state.mdf_model = mdf_model
|
155 |
+
if 'run_button_clicked' not in st.session_state:
|
156 |
+
st.session_state.run_button_clicked = False
|
157 |
+
if 'update_button_clicked' not in st.session_state:
|
158 |
+
st.session_state.update_button_clicked = False
|
159 |
+
param_inputs = {}
|
160 |
+
if mdf_model.metadata:
|
161 |
+
preferred_duration = float(mdf_model.metadata.get("preferred_duration", 10))
|
162 |
+
preferred_dt = float(mdf_model.metadata.get("preferred_dt", 0.1))
|
163 |
+
else:
|
164 |
+
preferred_duration = 100
|
165 |
+
preferred_dt = 0.1
|
166 |
+
param_inputs["Simulation Duration (s)"] = preferred_duration
|
167 |
+
param_inputs["Time Step (s)"] = preferred_dt
|
168 |
+
st.session_state.param_inputs = param_inputs
|
169 |
+
parameter_form_to_update_model_and_view(st.session_state.mdf_model, st.session_state.param_inputs)
|
170 |
+
update_view_tabs(mdf_model, param_inputs)
|
171 |
+
|
172 |
+
if __name__ == "__main__":
|
173 |
+
main()
|