From 6f652d9b72c48134332bf2ef54795c5881560798 Mon Sep 17 00:00:00 2001 From: Serge Smertin Date: Fri, 27 Sep 2024 17:07:10 +0200 Subject: [PATCH 1/2] Added `save-dashboard` command --- .gitignore | 3 ++- labs.yml | 10 ++++++++++ src/databricks/labs/lsql/cli.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 174f3dc4..e13561ee 100644 --- a/.gitignore +++ b/.gitignore @@ -161,4 +161,5 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ -*.out \ No newline at end of file +*.out +/.databricks-login.json diff --git a/labs.yml b/labs.yml index b82ccc5d..e7a0a65b 100644 --- a/labs.yml +++ b/labs.yml @@ -34,3 +34,13 @@ commands: description: Publish the dashboard after creating by setting to `yes` or `y`. - name: open-browser description: Open the dashboard in the browser after creating by setting to `yes` or `y`. + + - name: save-dashboard + description: Save the dashboard to the given path. + flags: + - name: dashboard-id + description: The dashboard ID to save. + - name: path + description: The path to save the dashboard to. + - name: overwrite + description: Overwrite the existing file if it exists. diff --git a/src/databricks/labs/lsql/cli.py b/src/databricks/labs/lsql/cli.py index fc50fe56..6446b358 100644 --- a/src/databricks/labs/lsql/cli.py +++ b/src/databricks/labs/lsql/cli.py @@ -4,6 +4,8 @@ from databricks.labs.blueprint.cli import App from databricks.labs.blueprint.entrypoint import get_logger +from databricks.labs.blueprint.tui import Prompts +from databricks.labs.blueprint.paths import WorkspacePath from databricks.sdk import WorkspaceClient from databricks.labs.lsql.dashboards import DashboardMetadata, Dashboards, QueryTile @@ -43,6 +45,16 @@ def create_dashboard( print(sdk_dashboard.dashboard_id) +@lsql.command +def save_dashboard(w: WorkspaceClient, prompts: Prompts, remote_folder: str = '~'): + """Save a dashboard to a folder""" + workspace_path = WorkspacePath(w, remote_folder).expanduser() + dashboards = {_.name: _ for _ in workspace_path.glob('*.lvdash.json')} + selected = prompts.choice_from_dict('Select existing dashboard', dashboards) + print(selected) + + + @lsql.command(is_unauthenticated=True) def fmt(folder: Path = Path.cwd(), *, normalize_case: str = "true", exclude: Iterable[str] = ()): """Format SQL files in a folder""" From f04aca5ffbe1b5e33bfa0eb76013da8c562f4f23 Mon Sep 17 00:00:00 2001 From: Serge Smertin Date: Fri, 27 Sep 2024 17:32:05 +0200 Subject: [PATCH 2/2] .. --- labs.yml | 10 ++++------ src/databricks/labs/lsql/cli.py | 13 ++++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/labs.yml b/labs.yml index e7a0a65b..8768646d 100644 --- a/labs.yml +++ b/labs.yml @@ -38,9 +38,7 @@ commands: - name: save-dashboard description: Save the dashboard to the given path. flags: - - name: dashboard-id - description: The dashboard ID to save. - - name: path - description: The path to save the dashboard to. - - name: overwrite - description: Overwrite the existing file if it exists. + - name: remote-path + description: The path to fetch dashboard from. + - name: local-path + description: The path to save files to. diff --git a/src/databricks/labs/lsql/cli.py b/src/databricks/labs/lsql/cli.py index 6446b358..955cfe68 100644 --- a/src/databricks/labs/lsql/cli.py +++ b/src/databricks/labs/lsql/cli.py @@ -46,12 +46,15 @@ def create_dashboard( @lsql.command -def save_dashboard(w: WorkspaceClient, prompts: Prompts, remote_folder: str = '~'): +def save_dashboard(w: WorkspaceClient, prompts: Prompts, remote_path: str = '~', local_path: Path = Path.cwd()): """Save a dashboard to a folder""" - workspace_path = WorkspacePath(w, remote_folder).expanduser() - dashboards = {_.name: _ for _ in workspace_path.glob('*.lvdash.json')} - selected = prompts.choice_from_dict('Select existing dashboard', dashboards) - print(selected) + workspace_path = WorkspacePath(w, remote_path).expanduser() + if workspace_path.is_dir(): + dashboards = {_.name: _ for _ in workspace_path.glob('*.lvdash.json')} + workspace_path = prompts.choice_from_dict('Select existing dashboard', dashboards) + lakeview_dashboards = Dashboards(w) + remote_dashboard = lakeview_dashboards.get_dashboard(workspace_path) + lakeview_dashboards.save_to_folder(remote_dashboard, Path(local_path))