Examples#

def export_report(template, out_path, server):
    try:
        server.export_report_as_pptx(
            template.guid,
            out_path,
            query=None,
        )
    except Exception as e:
        print(f"Error: unable to save the pptx report using the REST API: {e}")

def test_create_pptx_items(server, test_dir):
    # source pptx item
    item = server.create_item(name="input.pptx", source="pptx-test", sequence=0)
    item.set_payload_file(os.path.join(test_dir, "test_data", "input.pptx"))
    if server.put_objects(item) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push source pptx object")

    # source pptx item - notoc
    item = server.create_item(name="input-notoc.pptx", source="pptx-test", sequence=0)
    item.set_payload_file(os.path.join(test_dir, "test_data", "input-notoc.pptx"))
    if server.put_objects(item) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push source pptx object")

    # title
    title = server.create_item(name="title_text", source="pptx-test", sequence=0)
    title.set_payload_string("My presentation")
    if server.put_objects([title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push title object")

    # toc title
    title = server.create_item(name="toc_title", source="pptx-test", sequence=0)
    title.set_payload_string("Table of contents")
    if server.put_objects([title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push toc_title object")

    # toc link text
    title = server.create_item(name="toc_link_text", source="pptx-test", sequence=0)
    title.set_payload_string("Go back")
    if server.put_objects([title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push toc_link_text object")

    # plots title
    text_title = server.create_item(
        name="tri_plot_title", source="pptx-test", sequence=0
    )
    text_title.set_payload_string("\u5168\u90e8\u4e00\u8d77")
    if server.put_objects([text_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push text_title object")

    text = server.create_item(name="text", source="pptx-test", sequence=0)
    text.set_payload_string(
        "On a dark desert highway, cool wind in my hair..."
        "\nWarm smell of colitas, rising up through the air..."
        "\nUp ahead in the distance, I saw shimmering light..."
        "\nMy head grew heavy and my sight grew dim..."
        "\nI had to stop for the night..."
    )
    if server.put_objects([text]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push text object")

    # html_title item
    html_title = server.create_item(name="html_title", source="pptx-test", sequence=0)
    html_title.set_payload_string("My HTML item")
    if server.put_objects([html_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push html_title object")

    # HTML item
    html = server.create_item(name="html", source="pptx-test", sequence=0)
    html.set_payload_html(
        "<h1>Heading 1</h1>"
        "<h2>Heading 2</h2>"
        "<h3>Heading 3</h3>"
        "<h4>Heading 4</h4>"
        "<h5>Heading 5</h5>"
        "Two breaks below"
        "<br><br />"
        "<h6>Heading 6 (& one break below)</h6>"
        "<br>"
        "The end"
    )
    html.add_tag("pptx_slide_title", "headers and breaks")
    if server.put_objects([html]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push text object")

    # HTML item for insertion into slide title
    html = server.create_item(name="html-anim-title", source="pptx-test", sequence=0)
    html.set_payload_html("<h1>Anim + text</h1>Takes up two slides")
    html.add_tag("pptx_slide_title", "--movie")
    if server.put_objects([html]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push text object")

    # Image item
    image_title = server.create_item(name="image_title", source="pptx-test", sequence=0)
    image_title.set_payload_string("My image")
    if server.put_objects([image_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push image_title object")

    with open(os.path.join(test_dir, "test_data", "image.png"), "rb") as fp:
        img = fp.read()
    image_item = server.create_item(name="image", source="pptx-test", sequence=0)
    image_item.set_payload_image(img)
    if server.put_objects([image_item]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push image_item object")

    # logo at the end
    with open(os.path.join(test_dir, "test_data", "nexus_logo.png"), "rb") as fp:
        logo = fp.read()
    logo_item = server.create_item(name="logo", source="pptx-test", sequence=0)
    logo_item.set_payload_image(logo)
    if server.put_objects([logo_item]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push logo_item object")

    # movie item
    anim_title = server.create_item(name="anim_title", source="pptx-test", sequence=0)
    anim_title.set_payload_string("My movie with text")
    if server.put_objects([anim_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push anim_title object")

    anim = server.create_item(name="anim", source="pptx-test", sequence=0)
    anim.set_payload_animation(os.path.join(test_dir, "test_data", "movie.mp4"))
    if server.put_objects([anim]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push anim object")

    # table item
    table_title = server.create_item(name="table_title", source="pptx-test", sequence=0)
    table_title.set_payload_string("My table")
    if server.put_objects([table_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push table_title object")

    #  table
    random.seed(12345)
    array = numpy.ndarray((10, 5), dtype="f")
    for row in range(10):
        array[row, 0] = row
        array[row, 1] = row * 200 - 1003
        array[row, 2] = 1.2 * row ** 3.4 + 3.5 * row + 123.0
        array[row, 3] = (10 - row) ** 2.3
        array[row, 4] = random.uniform(-2000, 6000)
    col_names = ["Linear", "Shift", "Polynomial", "Invert Poly", "Random"]
    d = dict(array=array, collbls=col_names, title="Numeric table")
    table = server.create_item(name="table", source="pptx-test", sequence=0)
    table.set_payload_table(d)
    table.add_tag(
        "pptx_slide_title",
        "<h2>Linear</h2>linear description<br /><br /><h4>Iterations: 10</h4>",
    )
    if server.put_objects(table) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push table object")

    #  table 2
    random.seed(54321)
    array = numpy.ndarray((10, 5), dtype="f")
    for row in range(10):
        array[row, 0] = row
        array[row, 1] = row * 500 - 900
        array[row, 2] = 2.5 * row ** 3.4 + 5.5 * row + 65.0
        array[row, 3] = (10 - row) ** 9.4
        array[row, 4] = random.uniform(-5000, 3000)
    col_names = ["ID", "Location[X]", "Location[Y]", "Location[Z]", "turbViscosity[X]"]
    d = dict(array=array, collbls=col_names, title="Numeric table2")
    table2 = server.create_item(name="table", source="pptx-test", sequence=0)
    table2.set_payload_table(d)
    table2.add_tag("pptx_slide_title", "Location-Viscosity")
    if server.put_objects(table2) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push table2 object")

    # table 3
    value = "B \u4e14"
    # Numpy requires bytes...
    value = value.encode("utf-8")
    array = numpy.array(
        [['A {{"mylink"|nexus_link:"LINK"}}', value, "C"], [1, 2, 3]], dtype="S50"
    )
    d = dict(
        array=array,
        rowlbls=["Row 1", "Row 2"],
        collbls='["Column A", "Column B", "Column C"]',  # list as a string
        title="Simple ASCII table",
    )
    table3 = server.create_item(name="table", source="pptx-test", sequence=0)
    table3.set_payload_table_values(**d)
    if server.put_objects(table3) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push table3 object")

    # Tree item
    tree_title = server.create_item(name="tree_title", source="pptx-test", sequence=0)
    tree_title.set_payload_string("My tree")
    if server.put_objects([tree_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push tree_title object")
    leaves = list()
    for i in range(2):
        leaves.append(dict(key="leaves", name=f"Leaf {i}", value=i))
    children = list()
    children.append(dict(key="child", name="Boolean example", value=True))
    children.append(
        dict(
            key="child",
            name="Simple string",
            value='Hello world!!! {{"mylink"|nexus_link:"LINK"}}',
        )
    )
    children.append(dict(key="child", name="Integer example", value=10))
    children.append(dict(key="child", name="Float example", value=99.99))
    children.append(
        dict(key="child", name="multi-valued child", value=["val1", "val2"])
    )
    name = "Simple \u4e14 string"
    value = "Hello \u4e14 world!!"
    children.append(dict(key="child", name=name, value=value))
    children.append(dict(key="child", name="Integer string 3", value="20200102"))
    children.append(
        dict(key="child", name="The current date", value=datetime.datetime.now())
    )
    # this entity will display the image item (or a link to it) created above
    children.append(
        dict(key="child", name="A data item guid", value=uuid.UUID(image_item.guid))
    )
    children.append(
        dict(
            key="child_parent",
            name="A child parent",
            value="Parents can have values",
            children=leaves,
            state="expanded",
        )
    )
    tree = list()
    tree.append(
        dict(
            key="root",
            name="Top Level",
            value=None,
            children=children,
            state="collapsed",
            tree_global_toggle="1",
        )
    )
    tree_item = server.create_item(name="tree", source="pptx-test", sequence=0)
    tree_item.set_payload_tree(tree)
    if server.put_objects(tree_item) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push tree object")

    # line plot item
    line_plot_title = server.create_item(
        name="line_plot_title", source="pptx-test", sequence=0
    )
    line_plot_title.set_payload_string("My line plot")
    if server.put_objects([line_plot_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push line_plot_title object")

    d = dict(
        array=numpy.array(
            [
                [
                    3.98297228e00,
                    4.41889995e00,
                    4.85482761e00,
                    5.29075528e00,
                    5.72668294e00,
                ],
                [
                    -5.08168967e00,
                    -1.48497880e01,
                    -2.41973247e01,
                    -3.41192688e01,
                    -4.56471214e01,
                ],
            ],
            dtype="float64",
        ),
        rowlbls=["X", "Lift"],
        title="Cumulative_Total_Lift",
        collbls=None,
        plot="line"
    )
    line_plot = server.create_item(name="line_plot", source="pptx-test", sequence=0)
    line_plot.set_payload_table(d)
    if server.put_objects(line_plot) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push line_plot object")

    # bar plot item
    bar_plot_title = server.create_item(
        name="bar_plot_title", source="pptx-test", sequence=0
    )
    bar_plot_title.set_payload_string("My bar plot")
    if server.put_objects([bar_plot_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push bar_plot_title object")

    d = dict(
        array=numpy.array(
            [
                [
                    4800.47763538,
                    7627.54214859,
                    -39.70935933,
                    -529.44011697,
                    -735.07384467,
                    -5148.85339519,
                    2501.53895235,
                    936.93001461,
                    1147.34070158,
                ]
            ],
            dtype="float64",
        ),
        rowlbls=None,
        title="Total Lift Bar Chart",
        collbls=[
            "Axle",
            "Body",
            "Canard",
            "Fwd",
            "Mid",
            "Parachute",
            "Rear",
            "Strut",
            "Vert",
        ],
        plot="bar",
    )
    bar_plot = server.create_item(name="bar_plot", source="pptx-test", sequence=0)
    bar_plot.set_payload_table(d)
    bar_plot.add_tag("pptx_slide_title", "bar-plot-tag-title")
    bar_plot.add_tag("pptx_slide_comments", "<h3>Iteration #: 2025</h3>")
    if server.put_objects(bar_plot) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push bar_plot object")

    # pie plot item
    pie_plot_title = server.create_item(
        name="pie_plot_title", source="pptx-test", sequence=0
    )
    pie_plot_title.set_payload_string("My pie plot")
    if server.put_objects([pie_plot_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push pie_plot_title object")

    d = dict(
        array=numpy.array(
            [
                [
                    338.0,
                    192.0,
                    128.0,
                    32.0,
                    276.0,
                    1244.0,
                    344.0,
                    144.0,
                    104.0,
                    494.0,
                    182.0,
                    86.0,
                    980.0,
                    385.0,
                    1668.0,
                ]
            ],
            dtype="float64",
        ),
        rowlbls=None,
        title="Server Element Count",
        collbls=[
            "engine",
            "tires",
            "wheels",
            "lights",
            "front body",
            "rear body",
            "floor",
            "windshields",
            "windows",
            "bumpers",
            "hood",
            "mounts",
            "guardrail supports",
            "guardrail supports ->> 0001",
            "guardrail",
        ],
        plot="pie",
        pptx_legend_position="left"
    )
    pie_plot = server.create_item(name="pie_plot", source="pptx-test", sequence=0)
    pie_plot.set_payload_table(d)
    pie_plot.add_tag("pptx_slide_title", "pie-plot-tag-title")
    if server.put_objects(pie_plot) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push pie_plot object")

    # Scene title
    scene_title = server.create_item(name="scene_title", source="pptx-test", sequence=0)
    scene_title.set_payload_string("My 3D scene")
    if server.put_objects([scene_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push scene_title object")

    # Scene
    scene_item = server.create_item(name="scene", source="pptx-test", sequence=0)
    scene_item.set_payload_scene(os.path.join(test_dir, "test_data", "scene.avz"))
    if server.put_objects([scene_item]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push scene object")

    # ens title
    ens_title = server.create_item(name="ens_title", source="pptx-test", sequence=0)
    ens_title.set_payload_string("My session file")
    if server.put_objects([ens_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push ens_title object")

    # ens session file
    session_item = server.create_item(name="session", source="pptx-test", sequence=0)
    session_item.set_payload_file(os.path.join(test_dir, "test_data", "session.ens"))
    pie_plot.add_tag("pptx_slide_title", "session-tag-title")
    if server.put_objects([session_item]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push ens object")

    # evsn title
    evsn_title = server.create_item(name="evsn_title", source="pptx-test", sequence=0)
    evsn_title.set_payload_string("My scenario file")
    if server.put_objects([evsn_title]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push evsn_title object")

    # evsn scenario file
    scenario_item = server.create_item(name="scenario", source="pptx-test", sequence=0)
    scenario_item.set_payload_file(os.path.join(test_dir, "test_data", "scenario.evsn"))
    if server.put_objects([scenario_item]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push evsn object")

    # end text
    end_text = server.create_item(name="end_text", source="pptx-test", sequence=0)
    end_text.set_payload_string("The end")
    if server.put_objects([end_text]) != requests.codes.ok:
        print(server.get_last_error())
        print("Error: Unable to push end_text object")


def test_pptx_report_toc(server, db_dir):
    # ------with toc------------
    # create the parent pptx template
    pptx_template = server.create_template(
        name="pptx", parent=None, report_type="Layout:pptx"
    )
    # set the input pptx file
    pptx_template.input_pptx = "input.pptx"
    # set the output pptx file
    pptx_template.output_pptx = "output-get.pptx"
    # use_all_slides will use all the slides from the input pptx
    pptx_template.use_all_slides = "1"
    # set a top level filter of items in the database.
    pptx_template.set_filter("A|i_src|cont|pptx-test;")
    # save
    server.put_objects(pptx_template)

    # default report
    try:
        server.get_pptx_from_report(
            pptx_template.guid, directory_name=db_dir, query=None
        )
    except Exception as e:
        print(f"Error: unable to save the pptx report: {e}")
    verify_export(os.path.join(db_dir, pptx_template.output_pptx))

    # export the same report again with the specific REST API
    # uses a file name different from the one specified in output_pptx
    out_path = os.path.join(db_dir, "output.pptx")
    export_report(pptx_template, out_path, server)


def test_pptx_report_toc_select(server, db_dir):
    # ------picking slides (with toc)------------
    pptx_template = server.create_template(
        name="pptx-select", parent=None, report_type="Layout:pptx"
    )
    # set the input pptx file
    pptx_template.input_pptx = "input.pptx"
    # set the output pptx file
    pptx_template.output_pptx = "output-select.pptx"
    # set a top level filter of items in the database.
    pptx_template.set_filter("A|i_src|cont|pptx-test;O|i_tags|cont|imgcmp;")
    # stop using all slides
    pptx_template.use_all_slides = "0"
    server.put_objects(pptx_template)

    # start slide
    slide = server.create_template(
        name="start", parent=pptx_template, report_type="Layout:pptxslide"
    )
    slide.source_slide = "1"
    server.put_objects(slide)
    server.put_objects(pptx_template)

    # toc
    slide = server.create_template(
        name="toc", parent=pptx_template, report_type="Layout:pptxslide"
    )
    slide.source_slide = "2"
    server.put_objects(slide)
    server.put_objects(pptx_template)

    slide = server.create_template(
        name="html", parent=pptx_template, report_type="Layout:pptxslide"
    )
    slide.source_slide = "3"
    server.put_objects(slide)
    server.put_objects(pptx_template)

    slide = server.create_template(
        name="anim-text", parent=pptx_template, report_type="Layout:pptxslide"
    )
    slide.source_slide = "5"
    slide.set_html("<h1>Anim + text</h1>Takes up two slides")
    slide.set_filter("O|i_name|cont|anim;O|i_name|cont|text;")
    server.put_objects(slide)
    server.put_objects(pptx_template)

    table_intro_wrapper = server.create_template(
        name="table-intro-wrap", parent=pptx_template, report_type="Layout:pptxslide"
    )
    table_intro_wrapper.source_slide = "6"
    table_intro_wrapper.set_html("<h1>table-intro-wrapper</h1> subtitle")
    table_intro_wrapper.set_filter("A|i_name|cont|table;")
    server.put_objects(table_intro_wrapper)
    server.put_objects(pptx_template)

    table_intro = server.create_template(
        name="table-intro", parent=table_intro_wrapper, report_type="Layout:pptxslide"
    )
    table_intro.source_slide = "7"
    table_intro.set_html("<h1>table-intro</h1> subtitle")
    table_intro.set_filter("A|i_name|cont|table;")
    server.put_objects(table_intro)
    server.put_objects(table_intro_wrapper)
    server.put_objects(pptx_template)

    slide = server.create_template(
        name="table", parent=table_intro, report_type="Layout:pptxslide"
    )
    slide.source_slide = "8"
    slide.add_property({"show_tag_title_only": "1"})
    slide.set_html("<h1>table</h1>table description")
    server.put_objects(slide)
    server.put_objects(table_intro)
    server.put_objects(table_intro_wrapper)
    server.put_objects(pptx_template)

    slide = server.create_template(
        name="tree", parent=pptx_template, report_type="Layout:pptxslide"
    )
    slide.source_slide = "9"
    slide.set_html("<h1>Tree</h1>")  # should be ignored
    slide.set_filter("A|i_name|cont|tree;")
    server.put_objects(slide)
    server.put_objects(pptx_template)

    slide = server.create_template(
        name="line", parent=pptx_template, report_type="Layout:pptxslide"
    )
    slide.source_slide = "10"
    slide.set_filter("A|i_name|cont|line_plot;")
    server.put_objects(slide)
    server.put_objects(pptx_template)

    slide = server.create_template(
        name="bar", parent=pptx_template, report_type="Layout:pptxslide"
    )
    slide.source_slide = "11"
    slide.set_html("<h1>Bar plot</h1>Bar description")
    slide.set_filter("A|i_name|cont|bar_plot;")
    slide.add_property({"pptx_log_base": "2"})
    server.put_objects(slide)
    server.put_objects(pptx_template)

    slide = server.create_template(
        name="pie", parent=pptx_template, report_type="Layout:pptxslide"
    )
    slide.source_slide = "12"
    slide.set_filter("A|i_name|cont|pie_plot;")
    slide.add_property({"pptx_legend_overlap": "1"})
    server.put_objects(slide)
    server.put_objects(pptx_template)

    session_intro = server.create_template(
        name="session-intro", parent=pptx_template, report_type="Layout:pptxslide"
    )
    session_intro.source_slide = "16"
    session_intro.add_property({"exclude_from_toc": "1"})
    session_intro.exclude_from_toc = "1"
    session_intro.set_html("<h1>session-intro</h1>")
    session_intro.set_comments("<h3>Iteration #: 10</h3>")
    # session_intro.set_filter("A|i_name|cont|session;")
    server.put_objects(session_intro)
    server.put_objects(pptx_template)

    slide = server.create_template(
        name="session", parent=session_intro, report_type="Layout:pptxslide"
    )
    slide.source_slide = "17"
    slide.set_filter("A|i_name|cont|session;O|i_name|cont|logo;")
    server.put_objects(slide)
    server.put_objects(session_intro)
    server.put_objects(pptx_template)

    # create items comparison generator parent template
    generator = server.create_template(
        name="Items Comparison Gen", parent=pptx_template, report_type="Generator:itemscomparison"
    )
    # set properties
    generator.chunk_size = 2
    generator.filters_table = '{"arr": [["dp=dp0", "dp=dp1", "dp=dp0", "dp=dp2"], ' \
                                    '["var=temperature", "var=temperature", "var=temperature", "var=pressure"],' \
                                    ' ["300", "", "", ""]], ' \
                                    '"rowlbls": ["i_tags|cont", "i_tags|cont", "meta|margin_left"]}'

    server.put_objects(generator)
    server.put_objects(pptx_template)

    slide = server.create_template(
        name="img-cmp-gen", parent=generator, report_type="Layout:pptxslide"
    )
    slide.source_slide = "18"
    slide.set_html("<h1>Image comparison generator</h1>")
    slide.set_filter("A|i_tags|cont|imgcmp;")
    server.put_objects(slide)
    server.put_objects(generator)
    server.put_objects(pptx_template)

    # end
    slide = server.create_template(
        name="end", parent=pptx_template, report_type="Layout:pptxslide"
    )
    slide.source_slide = "19"
    server.put_objects(slide)
    server.put_objects(pptx_template)

    out_path = os.path.join(db_dir, pptx_template.output_pptx)
    export_report(pptx_template, out_path, server)