I have some code that illustrates an issue. I have two input fields.
If I input x, I want to calculate x2 as 2 * x.
If I input x2, I want to calculate x as x2 / 2.0
For the actual problem, the calculation is more complex.
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
def generate_float_input(name: str, label: str, value: float) -> html.Div:
label = html.Label(label)
inpt = dcc.Input(id=name, type='number',
value=value,
style={'border-style': 'solid',
'border-color': 'black',
'width': '100%',
'background-color': 'LightGreen',
'color': 'Black',
'border-width': 'thin'})
return html.Div([label, inpt], style={'columnCount': 2})
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
[
html.Div(
children=
[
generate_float_input("x", "X", 10),
generate_float_input("x2", 'x2', 100)
],
)
]
)
@app.callback(
Output(component_id="x2", component_property='value'),
[
Input(component_id="x", component_property='value')
],
)
def x(value):
print(value)
return value * 2.0
# Fails - Error loading dependencies
@app.callback(
Output(component_id="x", component_property='value'),
[
Input(component_id="x2", component_property='value')
],
)
def x2(value):
print(value)
return value / 2.0
if __name__ == '__main__':
app.run_server(debug=True)
The issue is on the second callback that generates an “Error loading dependencies” message. I understand why, its a question on a work around
Now I suspect the dependency issue is to prevent a cascade of updates that never stops.
Can I do something like this? In the call back for both items, do a check to see if the value is unchanged, and return dash.no_update preventing the cascade that never stops?
Or can I directly set the value of the input, render it, at a lower level such that it doesn’t have to appear as an output in the call back?
Any help welcome.
N.