- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 08:31 AM
I'm exporting dashboard objects from an existing workspace to new workspace but after importing ,the underlying dashboards data is not coming to new workspace. I'm using the below code. Can anyone help
import os
import requests
import json
import logging
# Set up logging
log_file = 'import_dashboards_log.log'
logging.basicConfig(filename=log_file, level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
# Target Databricks workspace URL and token (hardcoded)
target_workspace_url = 'https://.azuredatabricks.net'
target_workspace_token = 'dapib2e-3'
def create_folder(workspace_url, token, folder_path):
"""Create a folder in the Databricks workspace if it doesn't exist."""
url = f'{workspace_url}/api/2.0/workspace/mkdirs'
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
payload = {"path": folder_path}
response = requests.post(url, headers=headers, data=json.dumps(payload))
if response.status_code == 200 or response.status_code == 400: # 400 means folder already exists
logging.info(f"Folder created or already exists: {folder_path}")
print(f"Folder created or already exists: {folder_path}")
else:
logging.error(f"Failed to create folder {folder_path}. Error: {response.content}")
print(f"Failed to create folder {folder_path}. Error: {response.content}")
def import_dashboard(workspace_url, token, file_path, folder_path):
"""Import a dashboard JSON file into the new workspace."""
with open(file_path, 'r') as f:
dashboard_data = json.load(f)
# Prepare the import payload based on the provided JSON sample
import_dashboards = {
"name": dashboard_data.get('name'),
"parent": folder_path,
"tags": dashboard_data.get('tags', []),
"options": dashboard_data.get('options'),
"widgets": dashboard_data.get('widgets'),
"user": dashboard_data.get('user')
}
url = f'{workspace_url}/api/2.0/preview/sql/dashboards'
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = requests.post(url, headers=headers, data=json.dumps(import_dashboards))
if response.status_code == 200:
logging.info(f"Imported dashboard: {file_path}")
print(f"Imported dashboard: {file_path}")
else:
logging.error(f"Failed to import dashboard {file_path}. Error: {response.content}")
print(f"Failed to import dashboard {file_path}. Error: {response.content}")
def main():
"""Main function to import dashboards into the new workspace."""
exported_dir = 'exported_dashboards' # Directory where exported dashboards are saved
folder_path = "/Workspace/folders/new_dashboard_folder" # Path to the folder in the new workspace
print("\033[33mImporting dashboards...\033[0m") # Yellow color
logging.info("Starting to import dashboards.")
# Create folder in the workspace
create_folder(target_workspace_url, target_workspace_token, folder_path)
for filename in os.listdir(exported_dir):
if filename.endswith('.json'):
file_path = os.path.join(exported_dir, filename)
try:
import_dashboard(target_workspace_url, target_workspace_token, file_path, folder_path)
except Exception as e:
logging.error(f"An error occurred while importing {file_path}: {e}")
print(f"An error occurred while importing {file_path}: {e}")
print("\033[32mDashboards import process completed\033[0m") # Green color
logging.info("Dashboards import process completed.")
if __name__ == "__main__":
main()