Exemple #1
0
def test_rdmo004_multi_output_circular_dependencies(dash_duo):
    app = Dash(__name__)
    app.layout = html.Div(
        [dcc.Input(id="a"),
         dcc.Input(id="b"),
         html.P(id="c")])

    @app.callback(Output("a", "value"), [Input("b", "value")])
    def set_a(b):
        return ((b or "") + "X")[:100]

    @app.callback(
        [Output("b", "value"), Output("c", "children")], [Input("a", "value")])
    def set_bc(a):
        return [a, a]

    dash_duo.start_server(
        app,
        debug=True,
        use_debugger=True,
        use_reloader=False,
        dev_tools_hot_reload=False,
    )

    # the UI still renders the output triggered by callback.
    # The new system does NOT loop infinitely like it used to, each callback
    # is invoked no more than once.
    dash_duo.wait_for_text_to_equal("#c", "X")

    err_text = dash_duo.find_element("span.dash-fe-error__title").text
    assert err_text == "Circular Dependencies"
Exemple #2
0
def highlight_controls(uuid: str, tab: str) -> html.Div:
    return html.Div(
        style={"margin-top": "10px"},
        children=[
            html.Label("Data highlight criterias", className="webviz-underlined-label"),
            html.Div(
                children=[
                    wcc.Label("Absolute diff (%) above:"),
                    dcc.Input(
                        id={"id": uuid, "tab": tab, "selector": "Accept value"},
                        type="number",
                        required=True,
                        value=5,
                        persistence=True,
                        persistence_type="session",
                    ),
                ],
            ),
            html.Div(
                children=[
                    wcc.Label("Ignore response values below:"),
                    dcc.Input(
                        id={"id": uuid, "tab": tab, "selector": "Ignore <"},
                        type="number",
                        required=True,
                        value=0,
                        persistence=True,
                        persistence_type="session",
                        debounce=True,
                    ),
                ]
            ),
        ],
    )
 def matrix_element(idx, value=0):
     # TODO: maybe move element out of the name
     mid = self.id(kwarg_label, is_kwarg=True, idx=idx, hint=shape)
     if isinstance(value, np.ndarray):
         value = value.item()
     if not is_int:
         return dcc.Input(
             id=mid,
             inputMode="numeric",
             debounce=True,
             className="input",
             style=style,
             value=float(value) if value is not None else None,
             persistence=True,
             type="number",
             **kwargs,
         )
     else:
         return dcc.Input(
             id=mid,
             inputMode="numeric",
             debounce=True,
             className="input",
             style=style,
             value=int(value) if value is not None else None,
             persistence=True,
             type="number",
             step=1,
             **kwargs,
         )
Exemple #4
0
def ninput_app():
    app = Dash(__name__)
    app.layout = html.Div([
        dcc.Input(
            id="input_false",
            type="number",
            debounce=False,
            placeholder="debounce=False",
        ),
        html.Div(id="div_false"),
        html.Hr(id="silent-break-line"),
        dcc.Input(
            id="input_true",
            type="number",
            debounce=True,
            placeholder="debounce=True",
        ),
        html.Div(id="div_true"),
    ])

    @app.callback(
        [Output("div_false", "children"),
         Output("div_true", "children")],
        [Input("input_false", "value"),
         Input("input_true", "value")],
    )
    def render(fval, tval):
        return fval, tval

    yield app
 def well_options(self) -> html.Div:
     return html.Div(
         style={
             "marginLeft": "20px",
             "marginRight": "0px",
             "marginBotton": "0px"
         },
         children=[
             html.Div(children=html.Label(children=[
                 html.Span("Sampling:", style={"font-weight": "bold"}),
                 dcc.Input(
                     id=self.ids("sampling"),
                     debounce=True,
                     type="number",
                     required=True,
                     value=self.sampling,
                     persistence=True,
                     persistence_type="session",
                 ),
             ])),
             html.Div(children=html.Label(children=[
                 html.Span("Nextend:", style={"font-weight": "bold"}),
                 dcc.Input(
                     id=self.ids("nextend"),
                     debounce=True,
                     type="number",
                     required=True,
                     value=self.nextend,
                     persistence=True,
                     persistence_type="session",
                 ),
             ])),
         ],
     )
Exemple #6
0
def get_controls(id, desc, range, default_weight=0.0):  # pylint: disable=redefined-builtin,redefined-outer-name
    """Get controls for one variable.

    This includes
     * the description
     * range
     * weight
    """
    label = dcc.Input(id=id + "_label",
                      type='text',
                      value=desc,
                      className="label")
    range_low = dcc.Input(id=id + "_low",
                          type='number',
                          value=range[0],
                          className="range")
    range_high = dcc.Input(id=id + "_high",
                           type='number',
                           value=range[1],
                           className="range")
    slider = dcc.Slider(id=id + "_weight",
                        min=weight_range[0],
                        max=weight_range[1],
                        value=default_weight,
                        step=0.01,
                        className="slider")
    #grid = dcc.Input(id=id + "_grid", type='number', value=ngrid)
    return html.Tr([
        html.Td(label),
        html.Td([range_low, html.Span('to'), range_high]),
        html.Td([html.Span(slider),
                 html.Span('', id=id + "_weight_label")])
    ],
                   id=id + "_tr")
Exemple #7
0
def test_debc028_set_random_id():
    app = Dash(__name__)

    input1 = dcc.Input(value="Hello Input 1")
    input2 = dcc.Input(value="Hello Input 2")
    output1 = html.Div()
    output2 = html.Div()
    output3 = html.Div(id="output-3")

    app.layout = html.Div([input1, input2, output1, output2, output3])

    @app.callback(Output(output1, "children"), Input(input1, "value"))
    def update(v):
        return f"Input 1 {v}"

    @app.callback(Output(output2, "children"), Input(input2, "value"))
    def update(v):
        return f"Input 2 {v}"

    @app.callback(
        Output(output3, "children"), Input(input1, "value"), Input(input2, "value")
    )
    def update(v1, v2):
        return f"Output 3 - Input 1: {v1}, Input 2: {v2}"

    # Verify the auto-generated IDs are stable
    assert output1.id == "e3e70682-c209-4cac-629f-6fbed82c07cd"
    assert input1.id == "82e2e662-f728-b4fa-4248-5e3a0a5d2f34"
    assert output2.id == "d4713d60-c8a7-0639-eb11-67b367a9c378"
    assert input2.id == "23a7711a-8133-2876-37eb-dcd9e87a1613"
    # we make sure that the if the id is set explicitly, then it is not replaced by random id
    assert output3.id == "output-3"
Exemple #8
0
def test_inbs001_all_types(dash_dcc):
    def input_id(type_):
        return f"input_{type_}"

    app = Dash(__name__)
    app.layout = html.Div([
        dcc.Input(id=input_id(_), type=_, placeholder=f"input type {_}")
        for _ in ALLOWED_TYPES
    ] + [html.Div(id="output")] + [
        dcc.Input(id=input_id(_) + "2", type=_, placeholder=f"input type {_}")
        for _ in ALLOWED_TYPES
    ])

    @app.callback(
        Output("output", "children"),
        [Input(input_id(_), "value") for _ in ALLOWED_TYPES],
    )
    def cb_render(*vals):
        return " | ".join((val for val in vals if val))

    dash_dcc.start_server(app)

    assert (dash_dcc.find_element("#input_hidden").get_attribute("type") ==
            "hidden"), "hidden input element should present with hidden type"

    for atype in ALLOWED_TYPES[:-1]:
        dash_dcc.find_element(f"#input_{atype}").send_keys(
            f"test intp001 - input[{atype}]")

    with pytest.raises(WebDriverException):
        dash_dcc.find_element("#input_hidden").send_keys("no interaction")

    dash_dcc.percy_snapshot("inbs001 - dcc callback output rendering")

    assert dash_dcc.get_logs() == []
Exemple #9
0
def test_cbmt009_branched_chain(dash_duo):
    app = Dash(__name__)
    app.layout = html.Div(
        [
            dcc.Input(id="grandparent", value="input 1"),
            dcc.Input(id="parent-a"),
            dcc.Input(id="parent-b"),
            html.Div(id="child-a"),
            html.Div(id="child-b"),
        ]
    )
    call_counts = {
        "parent-a": Value("i", 0),
        "parent-b": Value("i", 0),
        "child-a": Value("i", 0),
        "child-b": Value("i", 0),
    }

    @app.callback(Output("parent-a", "value"), Input("grandparent", "value"))
    def update_parenta(value):
        call_counts["parent-a"].value += 1
        return "a: {}".format(value)

    @app.callback(Output("parent-b", "value"), Input("grandparent", "value"))
    def update_parentb(value):
        time.sleep(0.2)
        call_counts["parent-b"].value += 1
        return "b: {}".format(value)

    @app.callback(
        Output("child-a", "children"),
        Input("parent-a", "value"),
        Input("parent-b", "value"),
    )
    def update_childa(parenta_value, parentb_value):
        time.sleep(0.5)
        call_counts["child-a"].value += 1
        return "{} + {}".format(parenta_value, parentb_value)

    @app.callback(
        Output("child-b", "children"),
        Input("parent-a", "value"),
        Input("parent-b", "value"),
        Input("grandparent", "value"),
    )
    def update_childb(parenta_value, parentb_value, grandparent_value):
        call_counts["child-b"].value += 1
        return "{} + {} + {}".format(parenta_value, parentb_value, grandparent_value)

    dash_duo.start_server(app)

    dash_duo.wait_for_text_to_equal("#child-a", "a: input 1 + b: input 1")
    dash_duo.wait_for_text_to_equal("#child-b", "a: input 1 + b: input 1 + input 1")
    dash_duo.wait_for_text_to_equal("#parent-a", "a: input 1")
    dash_duo.wait_for_text_to_equal("#parent-b", "b: input 1")
    assert call_counts["parent-a"].value == 1
    assert call_counts["parent-b"].value == 1
    assert call_counts["child-a"].value == 1
    assert call_counts["child-b"].value == 1
Exemple #10
0
def test_msai001_auto_id_assert(dash_dcc):
    app = Dash(__name__)

    input1 = dcc.Input(value="Hello Input 1")
    input2 = dcc.Input(value="Hello Input 2")
    input3 = dcc.Input(value=3)
    output1 = html.Div()
    output2 = html.Div()
    output3 = html.Div(id="output-3")
    slider = dcc.Slider(0, 10, value=9)

    app.layout = html.Div(
        [input1, input2, output1, output2, output3, input3, slider])

    @app.callback(Output(output1, "children"), Input(input1, "value"))
    def update(v):
        return f"Output1: Input1={v}"

    @app.callback(Output(output2, "children"), Input(input2, "value"))
    def update(v):
        return f"Output2: Input2={v}"

    @app.callback(Output("output-3", "children"), Input(input1, "value"),
                  Input(input2, "value"))
    def update(v1, v2):
        return f"Output3: Input1={v1}, Input2={v2}"

    @app.callback(Output(slider, "value"), Input(input3, "value"))
    def update(v):
        return v

    # Verify the auto-generated IDs are stable
    assert output1.id == "e3e70682-c209-4cac-629f-6fbed82c07cd"
    assert input1.id == "82e2e662-f728-b4fa-4248-5e3a0a5d2f34"
    assert output2.id == "d4713d60-c8a7-0639-eb11-67b367a9c378"
    assert input2.id == "23a7711a-8133-2876-37eb-dcd9e87a1613"
    # we make sure that the if the id is set explicitly, then it is not replaced by random id
    assert output3.id == "output-3"

    dash_dcc.start_server(app)

    def escape_id(dep):
        _id = dep.id
        if _id[0] in "0123456789":
            _id = "\\3" + _id[0] + " " + _id[1:]
        return "#" + _id

    dash_dcc.wait_for_element(".rc-slider")
    dash_dcc.find_element(escape_id(input1))
    dash_dcc.find_element(escape_id(input2))
    dash_dcc.wait_for_text_to_equal(escape_id(output1),
                                    "Output1: Input1=Hello Input 1")
    dash_dcc.wait_for_text_to_equal(escape_id(output2),
                                    "Output2: Input2=Hello Input 2")
    dash_dcc.wait_for_text_to_equal(
        escape_id(output3),
        "Output3: Input1=Hello Input 1, Input2=Hello Input 2")
Exemple #11
0
    def intersection_option(self) -> html.Div:
        options = [
            {
                "label": "Keep zoom state",
                "value": "keep_zoom_state"
            },
            {
                "label": "Show surface fill",
                "value": "show_surface_fill"
            },
        ]
        value = ["show_surface_fill"]
        if self.segyfiles:
            options.append({"label": "Show seismic", "value": "show_seismic"})
        if self.zonelog is not None:
            options.append({"label": "Show zonelog", "value": "show_zonelog"})
            value.append("show_zonelog")

        return html.Div(
            style=self.set_style(marginTop="20px"),
            children=[
                html.Label("Intersection settings",
                           style={"font-weight": "bold"}),
                html.Label("Sampling"),
                dcc.Input(
                    id=self.ids("sampling"),
                    debounce=True,
                    type="number",
                    required=True,
                    value=self.sampling,
                    persistence=True,
                    persistence_type="session",
                ),
                html.Label("Extension"),
                dcc.Input(
                    id=self.ids("nextend"),
                    debounce=True,
                    type="number",
                    required=True,
                    value=self.nextend,
                    persistence=True,
                    persistence_type="session",
                ),
                dcc.Checklist(
                    id=self.ids("options"),
                    options=options,
                    value=value,
                    persistence=True,
                    persistence_type="session",
                ),
            ],
        )
Exemple #12
0
def test_rdsi002_event_properties_state_and_inputs(dash_duo):
    app = Dash(__name__)
    app.layout = html.Div([
        html.Button("Click Me", id="button"),
        dcc.Input(value="Initial Input", id="input"),
        dcc.Input(value="Initial State", id="state"),
        html.Div(id="output"),
    ])

    call_count = Value("i", 0)

    @app.callback(
        Output("output", "children"),
        [Input("input", "value"),
         Input("button", "n_clicks")],
        [State("state", "value")],
    )
    def update_output(input, n_clicks, state):
        call_count.value += 1
        return 'input="{}", state="{}"'.format(input, state)

    dash_duo.start_server(app)

    def btn():
        return dash_duo.find_element("#button")

    def output():
        return dash_duo.find_element("#output")

    assert (output().text == 'input="Initial Input", state="Initial State"'
            ), "callback gets called with initial input"

    btn().click()
    wait.until(lambda: call_count.value == 2, timeout=1)
    assert (output().text == 'input="Initial Input", state="Initial State"'
            ), "button click doesn't count on output"

    dash_duo.find_element("#input").send_keys("x")
    wait.until(lambda: call_count.value == 3, timeout=1)

    assert (output().text == 'input="Initial Inputx", state="Initial State"'
            ), "output get updated with key `x`"

    dash_duo.find_element("#state").send_keys("z")
    time.sleep(0.5)
    assert call_count.value == 3, "state not trigger callback with 0.5 wait"
    assert (output().text == 'input="Initial Inputx", state="Initial State"'
            ), "output remains the same as last step"

    btn().click()
    wait.until(lambda: call_count.value == 4, timeout=1)
    assert output().text == 'input="Initial Inputx", state="Initial Statez"'
Exemple #13
0
def test_state_and_inputs(dash_dcc):
    app = Dash(__name__)
    app.layout = html.Div([
        dcc.Input(value="Initial Input", id="input"),
        dcc.Input(value="Initial State", id="state"),
        html.Div(id="output"),
    ])

    call_count = Value("i", 0)

    @app.callback(
        Output("output", "children"),
        inputs=[Input("input", "value")],
        state=[State("state", "value")],
    )
    def update_output(input, state):
        call_count.value += 1
        return 'input="{}", state="{}"'.format(input, state)

    dash_dcc.start_server(app)

    input_ = dash_dcc.find_element("#input")

    # callback gets called with initial input
    wait.until(lambda: call_count.value == 1, timeout=1)

    assert dash_dcc.wait_for_text_to_equal(
        "#output", 'input="Initial Input", state="Initial State"')

    input_.send_keys("x")
    wait.until(lambda: call_count.value == 2, timeout=1)
    assert dash_dcc.wait_for_text_to_equal(
        "#output", 'input="Initial Inputx", state="Initial State"')

    dash_dcc.find_element("#state").send_keys("x")
    time.sleep(0.2)

    assert call_count.value == 2
    assert dash_dcc.wait_for_text_to_equal(
        "#output", 'input="Initial Inputx", state="Initial State"'
    ), "state value sshould not trigger callback"

    input_.send_keys("y")

    wait.until(lambda: call_count.value == 3, timeout=1)
    assert dash_dcc.wait_for_text_to_equal(
        "#output", 'input="Initial Inputxy", state="Initial Statex"'
    ), "input value triggers callback, and the last state change is kept"

    assert dash_dcc.get_logs() == []
def layout():
    if db.MONGO_CL == None:
        return html.Div([html.P("First you will need to select a DB in the Db Info menu")])
    else:
        return html.Div(
            [
                # dropdown_type,
                dropdown_site(db.DB_SITE),
                dropdown_sat(db.DB_SAT),
                dropdown_key_x(),
                    html.Div(children=[util.namedSlider(
                        name='Symbol size',
                        id='symbol_size',
                        min=1,
                        max=8,
                        step=1,
                        value=3,
                        marks={i: str(i) for i in range(1, 9)},
                    )],
                         style={'width': '20%', 'display': 'inline-block'}
                         ),

                # dropdown_key_y(),
                dcc.Input(
                    id="input_min",
                    type="number",
                    placeholder="Min Value",
                ),
                dcc.Input(
                    id="input_max",
                    type="number",
                    placeholder="Max value",
                ),
                exclude_start(),
                html.P("Color Scale"),
                dcc.Dropdown(
                    id='colorscale',
                    options=[{"value": x, "label": x}
                             for x in colorscales],
                    value='viridis'
                ),
                update_button,

                dcc.Graph(
                    id='plot_polar',
                    figure=get_empty_graph("select information first")
                )
            ]
        )
Exemple #15
0
def test_rdps011_toggle_persistence2(dash_duo):
    app = dash.Dash(__name__)
    app.layout = html.Div(
        [
            dcc.Input(id="persistence-val", value=""),
            dcc.Input(id="persisted2", value="a", persistence=""),
            html.Div(id="out"),
        ]
    )

    # this is not a good way to set persistence, as it doesn't allow you to
    # get the right initial value. Much better is to update the whole component
    # as we do in the previous test case... but it shouldn't break this way.
    @app.callback(
        Output("persisted2", "persistence"), [Input("persistence-val", "value")]
    )
    def set_persistence(val):
        return val

    @app.callback(Output("out", "children"), [Input("persisted2", "value")])
    def set_out(val):
        return val

    dash_duo.start_server(app)

    dash_duo.wait_for_text_to_equal("#out", "a")

    dash_duo.find_element("#persistence-val").send_keys("s")
    time.sleep(0.2)
    assert not dash_duo.get_logs()
    dash_duo.wait_for_text_to_equal("#out", "a")
    dash_duo.find_element("#persisted2").send_keys("pricot")
    dash_duo.wait_for_text_to_equal("#out", "apricot")

    dash_duo.find_element("#persistence-val").send_keys("2")
    dash_duo.wait_for_text_to_equal("#out", "a")
    dash_duo.find_element("#persisted2").send_keys("rtichoke")
    dash_duo.wait_for_text_to_equal("#out", "artichoke")

    # no persistence, still goes back to original value
    dash_duo.clear_input("#persistence-val")
    dash_duo.wait_for_text_to_equal("#out", "a")

    # apricot and artichoke saved
    dash_duo.find_element("#persistence-val").send_keys("s")
    dash_duo.wait_for_text_to_equal("#out", "apricot")
    dash_duo.find_element("#persistence-val").send_keys("2")
    assert not dash_duo.get_logs()
    dash_duo.wait_for_text_to_equal("#out", "artichoke")
def color_range_layout(uuid: str, map_id: str) -> wcc.FlexBox:
    return wcc.FlexBox(
        style={
            "display": "flex",
            "align-items": "center"
        },
        children=[
            wcc.Label(
                "Surface A" if map_id == "map1" else "Surface B",
                style={
                    "flex": 1,
                    "minWidth": "40px"
                },
            ),
            dcc.Input(
                id={
                    "id": uuid,
                    "colors": f"{map_id}_clip_min",
                },
                style={
                    "flex": 1,
                    "minWidth": "40px"
                },
                type="number",
                value=None,
                debounce=True,
                placeholder="Min",
                persistence=True,
                persistence_type="session",
            ),
            dcc.Input(
                id={
                    "id": uuid,
                    "colors": f"{map_id}_clip_max",
                },
                style={
                    "flex": 1,
                    "minWidth": "40px"
                },
                type="number",
                value=None,
                debounce=True,
                placeholder="Max",
                persistence=True,
                persistence_type="session",
            ),
        ],
    )
Exemple #17
0
def test_cbmt002_canceled_intermediate_callback(dash_duo):
    # see https://github.com/plotly/dash/issues/1053
    app = Dash(__name__)
    app.layout = html.Div(
        [
            dcc.Input(id="a", value="x"),
            html.Div("b", id="b"),
            html.Div("c", id="c"),
            html.Div(id="out"),
        ]
    )

    @app.callback(
        Output("out", "children"),
        [Input("a", "value"), Input("b", "children"), Input("c", "children")],
    )
    def set_out(a, b, c):
        return "{}/{}/{}".format(a, b, c)

    @app.callback(Output("b", "children"), [Input("a", "value")])
    def set_b(a):
        raise PreventUpdate

    @app.callback(Output("c", "children"), [Input("a", "value")])
    def set_c(a):
        return a

    dash_duo.start_server(app)
    dash_duo.wait_for_text_to_equal("#out", "x/b/x")
    chars = "x"
    for i in list(range(10)) * 2:
        dash_duo.find_element("#a").send_keys(str(i))
        chars += str(i)
        dash_duo.wait_for_text_to_equal("#out", "{0}/b/{0}".format(chars))
Exemple #18
0
def test_clsd014_input_output_callback(dash_duo):
    app = Dash(__name__, assets_folder="assets")

    app.layout = html.Div([
        html.Div(id="input-text"),
        dcc.Input(id="input", type="number", value=0)
    ])

    app.clientside_callback(
        ClientsideFunction(namespace="clientside",
                           function_name="input_output_callback"),
        Output("input", "value"),
        Input("input", "value"),
    )

    app.clientside_callback(
        ClientsideFunction(namespace="clientside",
                           function_name="input_output_follower"),
        Output("input-text", "children"),
        Input("input", "value"),
    )

    dash_duo.start_server(app)

    dash_duo.find_element("#input").send_keys("2")
    dash_duo.wait_for_text_to_equal("#input-text", "3")
    call_count = dash_duo.driver.execute_script("return window.callCount;")

    assert call_count == 2, "initial + changed once"

    assert dash_duo.get_logs() == []
Exemple #19
0
 def pad_output(input):
     return html.Div(
         [
             dcc.Input(id="sub-input-1", value="sub input initial value"),
             html.Div(id="sub-output-1"),
         ]
     )
Exemple #20
0
def test_cbsc016_extra_components_callback(dash_duo):
    lock = Lock()

    app = Dash(__name__)
    app._extra_components.append(dcc.Store(id="extra-store", data=123))

    app.layout = html.Div(
        [
            dcc.Input(id="input", value="initial value"),
            html.Div(html.Div([1.5, None, "string", html.Div(id="output-1")])),
        ]
    )
    store_data = Value("i", 0)

    @app.callback(
        Output("output-1", "children"),
        [Input("input", "value"), Input("extra-store", "data")],
    )
    def update_output(value, data):
        with lock:
            store_data.value = data
            return value

    dash_duo.start_server(app)

    dash_duo.wait_for_text_to_equal("#output-1", "initial value")

    input_ = dash_duo.find_element("#input")
    dash_duo.clear_input(input_)
    input_.send_keys("A")

    dash_duo.wait_for_text_to_equal("#output-1", "A")

    assert store_data.value == 123
    assert dash_duo.get_logs() == []
Exemple #21
0
def test_cbsc011_one_call_for_multiple_outputs_initial(dash_duo):
    app = Dash(__name__)
    call_count = Value("i", 0)

    app.layout = html.Div(
        [
            html.Div(
                [
                    dcc.Input(value="Input {}".format(i), id="input-{}".format(i))
                    for i in range(10)
                ]
            ),
            html.Div(id="container"),
            dcc.RadioItems(),
        ]
    )

    @app.callback(
        Output("container", "children"),
        [Input("input-{}".format(i), "value") for i in range(10)],
    )
    def dynamic_output(*args):
        call_count.value += 1
        return json.dumps(args, indent=2)

    dash_duo.start_server(app)
    dash_duo.wait_for_text_to_equal("#input-9", "Input 9")
    dash_duo.wait_for_contains_text("#container", "Input 9")

    assert call_count.value == 1
    dash_duo.percy_snapshot("test_rendering_layout_calls_callback_once_per_output")
Exemple #22
0
def test_clsd001_simple_clientside_serverside_callback(dash_duo):
    app = Dash(__name__, assets_folder="assets")

    app.layout = html.Div([
        dcc.Input(id="input"),
        html.Div(id="output-clientside"),
        html.Div(id="output-serverside"),
    ])

    @app.callback(Output("output-serverside", "children"),
                  [Input("input", "value")])
    def update_output(value):
        return 'Server says "{}"'.format(value)

    app.clientside_callback(
        ClientsideFunction(namespace="clientside", function_name="display"),
        Output("output-clientside", "children"),
        [Input("input", "value")],
    )

    dash_duo.start_server(app)

    dash_duo.wait_for_text_to_equal("#output-serverside", 'Server says "None"')
    dash_duo.wait_for_text_to_equal("#output-clientside",
                                    'Client says "undefined"')

    dash_duo.find_element("#input").send_keys("hello world")
    dash_duo.wait_for_text_to_equal("#output-serverside",
                                    'Server says "hello world"')
    dash_duo.wait_for_text_to_equal("#output-clientside",
                                    'Client says "hello world"')
Exemple #23
0
def test_cbsc011_one_call_for_multiple_outputs_initial(dash_duo):
    app = Dash(__name__)
    call_count = Value("i", 0)

    app.layout = html.Div(
        [
            html.Div(
                [
                    dcc.Input(value="Input {}".format(i), id="input-{}".format(i))
                    for i in range(10)
                ]
            ),
            html.Div(id="container"),
            dcc.RadioItems(),
        ]
    )

    @app.callback(
        Output("container", "children"),
        [Input("input-{}".format(i), "value") for i in range(10)],
    )
    def dynamic_output(*args):
        call_count.value += 1
        return json.dumps(args)

    dash_duo.start_server(app)
    dash_duo.wait_for_text_to_equal("#input-9", "Input 9")
    dash_duo.wait_for_contains_text("#container", "Input 9")

    assert call_count.value == 1
    inputs = [f'"Input {i}"' for i in range(10)]
    expected = f'[{", ".join(inputs)}]'
    dash_duo.wait_for_text_to_equal("#container", expected)
    assert dash_duo.get_logs() == []
Exemple #24
0
def get_searchbar(query=None):
    # type: (Optional[str]) -> html.Div
    '''
    Get a row of elements used for querying Shekels data.

    Args:
        query (str, optional): Query string. Default: None.

    Returns:
        Div: Div with query field and buttons.
    '''
    if query is None:
        query = 'select * from data'

    spacer = html.Div(className='col spacer')
    query = dcc.Input(
        id='query',
        className='col query',
        value=query,
        placeholder='SQL query that uses "FROM data"',
        type='text',
        autoFocus=True,
        debounce=True
    )

    search = get_button('search')
    init = get_button('init')
    update = get_button('update')

    row = html.Div(
        className='row',
        children=[query, spacer, search, spacer, init, spacer, update],
    )
    searchbar = html.Div(id='searchbar', className='menubar', children=[row])
    return searchbar
Exemple #25
0
    def _make_search_box(self, search_term=None):

        search_field = dcc.Input(
            id=self.id("input"),
            className="input",
            type="text",
            value=search_term,
            placeholder="Enter a formula or mp-id…",
        )
        search_button = Button(
            [Icon(kind="search"), html.Span(), "Search"],
            kind="primary",
            id=self.id("button"),
        )
        search = Field(
            [Control(search_field),
             Control(search_button)],
            addons=True,
            style={"marginBottom": "0"},
        )

        return html.Div([
            html.Label("Search Materials Project:", className="mpc-label"),
            search
        ])
Exemple #26
0
def test_incs001_csp_hashes_inline_scripts(dash_duo, add_hashes,
                                           hash_algorithm, expectation):
    app = Dash(__name__)

    app.layout = html.Div([
        dcc.Input(id="input_element", type="text"),
        html.Div(id="output_element")
    ])

    app.clientside_callback(
        """
        function(input) {
            return input;
        }
        """,
        Output("output_element", "children"),
        [Input("input_element", "value")],
    )

    with expectation:
        csp = {
            "default-src":
            "'self'",
            "script-src": ["'self'"] +
            (app.csp_hashes(hash_algorithm) if add_hashes else []),
        }

        flask_talisman.Talisman(app.server,
                                content_security_policy=csp,
                                force_https=False)

        dash_duo.start_server(app)

        dash_duo.find_element("#input_element").send_keys("xyz")
        assert dash_duo.wait_for_element("#output_element").text == "xyz"
Exemple #27
0
def test_rdrc001_race_conditions(dash_duo, permuted_str):
    endpoints = permuted_str.split(",")
    app = Dash(__name__)
    app.layout = html.Div([
        html.Div("Hello world", id="output"),
        dcc.Input(id="input", value="initial value"),
    ])

    @app.callback(Output("output", "children"), Input("input", "value"))
    def update(value):
        return value

    def delay():
        for i, route in enumerate(endpoints):
            if route in flask.request.path:
                time.sleep((DELAY_TIME * i) + DELAY_TIME)

    app.server.before_request(delay)
    dash_duo.start_server(app)

    dash_duo.wait_for_text_to_equal("#output",
                                    "initial value",
                                    timeout=DELAY_TIME * (len(endpoints) + 3) +
                                    3)

    assert not dash_duo.get_logs()
Exemple #28
0
 def make_input(persistence, n):
     return dcc.Input(
         id={"i": n, "id": "persisted"},
         className="persisted",
         value="a",
         persistence=persistence,
     )
Exemple #29
0
def test_cbmt008_direct_chain(dash_duo):
    app = Dash(__name__)
    app.layout = html.Div(
        [
            dcc.Input(id="input-1", value="input 1"),
            dcc.Input(id="input-2"),
            html.Div("test", id="output"),
        ]
    )

    call_counts = {"output": Value("i", 0), "input-2": Value("i", 0)}

    @app.callback(Output("input-2", "value"), Input("input-1", "value"))
    def update_input(input1):
        call_counts["input-2"].value += 1
        return "<<{}>>".format(input1)

    @app.callback(
        Output("output", "children"),
        Input("input-1", "value"),
        Input("input-2", "value"),
    )
    def update_output(input1, input2):
        call_counts["output"].value += 1
        return "{} + {}".format(input1, input2)

    dash_duo.start_server(app)

    dash_duo.wait_for_text_to_equal("#input-1", "input 1")
    dash_duo.wait_for_text_to_equal("#input-2", "<<input 1>>")
    dash_duo.wait_for_text_to_equal("#output", "input 1 + <<input 1>>")
    assert call_counts["output"].value == 1
    assert call_counts["input-2"].value == 1

    dash_duo.find_element("#input-1").send_keys("x")
    dash_duo.wait_for_text_to_equal("#input-1", "input 1x")
    dash_duo.wait_for_text_to_equal("#input-2", "<<input 1x>>")
    dash_duo.wait_for_text_to_equal("#output", "input 1x + <<input 1x>>")
    assert call_counts["output"].value == 2
    assert call_counts["input-2"].value == 2

    dash_duo.find_element("#input-2").send_keys("y")
    dash_duo.wait_for_text_to_equal("#input-2", "<<input 1x>>y")
    dash_duo.wait_for_text_to_equal("#output", "input 1x + <<input 1x>>y")
    dash_duo.wait_for_text_to_equal("#input-1", "input 1x")
    assert call_counts["output"].value == 3
    assert call_counts["input-2"].value == 2
def exclude_start():
    return html.Div([
        html.P('Exclude the first points',style={'display':'inline-block','margin-right':20}),
        dcc.Input(id='exclude_npt', value='0', type='text', size='2'),
        html.P(' points',style={'display':'inline-block','margin-right':5}),
    ],
        style={'width': '10%', 'display': 'inline-block'}
    )