Пример #1
0
def test_gene002_arbitrary_resources(dash_duo):
    app = Dash(__name__)

    app.layout = Div([Button(id="btn"), Div(id="container")])

    @app.callback(Output("container", "children"), [Input("btn", "n_clicks")])
    def update_container(n_clicks):
        if n_clicks is None:
            raise PreventUpdate

        return StyledComponent(id="styled",
                               value="Styled",
                               style={"font-family": "godfather"})

    dash_duo.start_server(app)

    assert (dash_duo.driver.execute_script(
        "return document.fonts.check('1em godfather')") is False)

    dash_duo.wait_for_element("#btn").click()
    assert dash_duo.wait_for_element("#styled").text == "Styled"

    WebDriverWait(dash_duo.driver, 10).until(
        lambda _: dash_duo.driver.execute_script(
            "return document.fonts.check('1em godfather')") is True, )

    dash_duo.percy_snapshot(name="gene002-arbitrary-resource")
Пример #2
0
def get_app():
    app = dash.Dash(__name__)

    columns = [{"name": i, "id": i} for i in ["a", "b"]]
    data = [dict(a=1, b=2), dict(a=11, b=22)]

    app.layout = Div(
        [
            Button(id="clear-table", children=["Clear table"]),
            DataTable(id="table", columns=columns, data=data),
        ]
    )

    @app.callback(
        [Output("table", "data"), Output("table", "columns")],
        [Input("clear-table", "n_clicks")],
    )
    def clear_table(n_clicks):
        if n_clicks is None:
            raise PreventUpdate

        nonlocal columns, data

        return (data, columns) if n_clicks % 2 == 0 else (None, None)

    return app
Пример #3
0
def szng003_on_prop_change_impl(
    test, fixed_columns, fixed_rows, merge_duplicate_headers, callback_props
):
    props = {**base_props, **fixed_columns, **fixed_rows, **merge_duplicate_headers}

    table = DataTable(**props, id="table")

    app = dash.Dash(__name__)
    app.layout = Div([Button(id="btn", children=["Update"]), table])

    @app.callback(
        [Output("table", key) for key in callback_props.keys()],
        [Input("btn", "n_clicks")],
        prevent_initial_call=True,
    )
    def callback(n_clicks):
        return [callback_props.get(key) for key in callback_props.keys()]

    test.start_server(app)

    target = test.driver.find_element_by_css_selector("#table")
    cells_are_same_width(target, target)

    test.driver.find_element_by_css_selector("#btn").click()
    cells_are_same_width(target, target)

    assert test.get_log_errors() == []
Пример #4
0
def get_app(props=dict(), special=False, clear=False):
    app = dash.Dash(__name__)

    baseProps = get_props(rows=200)

    if special:
        for c in baseProps.get("columns"):
            if c["id"] == "bbb":
                c["id"] = "b+bb"
            elif c["id"] == "ccc":
                c["id"] = "c cc"
            elif c["id"] == "ddd":
                c["id"] = "d:dd"
            elif c["id"] == "eee":
                c["id"] = "e-ee"
            elif c["id"] == "fff":
                c["id"] = "f_ff"
            elif c["id"] == "ggg":
                c["id"] = "g.gg"

        for i in range(len(baseProps["data"])):
            d = baseProps["data"][i]
            d["b+bb"] = d["bbb"]
            d["c cc"] = d["ccc"]
            d["d:dd"] = d["ddd"]
            d["e-ee"] = d["eee"]
            d["f_ff"] = d["fff"]
            d["g.gg"] = d["ggg"]

    baseProps.update(dict(filter_action="native"))
    baseProps.update(props)

    if clear:
        app.layout = Div([
            DataTable(**baseProps),
            Button(id="btn", children=["Clear filters"])
        ])

        @app.callback(Output("table", "filter_query"),
                      Input("btn", "n_clicks"))
        def get_filter(n_clicks):
            return ""

    else:
        app.layout = DataTable(**baseProps)

    return app
Пример #5
0
def test_szng001_widths_on_style_change(test):
    base_props = dict(
        data=[
            {"a": 85, "b": 601, "c": 891},
            {"a": 967, "b": 189, "c": 514},
            {"a": 398, "b": 262, "c": 743},
            {"a": 89, "b": 560, "c": 582},
            {"a": 809, "b": 591, "c": 511},
        ],
        columns=[
            {"id": "a", "name": "A"},
            {"id": "b", "name": "B"},
            {"id": "c", "name": "C"},
        ],
        style_data={
            "width": 100,
            "minWidth": 100,
            "maxWidth": 100,
            "border": "1px solid blue",
        },
        row_selectable="single",
        row_deletable=True,
    )

    styles = [
        dict(
            style_table=dict(
                width=500, minWidth=500, maxWidth=500, paddingBottom=10, display="none"
            )
        ),
        dict(style_table=dict(width=500, minWidth=500, maxWidth=500, paddingBottom=10)),
        dict(style_table=dict(width=750, minWidth=750, maxWidth=750, paddingBottom=10)),
        dict(
            style_table=dict(
                width=750, minWidth=750, maxWidth=750, paddingBottom=10, display="none"
            )
        ),
        dict(style_table=dict(width=350, minWidth=350, maxWidth=350, paddingBottom=10)),
    ]

    fixes = [
        dict(),
        dict(fixed_columns=dict(headers=True)),
        dict(fixed_rows=dict(headers=True)),
        dict(fixed_columns=dict(headers=True), fixed_rows=dict(headers=True)),
        dict(fixed_columns=dict(headers=True, data=1)),
        dict(fixed_rows=dict(headers=True, data=1)),
        dict(
            fixed_columns=dict(headers=True, data=1),
            fixed_rows=dict(headers=True, data=1),
        ),
    ]

    variations = []
    style = styles[0]
    i = 0
    for fix in fixes:
        variations.append({**style, **fix, **base_props, "id": "table{}".format(i)})
        i = i + 1

    variations_range = range(0, len(variations))

    tables = [DataTable(**variation) for variation in variations]

    app = dash.Dash(__name__)
    app.layout = Div(
        children=[
            Button(id="btn", children="Click me"),
            Div(
                [
                    DataTable(
                        **base_props,
                        id="table{}".format(width),
                        style_table=dict(
                            width=width,
                            minWidth=width,
                            maxWidth=width,
                            paddingBottom=10,
                        )
                    )
                    for width in [350, 500, 750]
                ]
            ),
            Div(tables),
        ]
    )

    @app.callback(
        [Output("table{}".format(i), "style_table") for i in variations_range],
        [Input("btn", "n_clicks")],
        prevent_initial_call=True,
    )
    def update_styles(n_clicks):
        if n_clicks < len(styles):
            style_table = styles[n_clicks]["style_table"]
            return [style_table for i in variations_range]
        else:
            raise PreventUpdate

    test.start_server(app)

    for style in styles:
        display = style.get("style_table", dict()).get("display")
        width = style.get("style_table", dict()).get("width")
        target = (
            test.driver.find_element_by_css_selector("#table{}".format(width))
            if display != "none"
            else None
        )

        for variation in variations:
            table = test.driver.find_element_by_css_selector(
                "#{}".format(variation["id"])
            )
            if target is None:
                assert table is not None
                assert (
                    test.driver.execute_script(
                        "return getComputedStyle(document.querySelector('#{} .dash-spreadsheet-container')).display".format(
                            variation["id"]
                        )
                    )
                    == "none"
                )
            else:
                cells_are_same_width(target, table)

        test.driver.find_element_by_css_selector("#btn").click()

    assert test.get_log_errors() == []