Skip to content

Commit 032e95a

Browse files
enable ES6 syntax by default
Signed-off-by: Richard Barella <ribab127@gmail.com>
1 parent 2d46453 commit 032e95a

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# better-dash-callback
32

43
A library that enables running clientside callback functions in Dash applications using Python syntax, eliminating the need for inline JavaScript.
@@ -60,7 +59,10 @@ app.layout = html.Div([
6059
@callback(
6160
Output("output", "children"),
6261
Input("input", "value"),
63-
clientside=True
62+
clientside=True,
63+
enable_es6=True,
64+
enable_stage3=True,
65+
prevent_initial_call=True
6466
)
6567
def update_output(value):
6668
return f"You entered: {value}"
@@ -71,6 +73,13 @@ if __name__ == "__main__":
7173

7274
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.
7375

76+
The `callback` function takes the following additional arguments:
77+
78+
* `clientside`: A boolean indicating whether the callback should be executed on the client-side (default is `False`).
79+
* `enable_es6`: A boolean indicating whether to enable ES6 syntax in the generated JavaScript code (default is `True`).
80+
* `enable_stage3`: A boolean indicating whether to enable Stage 3 syntax in the generated JavaScript code (default is `True`).
81+
* `prevent_initial_call`: A boolean indicating whether to prevent the callback from being called on the initial render (default is `False`).
82+
7483
## Installation
7584

7685
To install `better-dash-callback`, you can use `pip`:

better_dash_callback/src.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@
33
import dash
44
import inspect
55

6-
def callback(*args, clientside=False, **kwargs):
6+
def callback(*args, clientside=False, enable_es6=True, enable_stage3=True, **kwargs):
77
"""
88
A decorator to register a Dash callback. If `clientside` is True, the python callback
9-
will be executed clientside using jsbuilder, which will convert it to javascript.
9+
will be executed clientside using https://github.com/metapensiero/metapensiero.pj
10+
which will convert it to javascript.
1011
1112
If `clientside` is False or not set, the callback will be executed serverside
1213
using Dash's standard callback system.
1314
1415
Other than clientside argument, all other arguments are the same as Dash's @callback function.
1516
1617
:param clientside: Whether to execute the callback clientside. Defaults to False.
18+
:param enable_es6: Whether to enable ES6 syntax. Defaults to True.
19+
:param enable_stage3: Whether to enable Stage3 syntax. Defaults to True.
20+
:param args: Arguments to pass to the Dash callback. Include dash.Output, dash.Input, and dash.State.
21+
:param kwargs: Keyword arguments to pass to the Dash callback. Includes `prevent_initial_call=True`.
1722
"""
1823

1924
def decorator(func):
2025

2126
if clientside:
2227
python_code = inspect.getsource(func)
2328
python_code = python_code[python_code.find("def "):]
24-
js_code = metapensiero.pj.__main__.transform_string(python_code)
29+
js_code = metapensiero.pj.__main__.transform_string(python_code, enable_es6=enable_es6, enable_stage3=enable_stage3)
2530
dash.clientside_callback(js_code, *args, **kwargs)
2631
else:
2732
@dash.callback(*args, **kwargs)

examples/example.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import dash
2+
from dash import html, dcc, Output, Input
3+
from better_dash_callback import callback
4+
5+
app = dash.Dash(__name__)
6+
7+
app.layout = html.Div([
8+
dcc.Input(id="input", type="text"),
9+
html.Div(id="output")
10+
])
11+
12+
@callback(
13+
Output("output", "children"),
14+
Input("input", "value"),
15+
prevent_initial_call=True,
16+
clientside=True
17+
)
18+
def update_output(value):
19+
"""
20+
this code will be executed clientside as javascript.
21+
This page shows what syntax is supported: https://github.com/metapensiero/metapensiero.pj
22+
"""
23+
return f"You entered: {value}"
24+
25+
if __name__ == "__main__":
26+
app.run_server(debug=True)

0 commit comments

Comments
 (0)