From 9d225540e79ecd181e561128df811c48bfbc133b Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Fri, 20 Sep 2024 11:07:53 -0700 Subject: [PATCH 1/2] example store usage --- plugins/panel-examples/__init__.py | 46 +++++++++++++++++++++++++++++ plugins/panel-examples/fiftyone.yml | 1 + 2 files changed, 47 insertions(+) diff --git a/plugins/panel-examples/__init__.py b/plugins/panel-examples/__init__.py index 4b495c5..f83a70e 100644 --- a/plugins/panel-examples/__init__.py +++ b/plugins/panel-examples/__init__.py @@ -782,6 +782,51 @@ def render(self, ctx): ) +class StoreExample(foo.Panel): + @property + def config(self): + return foo.PanelConfig( + name="example_store", + label="Examples: Store", + ) + + def get_store(self, ctx): + store_name = "markdown_docs" + store = ctx.create_store(store_name) + return store + + def on_click_save(self, ctx): + store = self.get_store(ctx) + doc_name = ctx.panel.state.doc_name + store.set(doc_name, ctx.panel.state.src) + ctx.ops.notify(f"Saved {doc_name}") + + def on_click_clear(self, ctx): + ctx.panel.state.src = "" + ctx.panel.state.doc_name = "" + + def on_click_load(self, ctx): + store = self.get_store(ctx) + doc_name = ctx.panel.state.doc_name + src = store.get(doc_name) + print(src) + ctx.panel.state.src = src + + def on_change(self, ctx): + print(ctx.panel.state.src) + + def render(self, ctx): + panel = types.Object() + panel.str("doc_name", label="Document Name") + panel.btn("load_btn", label="Load", on_click=self.on_click_load) + panel.str( + "src", label="Markdown", view=types.CodeView(language="markdown") + ) + panel.btn("btn", label="Save", on_click=self.on_click_save) + panel.btn("clear", label="Clear", on_click=self.on_click_clear) + return types.Property(panel) + + def register(p): p.register(CounterExample) p.register(PlotExample) @@ -794,3 +839,4 @@ def register(p): p.register(InteractivePlotExample) p.register(DropdownMenuExample) p.register(WalkthroughExample) + p.register(StoreExample) diff --git a/plugins/panel-examples/fiftyone.yml b/plugins/panel-examples/fiftyone.yml index 2f1a9ac..3bf4435 100644 --- a/plugins/panel-examples/fiftyone.yml +++ b/plugins/panel-examples/fiftyone.yml @@ -17,3 +17,4 @@ panels: - example_interactive_plot - example_dropdown_menu - example_walkthrough + - example_store From 9e867c05cb06340b77a3bded1cc7948fe69d5020 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Mon, 23 Sep 2024 06:31:33 -0700 Subject: [PATCH 2/2] exec store example improvements --- plugins/panel-examples/__init__.py | 31 +++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/plugins/panel-examples/__init__.py b/plugins/panel-examples/__init__.py index f83a70e..46e3b61 100644 --- a/plugins/panel-examples/__init__.py +++ b/plugins/panel-examples/__init__.py @@ -795,21 +795,43 @@ def get_store(self, ctx): store = ctx.create_store(store_name) return store + def load_keys_list(self, ctx): + print("loading keys") + store = self.get_store(ctx) + keys = store.list_keys() + print(keys) + ctx.panel.state.keys = keys + + def load_stores_list(self, ctx): + store = self.get_store(ctx) + stores = store.list_all_stores() + ctx.panel.state.stores = stores + + def on_load(self, ctx): + self.load_keys_list(ctx) + self.load_stores_list(ctx) + def on_click_save(self, ctx): store = self.get_store(ctx) doc_name = ctx.panel.state.doc_name - store.set(doc_name, ctx.panel.state.src) + store.set(doc_name, ctx.panel.state.src, ttl=ctx.panel.state.ttl) ctx.ops.notify(f"Saved {doc_name}") def on_click_clear(self, ctx): + doc_name = ctx.panel.state.doc_name ctx.panel.state.src = "" ctx.panel.state.doc_name = "" + ctx.panel.state.ttl = None + # delete the key + store = self.get_store(ctx) + store.delete(doc_name) + self.load_keys_list(ctx) def on_click_load(self, ctx): + self.on_load(ctx) store = self.get_store(ctx) doc_name = ctx.panel.state.doc_name src = store.get(doc_name) - print(src) ctx.panel.state.src = src def on_change(self, ctx): @@ -818,12 +840,15 @@ def on_change(self, ctx): def render(self, ctx): panel = types.Object() panel.str("doc_name", label="Document Name") + panel.int("ttl", label="Time to Live", default=None) panel.btn("load_btn", label="Load", on_click=self.on_click_load) panel.str( "src", label="Markdown", view=types.CodeView(language="markdown") ) panel.btn("btn", label="Save", on_click=self.on_click_save) - panel.btn("clear", label="Clear", on_click=self.on_click_clear) + panel.btn("clear", label="Delete", on_click=self.on_click_clear) + panel.list("keys", types.String(), label="Keys") + panel.list("stores", types.String(), label="Stores") return types.Property(panel)