def discrim_html_output_datapane(self, ProcDiscrim, fileName):
     """Création d'un reporting en format HTML pour la méthode PROC DISCRIM
     grâce à la librairie datapane.
     
     Paramètres
     ----------
     ProcDiscrim : objet LinearDiscriminantAnalysis
         objet suite à appel de la fonction fit() de la classe 
         LinearDiscriminantAnalysis
     fileName : string
         nom du fichier de sortie (avec ou sans .html)
     """
     if fileName[-5:] != ".html":
         fileName += ".html"
     
     ProcDiscrim._stats_dataset()
     ProcDiscrim._stats_classes()
     ProcDiscrim._stats_pooled_cov_matrix()
     ProcDiscrim._stats_wilks()
     report = dp.Report(
         dp.Text("# Linear Discriminant Analysis"),
         dp.Text("## General information about the data"),
         dp.Table(ProcDiscrim.infoDataset),
         dp.Table(ProcDiscrim.infoClasses),
         dp.Text("## Informations on the covariance matrix"),
         dp.Table(ProcDiscrim.W),
         dp.Table(ProcDiscrim.infoCovMatrix),
         dp.Text("## Function of lda and its' intercept "
                 "and coefficients"),
         dp.Table(ProcDiscrim.infoFuncClassement),
         dp.Text("## Statistics. Wilks' Lambda"),
         dp.Table(ProcDiscrim.infoWilksStats))
     
     report.save(path=fileName)
Exemple #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)
Exemple #3
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")
Exemple #4
0
def test_full_report(tmp_path: Path):
    df = gen_df()
    name = gen_name()
    description = gen_description()
    source_url = "https://github.com/datapane/datapane"
    # create a basic report
    m = dp.Text("hello world!!")

    # Asset tests
    lis = [1, 2, 3]
    json_list: str = json.dumps(lis)
    plot = gen_plot()

    # create the DP
    fn = tmp_path / "json_list.json"
    fn.write_text(data=json_list)
    file_asset = dp.File(file=fn)
    json_asset = dp.File(data=json_list, is_json=True)
    plot_asset = dp.Plot(data=plot)
    list_asset = dp.File(data=lis, is_json=True)
    df_asset = dp.DataTable(df=df, caption="Our Dataframe")
    dp_report = dp.Report(m, file_asset, df_asset, json_asset, plot_asset, list_asset)
    dp_report.upload(name=name, description=description, source_url=source_url)

    with deletable(dp_report):
        # are the fields ok
        check_name(dp_report, name)
        assert dp_report.description == description
        assert dp_report.source_url == source_url
        assert len(dp_report.pages[0].blocks[0].blocks) == 6
Exemple #5
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")
Exemple #6
0
def __test_gen_report_id_check():
    """Test case unused atm"""
    # all fresh
    report = dp.Report(md_block, md_block, md_block)
    assert_report(report)  # expected_id_count=5)
    # 2 fresh
    report = dp.Report(md_block, md_block_id, md_block)
    assert_report(report)  # expected_id_count=4)
    # 0 fresh
    report = dp.Report(md_block_id, dp.Text("test", name="test-2"))
    assert_report(report)  # expected_id_count=2)
Exemple #7
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"))
Exemple #8
0
def test_textreport_gen():
    """Test TextReport API and id/naming handling"""
    s_df = gen_df()

    # Simple
    report = dp.TextReport("Text-3")
    assert_text_report(report, 1)

    # multiple blocks
    report = dp.TextReport("Text-1", "Text-2", s_df)
    assert_text_report(report, 3)

    # empty
    report = dp.TextReport()
    assert_text_report(report, 0)

    # mixed naming usage
    report = dp.TextReport("text-1", dp.Text("Text-4", name="test"))
    assert_text_report(report, 2)

    # arg/kwarg naming tests
    report = dp.TextReport(
        dp.Text("Text-arg-1"),
        dp.Text("Text-arg-2", name="text-arg-2"),
        t1="Text-1",
        t2=dp.Text("Text-2"),
        t3=dp.Text("Text-3", name="overwritten"),
    )
    assert_text_report(report, 5, ["text-1", "text-arg-2", "t1", "t2", "t3"])

    # dict/list test
    report = dp.TextReport(
        blocks=dict(t1="text-1",
                    t2=dp.Text("Text-2"),
                    t3=dp.Text("Text-3", name="overwritten")))
    assert_text_report(report, 3, ["t1", "t2", "t3"])
    report = dp.TextReport(
        blocks=["Text-1",
                dp.Text("Text-2"),
                dp.Text("Text-3", name="text-3")])
    assert_text_report(report, 3, ["text-1", "text-2", "text-3"])
Exemple #9
0
from glom import glom
from lxml import etree
from lxml.etree import DocumentInvalid

import datapane as dp
from datapane.client import DPError
from datapane.client.api.report.blocks import BaseElement
from datapane.client.api.report.core import BuilderState
from datapane.common.report import validate_report_doc

from ...e2e.common import gen_df, gen_plot

################################################################################
# Helpers
md_block_id = dp.Text(
    text="# Test markdown block <hello/> \n Test **content**",
    name="test-id-1")
md_block = dp.Text(text="# Test markdown block <hello/> \n Test **content**")
str_md_block = "Simple string Markdown"


def element_to_str(e: BaseElement) -> str:
    s = e._to_xml(BuilderState())
    return etree.tounicode(s.elements[0], pretty_print=True)


def num_blocks(report_str: str) -> int:
    x = "count(/Report/Main//*)"
    return int(etree.fromstring(report_str).xpath(x))

Exemple #10
0
    print("Premium:\t{0:+.2f} %".format((price / nav_per_share - 1) * 100, currency.symbol))

    rows += [{
        'ticker': ETHC_ticker,
        'currency': currency.symbol,
        'nav': nav,
        'nav_per_share': nav_per_share,
        'share_price': price,
        'shares_outstanding': shares_outstanding,
        'premium': (price / nav_per_share - 1) * 100
    }]

df = pd.DataFrame(rows)
r = dp.Report(
    f'# Ether Capital Corp. NAV',
    dp.Text(f'Updated {now}'),
    f'### Holdings',
    dp.Table(pd.DataFrame(current_holdings).iloc[1:]),
    f'### Share Price Premium',
    dp.Table(df),
    f'The maximum discount is {-df["premium"].min():.1f} %' \
            if df["premium"].mean() < 0 else
            f'The maximum premium is {df["premium"].max():+.1f} %'
)

r.save(
    path='report.html',
    name=f'Ether Capital Corp. NAV',
    open=False
)
Exemple #11
0
    "data/interim/most_negative_answers.pkl")
most_negative_answers = most_negative_answers.style.format("{:.0%}")

most_positive_sites = pd.read_pickle("data/interim/most_positive_sites.pkl")
most_positive_sites = list(most_positive_sites.index)
most_positive_sites_string = ", ".join(
    most_positive_sites[:-2] + [", and ".join(most_positive_sites[-2:])])

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"),
Exemple #12
0
# pip install datapane
# pip install plotly

import datapane as dp
import pandas as pd
import numpy as np
import plotly.express as px

# Scripts to create df and chart
df = px.data.gapminder()

chart = px.scatter(df.query("year==2007"),
                   x="gdpPercap",
                   y="lifeExp",
                   size="pop",
                   color="continent",
                   hover_name="country",
                   log_x=True,
                   size_max=60)

# Once you have the df and the chart, simply use
r = dp.Report(
    dp.Text("my simple report"),  # add description
    dp.DataTable(df),  # create a table
    dp.Plot(chart)  # create a chart
)

# Publish your report
r.publish(name='example', visibility=dp.Visibility.PUBLIC)
Exemple #13
0
import pandas as pd
import datapane as dp

# basic report creation, with params
df = pd.DataFrame.from_dict({"x": [4, 3, 2, 1], "y": [10.5, 20.5, 30.5, 40.5]})
blocks = [dp.Text(f"Dummy Markdown block - {dp.Params['p1']}"), dp.DataTable(df)]

# test running as main or by datapane runner
if dp.on_datapane:
    print("on datapane")
if __name__ == "__datapane__":  # same as dp.by_datapane
    print("by datapane")
    report = dp.Report(blocks=blocks)
    report.publish(name="dp_report", description="Description")
Exemple #14
0
              popup='Timberline Lodge',
              icon=folium.Icon(color='green')).add_to(m)
folium.Marker(location=[45.3300, -121.6823],
              popup='Some Other Location',
              icon=folium.Icon(color='red', icon='info-sign')).add_to(m)
folium_asset = dp.Plot(data=m)

# Plotly
fig = go.Figure()
fig.add_trace(go.Scatter(x=[0, 1, 2, 3, 4, 5], y=[1.5, 1, 1.3, 0.7, 0.8, 0.9]))
fig.add_trace(go.Bar(x=[0, 1, 2, 3, 4, 5], y=[1, 0.5, 0.7, -1.2, 0.3, 0.4]))
plotly_asset = dp.Plot(data=fig)

# Markdown
md_block = dp.Text(
    text=
    f"# Test markdown block with env var: {os.environ['ENV_VAR']} \n Test **content**"
)

# In-line JSON
list_asset = dp.File(data=lis, is_json=True)

# Downloadable file
file_asset = dp.File(data=lis)

# In-line image
img_asset = dp.File(file=Path("./datapane-logo.png"))

# Vega
vega_asset = dp.Plot(data=gen_plot())

# Table
Exemple #15
0
@author: ryan
"""

# %% Doc setup
import datapane as dp
import sidetable as stb

from geo import m
from profiles import Arashiyama_df, Chayama_df, Demachiyanagi_df, Hanazono_df, Kokusaikaikan_df, izakaya_df, restaurants, station_groupby_df

# %% Report

rprt = dp.Report(
    dp.Text("""
# 京都, in Stations and Their Restaurants
### Exploration of restaurants in Kyoto and the stations they're closest to
Splitting up Kyoto restaurants by regions centered on train/subway stations, as well as checking out some interesting characteristics of the different restaurants near each station. As it turns out, there's a ton of izakaya in the city
---
"""), dp.Plot(m),
    dp.Text("""
## Dataset
The dataset used for the current project can be found [here](https://www.kaggle.com/koki25ando/tabelog-restaurant-review-dataset). Station data was pulled manually from wikipedia and google maps.
"""), dp.DataTable(restaurants),
    dp.Text("""
## Some Special Station Profiles
*Taking a look at some stations which have interesting or peculiar restaurant selection*
"""), dp.DataTable(station_groupby_df),
    dp.Text("""
### Arashiyama
#### Lots (relatively, at least) of tofu options, beware of expensive lunch prices
		"""), dp.DataTable(Arashiyama_df),
    dp.Text("""
Exemple #16
0
import os
import pandas as pd
import datapane as dp

# basic report creation, with params
df = pd.DataFrame.from_dict({"x": [4, 3, 2, 1], "y": [10.5, 20.5, 30.5, 40.5]})
blocks = [dp.Text(f"Dummy Markdown block - {dp.Params['p1']}"), dp.DataTable(df), dp.Text(f"Text block with env var: {os.environ['ENV_VAR']}")]

# test running as main or by datapane runner
if dp.ON_DATAPANE:
    print("on datapane")
if __name__ == "__datapane__":  # same as dp.by_datapane
    print("by datapane")
    report = dp.Report(blocks=blocks)
    report.upload(name="dp_report", description="Description")
Exemple #17
0
# Plotly
fig = go.Figure()
fig.add_trace(
    go.Scatter(
        x=[0, 1, 2, 3, 4, 5],
        y=[1.5, 1, 1.3, 0.7, 0.8, 0.9]
    ))
fig.add_trace(
    go.Bar(
        x=[0, 1, 2, 3, 4, 5],
        y=[1, 0.5, 0.7, -1.2, 0.3, 0.4]
    ))
plotly_asset = dp.Plot(data=fig)

# Markdown
md_block = dp.Text(text="# Test markdown block \n Test **content**")

# In-line JSON
list_asset = dp.File(data=lis, is_json=True)

# Downloadable file
file_asset = dp.File(data=lis)

# In-line image
img_asset = dp.File(file=Path("./datapane-logo.png"))

# Vega
vega_asset = dp.Plot(data=alt.Chart(gen_df()).mark_line().encode(x="x", y="y"))

# Table
df_table_asset = dp.Table(gen_df())