✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Control over call backs

Is there any way of explicitly setting an Input component’s value, rendering it, from a call back without it triggering an update?

eg. Something like.

infield = get_component (component_id=“field”)
infield.value = “12345”
infield.render().

It relates to an example where I need a circular dependency. Input A is a function of Input B. That’s easy. However if Input B is changed, I have the inverse function and can work out what needs to go in A.

It’s getting around the dependencies.

Any ideas?

Example mini app where x2 is x ** 2, and I would like x = sqrt(x2) if x2 is changed.

I tried to do this with placeholders, but that doesn’t work

Thanks

from math import sqrt
from dash import Dash
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_core_components as dcc

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = Dash(__name__, external_stylesheets=external_stylesheets)


def generate_fixed_pair(label: str, name: str, value: str = '') -> html.Div:
    """
    Generate a label and display box pair
    :param label: Text for the label
    :param name: name of the field
    :param value: default value for the field
    :return: Html of the pair
    """
    label = html.Label(label)
    output = dcc.Input(
        value=value,
        style={
            'border-style': 'solid',
            'width': '100%',
            'border-width': 'thin'
        },
        id=name
    )
    return html.Div([label, output], style={'columnCount': 2})


app.layout = html.Div(
    children=
    [
        generate_fixed_pair('X', 'x'),
        generate_fixed_pair('X2', 'x2')
    ],
)


@app.callback(
    Output('x2', 'placeholder'),
    [
        Input('x', 'value')
    ]
)
def change_x(value):
    print(f"change_x({value})")
    if value == "":
        return ""
    return float(value) ** 2


@app.callback(
    Output('x', 'value'),
    [
        Input('x2', 'value')
    ]
)
def change_x2(value):
    print(f"change_x2({value})")
    if value == "":
        return ""
    return sqrt(float(value))


if __name__ == '__main__':
    app.run_server(debug=True, port=8052)