Create a callback that updates the output by calling a clientside (JavaScript) function instead of a Python function. Unlike `@app.callback`, `clientside_callback` is not a decorator: it takes either a `dash.dependencies.ClientsideFunction(namespace, function_name)`
(self, clientside_function, *args, **kwargs)
| 1412 | ) |
| 1413 | |
| 1414 | def clientside_callback(self, clientside_function, *args, **kwargs): |
| 1415 | """Create a callback that updates the output by calling a clientside |
| 1416 | (JavaScript) function instead of a Python function. |
| 1417 | |
| 1418 | Unlike `@app.callback`, `clientside_callback` is not a decorator: |
| 1419 | it takes either a |
| 1420 | `dash.dependencies.ClientsideFunction(namespace, function_name)` |
| 1421 | argument that describes which JavaScript function to call |
| 1422 | (Dash will look for the JavaScript function at |
| 1423 | `window.dash_clientside[namespace][function_name]`), or it may take |
| 1424 | a string argument that contains the clientside function source. |
| 1425 | |
| 1426 | For example, when using a `dash.dependencies.ClientsideFunction`: |
| 1427 | ``` |
| 1428 | app.clientside_callback( |
| 1429 | ClientsideFunction('my_clientside_library', 'my_function'), |
| 1430 | Output('my-div' 'children'), |
| 1431 | [Input('my-input', 'value'), |
| 1432 | Input('another-input', 'value')] |
| 1433 | ) |
| 1434 | ``` |
| 1435 | |
| 1436 | With this signature, Dash's front-end will call |
| 1437 | `window.dash_clientside.my_clientside_library.my_function` with the |
| 1438 | current values of the `value` properties of the components `my-input` |
| 1439 | and `another-input` whenever those values change. |
| 1440 | |
| 1441 | Include a JavaScript file by including it your `assets/` folder. The |
| 1442 | file can be named anything but you'll need to assign the function's |
| 1443 | namespace to the `window.dash_clientside` namespace. For example, |
| 1444 | this file might look: |
| 1445 | ``` |
| 1446 | window.dash_clientside = window.dash_clientside || {}; |
| 1447 | window.dash_clientside.my_clientside_library = { |
| 1448 | my_function: function(input_value_1, input_value_2) { |
| 1449 | return ( |
| 1450 | parseFloat(input_value_1, 10) + |
| 1451 | parseFloat(input_value_2, 10) |
| 1452 | ); |
| 1453 | } |
| 1454 | } |
| 1455 | ``` |
| 1456 | |
| 1457 | Alternatively, you can pass the JavaScript source directly to |
| 1458 | `clientside_callback`. In this case, the same example would look like: |
| 1459 | ``` |
| 1460 | app.clientside_callback( |
| 1461 | ''' |
| 1462 | function(input_value_1, input_value_2) { |
| 1463 | return ( |
| 1464 | parseFloat(input_value_1, 10) + |
| 1465 | parseFloat(input_value_2, 10) |
| 1466 | ); |
| 1467 | } |
| 1468 | ''', |
| 1469 | Output('my-div' 'children'), |
| 1470 | [Input('my-input', 'value'), |
| 1471 | Input('another-input', 'value')] |
no outgoing calls