Skip to content

Commit 0f519ad

Browse files
committed
show yaml parsing errors as annotations
1 parent f12a86e commit 0f519ad

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

waveform_editor/waveform_editor_gui.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,21 @@
2222
height=1200,
2323
theme="tomorrow",
2424
language="yaml",
25-
annotations=[
26-
dict(row=1, column=0, text="an error", type="error"),
27-
dict(row=2, column=0, text="a warning", type="warning"),
28-
],
2925
)
3026

3127
yaml_parser = YamlParser()
3228

33-
initial_yaml_str = code_editor.value
34-
yaml_parser.parse_waveforms_from_string(initial_yaml_str)
35-
3629

3730
def update_plot(value):
38-
yaml_parser.tendencies = []
3931
yaml_parser.parse_waveforms_from_string(value)
4032

41-
return yaml_parser.plot_tendencies()
33+
code_editor.annotations = yaml_parser.annotations
34+
code_editor.param.trigger("annotations")
35+
36+
if yaml_parser.waveform is None:
37+
return yaml_parser.plot_empty()
38+
else:
39+
return yaml_parser.plot_tendencies()
4240

4341

4442
hv_dynamic_map = hv.DynamicMap(pn.bind(update_plot, value=code_editor.param.value))

waveform_editor/yaml_parser.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
import holoviews as hv
2+
import param
23
import yaml
34

45
from waveform_editor.waveform import Waveform
56

67

7-
class YamlParser:
8+
class YamlParser(param.Parameterized):
9+
waveform = param.ClassSelector(
10+
class_=Waveform,
11+
default=None,
12+
doc="Waveform that contains the tendencies.",
13+
)
14+
15+
def __init__(self):
16+
self.annotations = []
17+
818
def parse_waveforms_from_file(self, file_path):
919
"""Loads a YAML file from a file path and stores its tendencies into a list.
1020
@@ -22,9 +32,45 @@ def parse_waveforms_from_string(self, yaml_str):
2232
Args:
2333
yaml_str: YAML content as a string.
2434
"""
25-
waveform_yaml = yaml.load(yaml_str, yaml.SafeLoader)
26-
waveform = waveform_yaml.get("waveform", [])
27-
self.waveform = Waveform(waveform)
35+
try:
36+
waveform_yaml = yaml.load(yaml_str, yaml.SafeLoader)
37+
waveform = waveform_yaml.get("waveform", [])
38+
self.waveform = Waveform(waveform)
39+
self.annotations.clear()
40+
except yaml.YAMLError as e:
41+
self.annotations = self.yaml_error_to_annotation(e)
42+
self.waveform = None
43+
44+
def yaml_error_to_annotation(self, error):
45+
annotations = []
46+
47+
print(f"Encountered the following YAMLError:\n {error}")
48+
if hasattr(error, "problem_mark"):
49+
line = error.problem_mark.line
50+
column = error.problem_mark.column
51+
message = error.problem
52+
53+
annotations.append(
54+
{
55+
"row": line,
56+
"column": column,
57+
"text": f"Error: {message}",
58+
"type": "error",
59+
}
60+
)
61+
else:
62+
annotations.append(
63+
{"row": 0, "column": 0, "text": "Unknown YAML error", "type": "error"}
64+
)
65+
66+
return annotations
67+
68+
def plot_empty(self):
69+
overlay = hv.Overlay()
70+
71+
# Force re-render by plotting an empty plot
72+
overlay = overlay * hv.Curve([])
73+
return overlay.opts(title="Waveform", width=800, height=400)
2874

2975
def plot_tendencies(self, plot_time_points=False):
3076
"""

0 commit comments

Comments
 (0)