Skip to content

Commit

Permalink
Merge pull request #17 from jobar8/jobar8/issue12
Browse files Browse the repository at this point in the history
Add save button
  • Loading branch information
jobar8 authored Oct 18, 2024
2 parents 91c2687 + e38e750 commit 16ab612
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
37 changes: 18 additions & 19 deletions src/attractor_explorer/attractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,19 @@ class ParameterSets(param.Parameterized):
Allows selection from sets of pre-defined parameters saved in YAML.
"""

data_folder: Path = Path(__file__).parent / 'data'
input_examples_filename = param.Filename('attractors.yml', search_paths=[data_folder.as_posix()])
output_examples_filename = param.Filename(
'saved_attractors.yml', check_exists=False, search_paths=[data_folder.as_posix()]
)
data_folder = param.Foldername('data', search_paths=[Path(__file__).parent.as_posix()])
input_examples_filename = param.String('attractors.yml')
output_examples_filename = param.String('saved_attractors.yml', precedence=1.0)

current = param.Callable(lambda: None, precedence=-1)
attractors: dict[str, Attractor]

load = param.Action(lambda x: x._load())
randomize = param.Action(lambda x: x._randomize())
sort = param.Action(lambda x: x._sort())
remember_this_one = param.Action(lambda x: x._remember())
# save = param.Action(lambda x: x._save(), precedence=0.8)
example = param.Selector(objects=[[]], precedence=1, instantiate=False)
random = param.Action(lambda x: x._randomize())
# sort = param.Action(lambda x: x._sort())
remember_this_one = param.Action(lambda x: x._remember(), precedence=0.81)
save = param.Action(lambda x: x._save(), precedence=0.99)
example = param.Selector(objects=[[]], precedence=0.8, instantiate=False)

def __init__(self, **params):
super().__init__(**params)
Expand All @@ -235,18 +234,18 @@ def __init__(self, **params):
pass

def _load(self):
with Path(self.input_examples_filename).open('r') as f: # type: ignore
with (Path(self.data_folder) / self.input_examples_filename).open('r') as f: # type: ignore
vals = yaml.safe_load(f)
if len(vals) > 0:
self.param.example.objects[:] = vals
self.param.example.objects = vals
self.example = vals[0]

# def _save(self):
# if self.output_examples_filename == self.param.input_examples_filename.default:
# msg = 'Cannot override the default attractors file.'
# raise FileExistsError(msg)
# with Path(self.data_folder / self.output_examples_filename).open('w') as f:
# yaml.dump(self.param.example.objects, f)
def _save(self):
if self.output_examples_filename == self.param.input_examples_filename.default:
msg = 'Cannot override the default attractors file.'
raise FileExistsError(msg)
with (Path(self.data_folder) / self.output_examples_filename).open('w') as f: # type: ignore
yaml.dump(list(self.param.example.objects), f)

def __call__(self):
return self.example
Expand All @@ -260,7 +259,7 @@ def _sort(self):
self.example = self.param.example.objects[0]

def _add_item(self, item):
self.param.example.objects += [item]
self.param.example.objects.append(item)
self.example = item

def _remember(self):
Expand Down
10 changes: 5 additions & 5 deletions src/attractor_explorer/attractors_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AttractorsExplorer(pn.viewable.Viewer):
options=colormaps,
ncols=1,
swatch_width=100,
margin=(25, 0, 200, 0),
margin=(25, 10, 25, 10),
stylesheets=[CMAP_CSS_HACK],
)

Expand Down Expand Up @@ -124,29 +124,29 @@ def update_attractor(self):
expand=True,
show_name=False,
),
ats.interpolation,
ats.colormap,
ats.interpolation,
pn.layout.Card(
pn.Param(
ats.param_sets.param,
widgets={
'input_examples_filename': {
'widget_type': pn.widgets.TextInput,
'stylesheets': ['.bk-input-group > label {background-color: black}'],
'name': '',
# 'name': '',
},
'example': {
'stylesheets': ['bk-panel-models-widgets-CustomSelect {background: #2b3035; color: black}'],
'name': '',
},
},
show_name=False,
parameters=['input_examples_filename', 'load', 'example', 'remember_this_one'],
),
title='Load and Save',
collapsed=True,
header_color='white',
header_background='#2c71b4',
margin=(40, 10, 100, 10),
),
],
main=[
Expand All @@ -161,4 +161,4 @@ def update_attractor(self):
header_background='teal',
theme='dark',
theme_toggle=False,
).servable('Attractor Explorer')
).servable('Attractor Explorer') # .show(port=5006, open=False)

0 comments on commit 16ab612

Please sign in to comment.