Skip to content

How to change database and tables of exported Dashboard file (pickle file)? #4190

@JoeNguyen93

Description

@JoeNguyen93

Assume I have a Dashboard with a slice that points to tableA of dbA.sqlite. After exporting that Dashboard and import it as an Python object in my script in order to modify the database and tables, which attribute I should modify to point the slice to tableB of dbB.sqlite (The tables have the same schema, just different in name) ?

Example file:

import pickle

with open("test_dashboard.pickle", "rb") as f:
    dashboard = pickle.load(f)
    # first_table_obj = dashboard['datasources'][0]
    # print(dashboard['datasources'])
    # print(type(first_table_obj))  # <class 'superset.connectors.sqla.models.SqlaTable'>
    # print('Database id of table:', first_table_obj.database_id)
    dashboard_obj = dashboard['dashboards'][0]
    dashboard_obj.dashboard_title = 'TestDashboard_2'  # Change Dashboard Title
    print('----------')
    # for table in dashboard['datasources']:
    #     table.table_name = '{}_2'.format(table.table_name)
    #     table.database_id = 2
    for slice in dashboard_obj.slices:
        print('Data source of slice:', slice.datasource)  # commit_info and file_info (tables)
        print('Type of datasource:', type(slice.datasource))   # <class 'superset.connectors.sqla.models.SqlaTable'>
        print('Database of slice:', slice.datasource.database)  # Test_Data (db)
        print(type(slice.datasource.database))  # <class 'superset.models.core.Database'>
        print('Table id of the slice:', slice.datasource_id)  # Table id

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions