Esempio n. 1
0
def gen_report_complex_no_files() -> dp.Report:
    """Generate a complex layout report with simple elements"""
    select = dp.Select(blocks=[md_block, md_block], type=dp.SelectType.TABS)
    group = dp.Group(md_block, md_block, columns=2)

    return dp.Report(
        dp.Page(
            blocks=[
                dp.Group(md_block, md_block, columns=2),
                dp.Select(blocks=[md_block, group],
                          type=dp.SelectType.DROPDOWN),
            ],
            title="Page Uno",
        ),
        dp.Page(
            blocks=[
                dp.Group(select, select, columns=2),
                dp.Select(blocks=[md_block, md_block, md_block],
                          type=dp.SelectType.TABS),
            ],
            title="Page Duo",
        ),
        dp.Page(
            blocks=[
                dp.Group(group, group, columns=2),
                dp.Select(blocks=[select, select], type=dp.SelectType.TABS),
            ],
            title="Page Tres",
        ),
    )
Esempio n. 2
0
def test_gen_report_nested_blocks():
    s = "# Test markdown block <hello/> \n Test **content**"
    report = dp.Report(blocks=[
        dp.Group(dp.Text(s, name="test-id-1"),
                 "Simple string Markdown",
                 label="test-group-label"),
        dp.Select(
            blocks=[
                dp.Text(s, name="test-id-2", label="test-block-label"),
                "Simple string Markdown",
            ],
            label="test-select-label",
        ),
    ])

    # No additional wrapper block
    assert len(report.pages[0].blocks) == 2
    assert isinstance(report.pages[0].blocks[0], dp.Group)
    assert isinstance(report.pages[0].blocks[1], dp.Select)
    assert isinstance(report.pages[0].blocks[1].blocks[1], dp.Text)
    assert glom(report, ("pages.0.blocks", ["_attributes.label"])) == [
        "test-group-label", "test-select-label"
    ]
    assert glom(report, "pages.0.blocks.0.blocks.0.name") == "test-id-1"
    assert glom(
        report,
        "pages.0.blocks.1.blocks.0._attributes.label") == "test-block-label"
    assert_report(report, 0)
Esempio n. 3
0
def test_gen_failing_reports():
    # local reports with unsupported elements
    with pytest.raises(DPError):
        r = dp.Report(dp.DataTable(gen_df()))
        r._gen_report(embedded=True, title="TITLE", description="DESCRIPTION")

    # nested pages
    with pytest.raises((DocumentInvalid, DPError)):
        r = dp.Report(dp.Page(dp.Page(md_block)))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")
    with pytest.raises((DocumentInvalid, DPError)):
        r = dp.Report(dp.Group(dp.Page(md_block)))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")

    # group with 0 object
    with pytest.raises((DocumentInvalid, DPError)):
        r = dp.Report(dp.Page(dp.Group(blocks=[])))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")

    # select with 1 object
    with pytest.raises((DocumentInvalid, DPError)):
        r = dp.Report(dp.Page(dp.Select(blocks=[md_block])))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")

    # empty text block
    with pytest.raises((AssertionError, DocumentInvalid, DPError)):
        r = dp.Report(dp.Text(" "))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")
Esempio n. 4
0
def gen_report_complex_with_files(datadir: Path,
                                  single_file: bool = False,
                                  local_report: bool = False) -> dp.Report:
    # Asset tests
    lis = [1, 2, 3]
    small_df = gen_df()
    big_df = gen_df(10000)

    # text
    # md_block
    html_block = dp.HTML(html="<h1>Hello World</h1>")
    html_block_1 = dp.HTML(html=h2("Hello World"))
    code_block = dp.Code(code="print('hello')", language="python")
    formula_block = dp.Formula(formula=r"\frac{1}{\sqrt{x^2 + 1}}")
    big_number = dp.BigNumber(heading="Tests written", value=1234)
    big_number_1 = dp.BigNumber(heading="Real Tests written :)",
                                value=11,
                                change=2,
                                is_upward_change=True)
    embed_block = dp.Embed(url="https://www.youtube.com/watch?v=JDe14ulcfLA")

    # assets
    plot_asset = dp.Plot(data=gen_plot(), caption="Plot Asset")
    list_asset = dp.File(data=lis, filename="List Asset", is_json=True)
    img_asset = dp.File(file=datadir / "datapane-logo.png")

    # tables
    table_asset = dp.Table(data=small_df, caption="Test Basic Table")
    # local reports don't support DataTable
    dt_asset = table_asset if local_report else dp.DataTable(
        df=big_df, caption="Test DataTable")

    if single_file:
        return dp.Report(dp.Group(blocks=[md_block, dt_asset]))
    else:
        return dp.Report(
            dp.Page(
                dp.Select(md_block,
                          html_block,
                          html_block_1,
                          code_block,
                          formula_block,
                          embed_block,
                          type=dp.SelectType.TABS),
                dp.Group(big_number, big_number_1, columns=2),
            ),
            dp.Page(
                plot_asset,
                list_asset,
                img_asset,
                table_asset,
                dt_asset,
            ),
        )
Esempio n. 5
0
def test_markdown_format(datadir: Path):
    text = """
# My wicked markdown

{{plot}}

As above we do ...

{{select}}

Here's the dataset used...

{{}}
"""

    table_asset = gen_df()
    plot_asset = dp.Plot(data=alt.Chart(gen_df()).mark_line().encode(x="x",
                                                                     y="y"),
                         caption="Plot Asset")
    select_asset = dp.Select(dp.Text("Hello"), "World")

    # missing context
    with pytest.raises(DPError):
        dp.Text(text).format(table_asset,
                             plot=plot_asset,
                             select1=select_asset)
    with pytest.raises(DPError):
        dp.Text(text).format(plot=plot_asset, select=select_asset)

    # test string
    group = dp.Text(text).format(table_asset,
                                 plot=plot_asset,
                                 select=select_asset)
    assert isinstance(group, dp.Group)
    assert glom(group, ("blocks", ["_tag"])) == [
        "Text", "Plot", "Text", "Select", "Text", "Table"
    ]

    # test file
    group = dp.Text(file=datadir / "report.md").format(table_asset,
                                                       plot=plot_asset,
                                                       select=select_asset)
    assert isinstance(group, dp.Group)
    assert glom(group, ("blocks", ["_tag"])) == [
        "Text", "Plot", "Text", "Select", "Text", "Table"
    ]
    assert "file-input" in element_to_str(group)
    assert "file-input" in glom(group, "blocks.0.content")
Esempio n. 6
0
def test_gen_failing_reports():
    # nested pages
    with pytest.raises((DocumentInvalid, DPError)):
        r = dp.Report(dp.Page(dp.Page(md_block)))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")
    with pytest.raises((DocumentInvalid, DPError)):
        r = dp.Report(dp.Group(dp.Page(md_block)))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")

    # group with 0 object
    with pytest.raises((DocumentInvalid, DPError)):
        r = dp.Report(dp.Page(dp.Group(blocks=[])))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")

    # select with 1 object
    with pytest.raises((DocumentInvalid, DPError)):
        r = dp.Report(dp.Page(dp.Select(blocks=[md_block])))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")

    # empty text block
    with pytest.raises((AssertionError, DocumentInvalid, DPError)):
        r = dp.Report(dp.Text(" "))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")

    # empty df
    with pytest.raises((AssertionError, DocumentInvalid, DPError)):
        r = dp.Report(dp.DataTable(pd.DataFrame()))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")

    # invalid names
    with pytest.raises(DocumentInvalid):
        r = dp.Report(dp.Text("a", name="my-name"), dp.Text("a",
                                                            name="my-name"))
        r._gen_report(embedded=False, title="TITLE", description="DESCRIPTION")

    with pytest.raises(DPError):
        dp.Report(dp.Text("a", name="3-invalid-name"))
Esempio n. 7
0
                f'### Labs',
                dp.Plot(lab_bar), f'### Homeworks',
                dp.Plot(hw_bar), f'### Projects',
                dp.Plot(project_bar)
            ]),
    dp.Page(label='Exam Grades',
            blocks=[f'### Kernel Density Plot ',
                    dp.Plot(exams_kdp)]),
    dp.Page(label='Final Grades',
            blocks=[
                f'### Comparing Final Grade Data ',
                dp.Plot(fg_hist),
                dp.Plot(letter_pie)
            ]),
    dp.Page(
        dp.Select(blocks=[
            dp.Plot(sex_bar, label='Sex-Bar Chart'),
            dp.Plot(urm_bar, label='URM-Bar Chart'),
            dp.Plot(race_bar, label='Race-Bar Chart'),
            dp.Plot(sex_kdp, label='Sex-KDP')
        ],
                  type=dp.SelectType.DROPDOWN),
        label='Demographics',
        #blocks=[f'### Comparing Final Grade Data among Various Demographics']
    ))

# Publish
r.publish(name=f'CSE 1223 Dashboard AU19',
          open=True,
          description=f'Grade Data for CSE 1223 during the AU19 semester')
Esempio n. 8
0
        dp.BigNumber(value=f"${round(burn)}", heading="Monthly outgoings"),
        dp.BigNumber(value=f"${CURRENT_CASH_GBP}",
                     heading="Cash in bank",
                     is_upward_change=True,
                     change=7)
    ],
             columns=2),
    dp.Select(blocks=[
        dp.Group(blocks=[
            """This plot models cash and revenue, dependent on various other growth scenarios and based on this month's burn. 

- Blue area is cash remaining
- Orange line is the cumulative sum of all revenue
- Purple line is monthly revenue

> Drag the slider to adjust growth rate. The growth rate at which the blue area never crosses 0 is the growth you need to achieve **[Default Alive](http://paulgraham.com/aord.html)**.
""", chart_final
        ],
                 label='Interactive Plot'),
        dp.Group(blocks=[
            "> In this dataset, `cash_0g`/`revenue_0g` presumes no further growth, whereas `cash_dg`/`revenue_dg` presumes growth at the current rate continues.",
            dp.DataTable(df)
        ],
                 label='Interactive Dataset')
    ])).publish(name='Finance Model', open=True)
"""# Deploying as an app

Because we defined the input variables using `dp.Params` above, we can also easily use Datapane to turn our script into an app which non-technical people can run themselves.

Note that deploying an app requires a private **Datapane Cloud** server, [so create one here](https://datapane.com/pricing/#contact-form) before continuing. Pricing starts at $99 per month with a 30-day free trial.

If we want user