Skip to content

Commit ab14e7c

Browse files
committed
Schema type Conversion and state refresh
1 parent d85641d commit ab14e7c

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/fourc_webviewer/fourc_webserver.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def __init__(
7474
# create temporary directory
7575
self._server_vars["temp_dir_object"] = tempfile.TemporaryDirectory()
7676

77+
# Register on_field_blur function, which is called when the user leaves a field
78+
self.server.controller.on_field_blur = self.on_field_blur
79+
7780
# initialize state variables for the different modes and
7881
# statuses of the client (e.g. view mode versus edit mode,
7982
# read-in and export status, ...)
@@ -1146,23 +1149,38 @@ def parse_validation_error_text(self, text):
11461149
@change("general_sections")
11471150
def on_sections_change(self, general_sections, **kwargs):
11481151
"""Reaction to change of state.general_sections."""
1152+
11491153
self.sync_server_vars_from_state()
11501154
try:
11511155
fourcinput = FourCInput(self._server_vars["fourc_yaml_content"])
1156+
# print(CONFIG)
1157+
1158+
def get_by_path(dct, path):
1159+
"""Retrieve the value at the nested path from dct.
1160+
1161+
Raises KeyError if any key is missing.
1162+
"""
1163+
# print(f"get_by_path: {path}")
1164+
current = dct
1165+
for key in path:
1166+
current = current[key]
1167+
return current
11521168

1153-
def dict_leaves_to_number(value):
1169+
def dict_leaves_to_number_if_schema(value, schema_path=[]):
11541170
"""Convert all leaves of a dict to numbers if possible."""
11551171
if isinstance(value, dict):
11561172
for k, v in value.items():
1157-
value[k] = dict_leaves_to_number(v)
1173+
value[k] = dict_leaves_to_number_if_schema(
1174+
v, schema_path + ["properties", k]
1175+
)
11581176
return value
1159-
if isinstance(value, str):
1177+
if isinstance(value, str) and get_by_path(
1178+
CONFIG["json_schema"], schema_path + ["type"]
1179+
) in ["number", "integer"]:
11601180
return smart_string2number_cast(value)
11611181
return value
11621182

1163-
for section_name in fourcinput._sections:
1164-
if section_name[0:5] != "FUNCT":
1165-
dict_leaves_to_number(fourcinput._sections[section_name])
1183+
dict_leaves_to_number_if_schema(fourcinput._sections)
11661184

11671185
fourcinput.validate()
11681186
self.state.input_error_dict = {}
@@ -1172,6 +1190,12 @@ def dict_leaves_to_number(value):
11721190
) # exc.args[0] is the error message
11731191
return False
11741192

1193+
def on_field_blur(self):
1194+
"""Reaction to user leaving the field."""
1195+
# also gets called when a new file is loaded
1196+
# basically just sets the state based on server_vars
1197+
self.init_general_sections_state_and_server_vars()
1198+
11751199
""" --- Other helper functions"""
11761200

11771201
def convert_string2num_all_sections(self):

src/fourc_webviewer/gui_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,9 @@ def _functions_panel(server):
437437
)
438438

439439

440-
def _prop_value_table():
440+
def _prop_value_table(server):
441441
"""Table (property - value) layout (for general sections)."""
442+
442443
with vuetify.VTable(
443444
v_if=(
444445
"section_names[selected_main_section_name]['content_mode'] == all_content_modes['general_section']",
@@ -501,6 +502,7 @@ def _prop_value_table():
501502
"|| json_schema['properties']?.[selected_section_name]?.['properties']?.[item_key]?.['type'] == 'integer')"
502503
"&& !json_schema['properties']?.[selected_section_name]?.['properties']?.[item_key]?.['enum']"
503504
),
505+
blur=server.controller.on_field_blur,
504506
update_modelValue="flushState('general_sections')", # this is required in order to flush the state changes correctly to the server, as our passed on v-model is a nested variable
505507
classes="w-80 pb-1",
506508
dense=True,
@@ -1178,7 +1180,7 @@ def create_gui(server, render_window):
11781180

11791181
# Further elements with conditional rendering (see above)
11801182
_sections_dropdown()
1181-
_prop_value_table()
1183+
_prop_value_table(server)
11821184
_materials_panel()
11831185
_functions_panel(server)
11841186
_design_conditions_panel()

0 commit comments

Comments
 (0)