Looker Community

Adding DashboardElements to a Dashboard via API

Hello,

I’m having some trouble figuring out how to add dashboard elements to a dashboard via the Looker API. Has anyone been able to accomplish this, and if so, could I get a walk through of how to do so?

Thanks in advance!

HI @ahuynh,

This is not yet possible with our existing API. This is something we are looking into extending, I don’t think we have a solid timeline, but it is a work in progress.

Mike

In the 5.14 release, we have added newly documented experimental endpoints for managing Dashboard Elements via the 3.1 API. You can learn more about the change described for the API Explorer

This code snippet is from test code for a Python SDK prototype (still rapidly and drastically changing, with no public or beta ETA) that shows the basics of how to add elements to a new or existing dashboard. Most of the work on the SDK at this point is just making calls to the swagger python codegen API more convenient, but this code does show the kinds of values you need to provide to the API, and a method for adding and manipulating dashboard elements. I hope it helps clear up some things for you.

def draw_dashboard(sdk, dashboard):
    top_center = sdk.create_dashboard_element(
        dashboard_id=dashboard.id,
        type="text",
        title="What's all this 1",
        title_text="This is the title of a text element",
        subtitle_text="Put any subtitle here",
        body_text="Put something else in the body"
    )
    # vis types from https://docs.looker.com/reference/dashboard-reference/lookml-visualization-reference#type-for-elements
    # text | table | single_value | looker_single_record | looker_column | looker_bar |
    # looker_scatter | looker_line | looker_area | looker_pie | looker_donut_multiples |
    # looker_geo_coordinates | looker_geo_choropleth
    brand_query = sdk.create_query(
        model="thelook",
        view="products",
        fields=["products.brand", "products.count"],
        vis_config={
            'type': 'looker_column'
        }
    )
    # vis_config={
    #     'stacking': '',
    #     'show_value_labels': False,
    #     'label_density': 25,
    #     'legend_position': 'center',
    #     'x_axis_gridlines': False,
    #     'y_axis_gridlines': True,
    #     'show_view_names': True,
    #     'limit_displayed_rows': False,
    #     'y_axis_combined': False,
    #     'show_y_axis_labels': True,
    #     'show_y_axis_ticks': True,
    #     'y_axis_tick_density': 'default',
    #     'y_axis_tick_density_custom': 5,
    #     'show_x_axis_label': True,
    #     'show_x_axis_ticks': True,
    #     'x_axis_scale': 'auto',
    #     'y_axis_scale_mode': 'linear',
    #     'ordering': 'none',
    #     'show_null_labels': False,
    #     'show_totals_labels': False,
    #     'show_silhouette': False,
    #     'totals_color': '#808080',
    #     'type': 'looker_column',
    #     'show_row_numbers': True,
    #     'truncate_column_names': False,
    #     'hide_totals': False,
    #     'hide_row_totals': False,
    #     'table_theme': 'editable',
    #     'enable_conditional_formatting': False,
    #     'conditional_formatting_include_totals': False,
    #     'conditional_formatting_include_nulls': False,
    #     'hidden_fields': ['users.id'],
    #     'series_types': {},
    #     'y_axis_orientation': ['right', 'left'],
    #     'y_axis_max': ['10', '100']
    # }
    bottom_left = sdk.create_dashboard_element(
        dashboard_id=dashboard.id,
        type="vis",
        title_text="Brand Count",
        title="Brand Count Title",
        subtitle_text="Vis Subtitle",
        body_text="Vis Body",
        query_id=brand_query.id
    )
    bottom_right = sdk.create_dashboard_element(
        dashboard_id=dashboard.id,
        type="data",
        title_text="Users Data",
        title="Users Data Title",
        subtitle_text="Data Subtitle",
        body_text="Data Body",
        query_id=query.id
    )
    dashboard = sdk.dashboard(dashboard.id)  # fetch updated dashboard
    layout = dashboard.dashboard_layouts[0]
    top_height = 2
    for comp in layout.dashboard_layout_components:
        if comp.dashboard_element_id == top_center.id:
            sdk.update_dashboard_layout_component(
                comp.id,
                dashboard_layout_id=layout.id,
                dashboard_element_id=top_center.id,
                row=0,
                column=0,
                width=24,
                height=top_height
            )
        elif comp.dashboard_element_id == bottom_left.id:
            sdk.update_dashboard_layout_component(
                comp.id,
                dashboard_layout_id=layout.id,
                dashboard_element_id=bottom_left.id,
                row=top_height,
                column=0,
                width=12,
                height=8
            )
        elif comp.dashboard_element_id == bottom_right.id:
            sdk.update_dashboard_layout_component(
                comp.id,
                dashboard_layout_id=layout.id,
                dashboard_element_id=bottom_right.id,
                row=top_height,
                column=12,
                width=12,
                height=8
            )
    return sdk.dashboard(dashboard.id)  # fetch updated dashboard

def create_filters(sdk: LookerSDK, dashboard: Dashboard):
    for f in dashboard.dashboard_filters:
        sdk.delete_dashboard_filter(f.id)

    dashboard_filter = sdk.create_dashboard_filter(
        dashboard_id=dashboard.id,
        name='Brand match',
        title='Brand match',
        row=0,
        type='field_filter',
        model='thelook',
        explore='products',
        dimension='products.brand',
        allow_multiple_values=True,
        default_value='A%,B%,C%,D%',
    )

    # activate the filter for the brand tile
    [brand_tile] = [tile for tile in dashboard.dashboard_elements if tile.title_text == 'Brand Count']
    sdk.update_dashboard_element(
        brand_tile.id,
        listen={'Brand match': 'products.brand'}
    )
    return sdk.dashboard(dashboard.id)  # fetch updated dashboard

Trying to do this and getting an error:

'DashboardApi' object has no attribute 'create_dashboard_element'