|
| 1 | + |
| 2 | +# better-dash-callback |
| 3 | + |
| 4 | +A library that enables running clientside callback functions in Dash applications using Python syntax, eliminating the need for inline JavaScript. |
| 5 | + |
| 6 | +## Problem |
| 7 | + |
| 8 | +When building Dash applications, you often need to write clientside callback functions using inline JavaScript. This can be cumbersome and error-prone, especially for complex logic. Moreover, inline JavaScript code lacks syntax highlighting and debugging capabilities. |
| 9 | + |
| 10 | +## Solution |
| 11 | + |
| 12 | +`better-dash-callback` provides a solution to this problem by allowing you to write clientside callback functions using Python syntax. This makes your code more readable, maintainable, and efficient. |
| 13 | + |
| 14 | +## Example |
| 15 | + |
| 16 | +Let's consider a simple example where we want to update the text of a component based on the value of an input component. |
| 17 | + |
| 18 | +**Using Dash's `clientside_callback`** |
| 19 | + |
| 20 | +```python |
| 21 | +import dash |
| 22 | +from dash import html |
| 23 | +from dash.dependencies import Input, Output |
| 24 | + |
| 25 | +app = dash.Dash(__name__) |
| 26 | + |
| 27 | +app.layout = html.Div([ |
| 28 | + html.Input(id="input", type="text"), |
| 29 | + html.Div(id="output") |
| 30 | +]) |
| 31 | + |
| 32 | +app.clientside_callback( |
| 33 | + """ |
| 34 | + function(value) { |
| 35 | + return 'You entered: ' + value; |
| 36 | + } |
| 37 | + """, |
| 38 | + Output("output", "children"), |
| 39 | + Input("input", "value") |
| 40 | +) |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + app.run_server(debug=True) |
| 44 | +``` |
| 45 | + |
| 46 | +**Using `better-dash-callback`** |
| 47 | + |
| 48 | +```python |
| 49 | +import dash |
| 50 | +from dash import html |
| 51 | +from better_dash_callback import callback |
| 52 | + |
| 53 | +app = dash.Dash(__name__) |
| 54 | + |
| 55 | +app.layout = html.Div([ |
| 56 | + html.Input(id="input", type="text"), |
| 57 | + html.Div(id="output") |
| 58 | +]) |
| 59 | + |
| 60 | +@callback( |
| 61 | + Output("output", "children"), |
| 62 | + Input("input", "value"), |
| 63 | + clientside=True |
| 64 | +) |
| 65 | +def update_output(value): |
| 66 | + return f"You entered: {value}" |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + app.run_server(debug=True) |
| 70 | +``` |
| 71 | + |
| 72 | +As you can see, the `better-dash-callback` example is more elegant and easier to read. You can write your callback function using Python syntax, without having to worry about inline JavaScript code. |
| 73 | + |
| 74 | +## Installation |
| 75 | + |
| 76 | +To install `better-dash-callback`, you can use `pip`: |
| 77 | + |
| 78 | +```bash |
| 79 | +pip3 install better-dash-callback |
| 80 | +``` |
| 81 | + |
| 82 | +## License |
| 83 | + |
| 84 | +`better-dash-callback` is licensed under the MIT License. |
0 commit comments