Skip to content

Commit 7f7ae94

Browse files
authored
Merge pull request #15 from gridfm/fix/interactive
add output cell
2 parents da8578b + 85ddeac commit 7f7ae94

File tree

1 file changed

+67
-27
lines changed

1 file changed

+67
-27
lines changed

gridfm_datakit/interactive.py

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from importlib import resources
88
from ipyfilechooser import FileChooser
99

10+
# Run the generation function directly
11+
from gridfm_datakit.generate import generate_power_flow_data_distributed
12+
1013

1114
def get_available_load_profiles():
1215
"""Get list of available aggregated load profiles."""
@@ -102,6 +105,17 @@ def interactive_interface():
102105
# Get available load profiles
103106
available_profiles = get_available_load_profiles()
104107

108+
# Dedicated output box
109+
output_box = widgets.Output(
110+
layout=widgets.Layout(
111+
border="1px solid #bbb",
112+
padding="10px",
113+
margin="10px 0",
114+
height="300px",
115+
overflow="auto",
116+
),
117+
)
118+
105119
# Network Configuration
106120
global \
107121
network_source, \
@@ -303,7 +317,7 @@ def update_network_visibility(*args):
303317
)
304318

305319
sigma = widgets.FloatSlider(
306-
value=0.05,
320+
value=0.2,
307321
min=0.0,
308322
max=0.3,
309323
step=0.01,
@@ -386,8 +400,8 @@ def update_network_visibility(*args):
386400
min=1,
387401
max=20,
388402
step=1,
389-
description="Topology Variants per load scenario:",
390-
style={"description_width": "150px"},
403+
description="Top. Variants per load scenario:",
404+
style={"description_width": "200px"},
391405
layout=widgets.Layout(width="550px"),
392406
readout_format="d",
393407
)
@@ -417,7 +431,7 @@ def update_network_visibility(*args):
417431
layout=widgets.Layout(width="550px", height="120px"),
418432
)
419433

420-
# Generation Perturbation Configuration
434+
# Generation Configuration
421435

422436
global gen_perturbation_type, gen_sigma
423437

@@ -493,7 +507,7 @@ def update_network_visibility(*args):
493507
)
494508

495509
large_chunk_size = widgets.IntSlider(
496-
value=50,
510+
value=200,
497511
min=10,
498512
max=500,
499513
step=10,
@@ -508,15 +522,15 @@ def update_network_visibility(*args):
508522
no_stats = widgets.Checkbox(
509523
value=False,
510524
description="Disable statistical calculations (faster)",
511-
style={"description_width": "200px"},
512-
layout=widgets.Layout(width="350px"),
525+
style={"description_width": "100px"},
526+
layout=widgets.Layout(width="500px"),
513527
)
514528

515529
overwrite = widgets.Checkbox(
516530
value=True,
517531
description="Overwrite existing files (vs. append)",
518-
style={"description_width": "200px"},
519-
layout=widgets.Layout(width="350px"),
532+
style={"description_width": "100px"},
533+
layout=widgets.Layout(width="500px"),
520534
)
521535

522536
# Set mode to "pf"
@@ -695,25 +709,48 @@ def update_advanced_load_scaling(*args):
695709

696710
# Button to create YAML config only
697711
def save_config_only(b):
698-
config = create_config()
699-
config_path = Path(config_filename.value)
700-
with open(config_path, "w") as f:
701-
yaml.dump(config, f, default_flow_style=False)
702-
print(f"YAML configuration saved to {config_path}")
712+
with output_box:
713+
output_box.clear_output()
714+
try:
715+
config = create_config()
716+
config_path = Path(config_filename.value)
717+
with open(config_path, "w") as f:
718+
yaml.dump(config, f, default_flow_style=False)
719+
print(f"YAML configuration saved to {config_path.resolve()}")
720+
except Exception as e:
721+
print("Error while saving configuration:")
722+
print(e)
703723

704724
def save_and_run_config(b):
705725
"""Save configuration and run the generation script."""
706-
config = create_config()
707-
708-
# Save config to file
709-
config_path = Path(config_filename.value)
710-
with open(config_path, "w") as f:
711-
yaml.dump(config, f, default_flow_style=False)
712-
713-
# Run the generation function directly
714-
from gridfm_datakit.generate import generate_power_flow_data_distributed
715-
716-
generate_power_flow_data_distributed(str(config_path))
726+
# Grey/disable both buttons immediately
727+
run_button.description = "Running…"
728+
run_button.disabled = True
729+
save_config_button.disabled = True
730+
731+
with output_box:
732+
output_box.clear_output()
733+
try:
734+
config = create_config()
735+
736+
# Save config to file
737+
config_path = Path(config_filename.value)
738+
with open(config_path, "w") as f:
739+
yaml.dump(config, f, default_flow_style=False)
740+
print(f"\nConfiguration written to: {config_path.resolve()}\n")
741+
742+
# Run the generator (stdout/stderr will appear here)
743+
print("Starting generation...")
744+
generate_power_flow_data_distributed(str(config_path))
745+
print("\nGeneration completed successfully.")
746+
except Exception as e:
747+
print("Error during generation:")
748+
print(e)
749+
finally:
750+
# Re-enable buttons and restore label regardless of success/failure
751+
run_button.description = "Generate and Run Configuration"
752+
run_button.disabled = False
753+
save_config_button.disabled = False
717754

718755
save_config_button = widgets.Button(
719756
description="Create YAML Config",
@@ -739,5 +776,8 @@ def save_and_run_config(b):
739776
display(admittance_box)
740777
display(execution_box)
741778
display(config_filename)
742-
display(save_config_button)
743-
display(run_button)
779+
display(save_config_button, run_button)
780+
781+
# Display the dedicated output area last so it is close to the buttons
782+
display(widgets.HTML("<b>Output</b>"))
783+
display(output_box)

0 commit comments

Comments
 (0)