From 1e35f069b0f33621a841464f9f0ed711126d0896 Mon Sep 17 00:00:00 2001 From: LUIS NOVO Date: Wed, 13 Nov 2024 17:33:38 -0300 Subject: [PATCH] add option to save insight as note --- open_notebook/domain/notebook.py | 10 ++++++++++ pages/components/source_panel.py | 11 +++++++++-- pages/stream_app/source.py | 6 +++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/open_notebook/domain/notebook.py b/open_notebook/domain/notebook.py index 5ef3f14..0b9db84 100644 --- a/open_notebook/domain/notebook.py +++ b/open_notebook/domain/notebook.py @@ -130,6 +130,16 @@ class SourceInsight(ObjectModel): logger.exception(e) raise DatabaseOperationError(e) + def save_as_note(self, notebook_id: str = None) -> Any: + note = Note( + title=f"{self.insight_type} from source {self.source.title}", + content=self.content, + ) + note.save() + if notebook_id: + note.add_to_notebook(notebook_id) + return note + class Source(ObjectModel): table_name: ClassVar[str] = "source" diff --git a/pages/components/source_panel.py b/pages/components/source_panel.py index eac9e5f..57aeb6b 100644 --- a/pages/components/source_panel.py +++ b/pages/components/source_panel.py @@ -8,7 +8,7 @@ from open_notebook.utils import surreal_clean from pages.stream_app.utils import run_patterns -def source_panel(source_id: str, modal=False): +def source_panel(source_id: str, notebook_id=None, modal=False): source: Source = Source.get(source_id) if not source: raise ValueError(f"Source not found: {source_id}") @@ -36,11 +36,18 @@ def source_panel(source_id: str, modal=False): for insight in source.insights: with st.expander(f"**{insight.insight_type}**"): st.markdown(insight.content) - if st.button( + x1, x2 = st.columns(2) + if x1.button( "Delete", type="primary", key=f"delete_insight_{insight.id}" ): insight.delete() st.rerun(scope="fragment" if modal else "app") + if notebook_id: + if x2.button( + "Save as Note", icon="📝", key=f"save_note_{insight.id}" + ): + insight.save_as_note(notebook_id) + st.toast("Saved as Note. Refresh the Notebook to see it.") with c2: transformations = Transformation.get_all() diff --git a/pages/stream_app/source.py b/pages/stream_app/source.py index 13978b4..390ff2e 100644 --- a/pages/stream_app/source.py +++ b/pages/stream_app/source.py @@ -17,8 +17,8 @@ from .consts import source_context_icons @st.dialog("Source", width="large") -def source_panel_dialog(source_id): - source_panel(source_id, modal=True) +def source_panel_dialog(source_id, notebook_id=None): + source_panel(source_id, notebook_id=notebook_id, modal=True) @st.dialog("Add a Source", width="large") @@ -121,7 +121,7 @@ def source_card(source, notebook_id): f"Updated: {naturaltime(source.updated)}, **{len(source.insights)}** insights" ) if st.button("Expand", icon="📝", key=source.id): - source_panel_dialog(source.id) + source_panel_dialog(source.id, notebook_id) st.session_state[notebook_id]["context_config"][source.id] = context_state