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", ), )
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")
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, ), )
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)
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"))
def test_gen_report_nested_mixed(): report = dp.Report( dp.Group( md_block_id, str_md_block, ), "Simple string Markdown #2", ) assert_report(report, 0) assert len(glom(report, "pages.0.blocks")) == 1 assert isinstance(glom(report, "pages.0.blocks.0"), dp.Group) assert isinstance(report.pages[0].blocks[0].blocks[0], dp.Group) assert isinstance(report.pages[0].blocks[0].blocks[1], dp.Text) assert report.pages[0].blocks[0].blocks[0].blocks[0].name == "test-id-1"
legend_bordercolor='black', legend_borderwidth=2, barmode='group', bargap=.3) # Create report r = dp.Report( dp.Page( label='Introduction', blocks=[ dp.HTML(html), "The data has been compiled over 3 semesters, for the introductory computer science class CSE 1223.", dp.Group(dp.BigNumber(heading="Number of Students", value=num_students), dp.BigNumber(heading="Class Average", value=str(class_average) + "%", change="2%", is_upward_change=True), columns=2), dp.BigNumber(heading="Pass Rate", value=str(pass_rate) + "%"), ]), dp.Page(label='Deliverables', blocks=[ 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)]),
response_rate = "80%" nps_score = "52%" report = dp.Report( dp.Text(executive_summary_pt1).format( # nps_score=nps_score, # responce_rate=response_rate, most_positive=most_positive_answers, most_negative=most_negative_answers, ), dp.Text("#### Trend Charts"), dp.Group( dp.File(file="images/largest_since_fy18_plot.png"), dp.File(file="images/smallest_since_fy18_plot.png"), columns=2, ), dp.Group( dp.File(file="images/largest_since_fy20_plot.png"), dp.File(file="images/smallest_since_fy20_plot.png"), columns=2, ), dp.Text(file="text/executive_summary_pt2.md").format( ct_site_plot=dp.File(file="images/ct_site_section_plot.png"), academic_affairs_plot=dp.File( file="images/academic_affairs_section_plot.png"), student_life_plot=dp.File(file="images/student_life_section_plot.png"), college_prep_plot=dp.File(file="images/college_prep_section_plot.png"), coaching_plot=dp.File( file="images/coaching_programming_section_plot.png"),
Now that we have a series of plots, we can create a report using Datapane. In addition to the visualizations, this report includes Datapane's `BigNumber` component to display top level KPIs, and our `DataTable` component to allow our viewers to filter, explore, and download the data themselves. Although we could create a linear list of all blocks (similar to how they are displayed in this notebook), we can build a more powerful and accessible report by using Datapane's [layout components](https://docs.datapane.com/reports/blocks/layout-pages-and-selects). From these components, we are using `Group` to place the `BigNumber` blocks in two columns, have used the `Page` block to add multiple pages to our report, and are using the `Select` block to create tabs where users can choose their dataset. This results in a beautiful, interactive document which we can publish and share. """ dp.Report( "# Finance Model", dp.Group(blocks=[ 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'),
nflx = nflx[['Open', 'Close', 'Volume']] nflx["index"] = np.arange(len(nflx)) fig8 = go.Figure( ff.create_scatterplotmatrix(nflx, diag='box', index='index', size=3, height=600, width=1150, colormap='RdBu', title={ 'text': "Netflix Stock Price (Scatterplot Matrix)", 'x': 0.5, 'xanchor': 'center' })) dp.Report( dp.Group(dp.Plot(fig0), dp.Plot(fig1), dp.Plot(fig2), dp.Plot(fig3), dp.Plot(fig4), dp.Plot(fig5), dp.Plot(fig6), dp.Plot(fig7), columns=2, rows=4), dp.Plot(fig8)).publish(name='stock_report', open=True)