Exemple #1
0
def download_dashboard(
    dashboard: models.Dashboard,
    style: str = "tiled",
    width: int = 545,
    height: int = 842,
    filters: Optional[Dict[str, str]] = None,
):
    """Download specified dashboard as PDF"""
    assert dashboard.id
    id = int(dashboard.id)
    task = sdk.create_dashboard_render_task(
        id,
        "pdf",
        models.CreateDashboardRenderTask(
            dashboard_style=style,
            dashboard_filters=urllib.parse.urlencode(filters) if filters else None,
        ),
        width,
        height,
    )

    if not (task and task.id):
        raise sdk_exceptions.RenderTaskError(
            f'Could not create a render task for "{dashboard.title}"'
        )

    # poll the render task until it completes
    elapsed = 0.0
    delay = 0.5  # wait .5 seconds
    while True:
        poll = sdk.render_task(task.id)
        if poll.status == "failure":
            print(poll)
            raise sdk_exceptions.RenderTaskError(
                f'Render failed for "{dashboard.title}"'
            )
        elif poll.status == "success":
            break

        time.sleep(delay)
        elapsed += delay
    print(f"Render task completed in {elapsed} seconds")

    result = sdk.render_task_results(task.id)
    filename = f"{dashboard.title}.pdf"
    with open(filename, "wb") as f:
        f.write(result)
    print(f'Dashboard pdf saved to "{filename}"')
Exemple #2
0
def download_dashboard(sdk, dashboard_id, file_name, size=DEFAULT_PDF_PAGE_SIZE, is_landscape=DEFAULT_PDF_IS_LANDSCAPE, filters=[]):
    if filters:
        filters = [f'{filter_[0]}={filter_[1]}' for filter_ in filters]
        filter_exp = '&'.join(filters)
    else:
        filter_exp = ''
    file_log.debug(f'download_dashboard({dashboard_id}) with filter expression {filter_exp}')
    if DOWNLOAD_DASHBOARDS:
        try:
            height = PDF_SIZES[size]['height']
            width = PDF_SIZES[size]['width']
        except:
            file_log.debug('FAILED to set height and width for PDF render')
            height = DEFAULT_PDF_HEIGHT
            width = DEFAULT_PDF_WIDTH        
        
        task = sdk.create_dashboard_render_task(
            dashboard_id= dashboard_id,
            result_format= 'pdf',
            body= models.CreateDashboardRenderTask(
                dashboard_style= 'tiled',
                dashboard_filters= filter_exp,
            ),
            height= height,
            width= width,
        )

        # poll the render task until it completes
        elapsed = 0.0
        delay = 0.5  # wait .5 seconds
        while True:
            poll = sdk.render_task(task.id)
            if poll.status == "failure":
                logger.warning(f'render failure: {poll}')
                return False
            elif poll.status == "success":
                result = sdk.render_task_results(task.id)
                with open(file_name, "wb") as f:
                    f.write(result)
                logger.info(f"Render task completed in {elapsed} seconds")
                return True

            time.sleep(delay)
            elapsed += delay
Exemple #3
0
def download_dashboard(dashboard: models.Dashboard, style: str, width: int,
                       height: int):
    """Download specified dashboard as PDF."""
    assert dashboard.id
    id = int(dashboard.id)
    task = sdk.create_dashboard_render_task(
        id,
        "pdf",
        models.CreateDashboardRenderTask(dashboard_style=style),
        width,
        height,
    )

    if not (task and task.id):
        print(f"Could not create a render task for {dashboard.title}")
        return None

    # poll the render task until it completes
    elapsed = 0.0
    delay = 0.5  # wait .5 seconds
    while True:
        poll = sdk.render_task(task.id)
        if poll.status == "failure":
            print(poll)
            print(f"Render failed for {dashboard.title}")
            return None
        elif poll.status == "success":
            break

        time.sleep(delay)
        elapsed += delay
    print(f"Render task completed in {elapsed} seconds")

    result = sdk.render_task_results(task.id)
    fileName = f"{dashboard.title}.pdf"
    with open(fileName, "wb+") as f:
        f.write(result)
    print(f"Dashboard pdf saved to {fileName}")
            def get_pdf():
                filters = 'url_filter_settings_from_Looker_link'
                dashboard_number=326
                task = sdk.create_dashboard_render_task(
                    dashboard_id=dashboard_number,
                    result_format="pdf",
                    body=models.CreateDashboardRenderTask(
                        dashboard_style="tiled",
                        dashboard_filters=filters
                    ),
                    width=545,
                    height=842
                )

                if not (task and task.id):
                    print("Render failed")

                #Poll the render task until it completes - needs to sleep or else render fails.
                elapsed = 0.0
                delay = 0.5  # wait .5 seconds
                while True:
                    poll = sdk.render_task(task.id)
                    if poll.status == "failure":
                        print(poll)
                        print("Render failed")
                    elif poll.status == "success":
                        break

                    time.sleep(delay)
                    elapsed += delay
                print(f"Render task completed in {elapsed} seconds")

                result = sdk.render_task_results(task.id)
                filename = directory + user + "/" + cid + "_file.pdf"
                with open(filename, "wb") as f:
                    f.write(result)
                print(f'Dashboard pdf saved as "{filename}"')
                def get_pdf():
                    filters = '?Campaign%20Name=&Campaign%20ID=' + str(cid) + '&filter_config=%7B"Campaign%20Name":%5B%7B"type":"%3D","values":%5B%7B"constant":""%7D,%7B%7D%5D,"id":22%7D%5D,"Campaign%20ID":%5B%7B"type":"%3D","values":%5B%7B"constant":"' + str(cid) + '"%7D,%7B%7D%5D,"id":23%7D%5D%7D'
                    dashboard_number=326
                    task = sdk.create_dashboard_render_task(
                        dashboard_id=dashboard_number,
                        result_format="pdf",
                        body=models.CreateDashboardRenderTask(
                            dashboard_style="tiled",
                            dashboard_filters=filters
                        ),
                        width=545,
                        height=842
                    )

                    if not (task and task.id):
                        print("Render failed")

                    # poll the render task until it completes
                    elapsed = 0.0
                    delay = 0.5  # wait .5 seconds
                    while True:
                        poll = sdk.render_task(task.id)
                        if poll.status == "failure":
                            print(poll)
                            print("Render failed")
                        elif poll.status == "success":
                            break

                        time.sleep(delay)
                        elapsed += delay
                    print(f"Render task completed in {elapsed} seconds")

                    result = sdk.render_task_results(task.id)
                    filename = directory + user + "/" + str(cid) + "_file.pdf"
                    with open(filename, "wb") as f:
                        f.write(result)
                    print(f'Dashboard pdf saved as "{filename}"')