Open xavier-GitHub76 opened 4 months ago
To resolve the superset.commands.exceptions.CommandInvalidError: Error importing dashboard
issue when trying to import dashboards using the CLI in Apache Superset, follow these steps:
Check User Permissions:
Ensure the user has the necessary permissions to create or overwrite dashboards. This is checked by security_manager.can_access("can_write", "Dashboard")
.
Validate Configuration:
Ensure the dashboard configuration (config
dictionary) is correctly formatted and includes all necessary fields. Pay special attention to the uuid
, position
, and metadata
fields.
Handle UUIDs and IDs:
Ensure that the UUIDs for charts and datasets are correctly mapped and updated in the configuration. The update_id_refs
function helps in updating these references.
Remove Deprecated Fields:
Remove any deprecated fields from the configuration, such as show_native_filters
in the metadata
.
Additionally, ensure that the CLI command for importing dashboards is correctly used. Here is an example of how to use the import_dashboards
command from superset/cli/importexport.py
:
@click.command()
@with_appcontext
@click.option(
"--path",
"-p",
required=True,
help="Path to a single ZIP file",
)
@click.option(
"--username",
"-u",
required=True,
help="Specify the user name to assign dashboards to",
)
def import_dashboards(path: str, username: Optional[str]) -> None:
"""Import dashboards from ZIP file"""
from superset.commands.dashboard.importers.dispatcher import ImportDashboardsCommand
from superset.commands.importers.v1.utils import get_contents_from_bundle
if username is not None:
g.user = security_manager.find_user(username=username)
if is_zipfile(path):
with ZipFile(path) as bundle:
contents = get_contents_from_bundle(bundle)
else:
with open(path) as file:
contents = {path: file.read()}
try:
ImportDashboardsCommand(contents, overwrite=True).run()
except Exception: # pylint: disable=broad-except
logger.exception(
"There was an error when importing the dashboards(s), please check "
"the exception traceback in the log"
)
sys.exit(1)
By following these steps and ensuring the configuration and permissions are correctly handled, you should be able to resolve the CommandInvalidError
when importing dashboards using the CLI in Apache Superset [1][2][3].
To continue the conversation, mention @dosu.
I have same issue as you too, in my cases:
Okay, please follow this https://github.com/apache/superset/issues/26130 to debug your zip file and yaml file to see any arguments have errors
Hello, my zip file is correct : the zip file (created from CLI) is imported with success from gui I have just the error when I import the zip file from CLI
I have try again on superset 4.0.0, I have the same behavior
Best regards
Yes, I have the same problem. Neither export nor import works on 4.0.1
If I import dashboard via GUI than import_dashboard from CLI successfully updates the dashboard. I'm also wondering where should I place the password for the database when importing via CLI? Superset prompts you for it when importing through the GUI.
@maximStafeevAtAllscripts What worked for me:
I found some similar issues and discussions that might help you resolve the "superset.commands.exceptions.CommandInvalidError: Error importing dashboard" error when using the CLI to import dashboards in Apache Superset:
Similar Discussions:
Closed Issues:
Root
|
-> dataset_export_20240308T102914
|
-> Databases
-> Datasets
Dashboard import asking for password database failed: The suggested solution is to use the API for importing dashboards instead of the CLI. Here is a sample script:
import requests
new_zip_file = 'path/to/dashboard_export.zip'
username = 'admin'
password = 'admin'
base_url = 'http://localhost:8088'
while True:
try:
response = requests.get(base_url, timeout=10)
response.raise_for_status()
break
except Exception:
pass
login_url = f"{base_url}/login/"
session = requests.Session()
payload = {}
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'en-US,en;q=0.9'
}
response = session.request("GET", login_url, headers=headers, data=payload)
csrf_token = response.text.split('<input id="csrf_token" name="csrf_token" type="hidden" value="')[1].split('">')[0]
# perform login
payload = f'csrf_token={csrf_token}&username={username}&password={password}'
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Content-Type': 'application/x-www-form-urlencoded'}
session.request("POST", login_url, headers=headers, data=payload, allow_redirects=False)
cookie = session.cookies.get_dict().get('session')
print(session.cookies.get_dict())
# Import dashboards
import_dashboard_url = f"{base_url}/api/v1/dashboard/import/"
with open(new_zip_file, 'rb') as f:
payload = {
'passwords': '{"databases/replaceWithYourDatabase.yaml":"databasePassword"}',
'overwrite': 'true'
}
files = [
('formData', ('dashboards.zip', f, 'application/zip'))
]
headers = {
'Accept': 'application/json',
'Cookie': f'session={cookie}',
'X-CSRFToken': csrf_token
}
response = requests.request("POST", import_dashboard_url, headers=headers, data=payload, files=files)
print(response.text)
Additionally, ensure that the user running the import command has the necessary permissions to either overwrite an existing dashboard or create a new one. The ImportDashboardsCommand
class handles the import process and checks for these permissions [6][7][8].
If the user does not have the necessary permissions, you can either grant the required permissions or run the import command with a user who has the appropriate permissions. Also, make sure that the JSON file being imported is valid and contains the necessary data for the import process.
To continue talking to Dosu, mention @dosu.
Pinging @betodealmeida in case they have any insight here.
Bug description
Hello,
I try to export/import dashboard with cli but I have an error "superset.commands.exceptions.CommandInvalidError: Error importing dashboard"
How to reproduce the bug
I export my dashboards with CLI :
I delete all elements on my superset
I try import with cli superset import-dashboards -p myDashboards.zip -u xavier
I try also after a creation of database connexion :
Best regards
Screenshots/recordings
No response
Superset version
master / latest-dev
Python version
3.9
Node version
16
Browser
Chrome
Additional context
No response
Checklist