Skip to main content

Runs clientside callback functions in Dash applications using Python syntax, eliminating the need for inline JavaScript.

Project description

better-dash-callback

A library that enables running clientside callback functions in Dash applications using Python syntax, eliminating the need for inline JavaScript.

Problem

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.

Solution

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.

Dependencies

better-dash-callback depends on metapensiero.pj, a Python-to-JavaScript compiler that allows you to write Python code that can be executed in a JavaScript environment.

Supported Python-to-JavaScript Syntax

The supported Python-to-JavaScript syntax is listed in the metapensiero.pj documentation. This includes support for many Python features, such as functions, classes, loops, and conditional statements.

Example

Let's consider a simple example where we want to update the text of a component based on the value of an input component.

Using Dash's clientside_callback

import dash
from dash import html
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

app.layout = html.Div([
    html.Input(id="input", type="text"),
    html.Div(id="output")
])

app.clientside_callback(
    """
    function(value) {
        return 'You entered: ' + value;
    }
    """,
    Output("output", "children"),
    Input("input", "value"),
    
)

if __name__ == "__main__":
    app.run_server(debug=True)

Using better-dash-callback

import dash
from dash import html
from better_dash_callback import callback

app = dash.Dash(__name__)

app.layout = html.Div([
    html.Input(id="input", type="text"),
    html.Div(id="output")
])

@callback(
    Output("output", "children"),
    Input("input", "value"),
    clientside=True,
    prevent_initial_call=True
)
def update_output(value):
    return f"You entered: {value}"

if __name__ == "__main__":
    app.run_server(debug=True)

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.

The callback function takes the following additional arguments:

  • clientside: A boolean indicating whether the callback should be executed on the client-side (default is False).
  • disable_es6: A boolean indicating whether to disable ES6 syntax in the generated JavaScript code and revert back to ES5 support (default is False).
  • enable_stage3: A boolean indicating whether to enable Stage 3 syntax in the generated JavaScript code (default is False).
  • Any arguments and keyword-arguments supported by dash.callback are also supported by better-dash-callback.callback

Installation

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

pip3 install better-dash-callback

License

better-dash-callback is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

better_dash_callback-0.1.3.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

better_dash_callback-0.1.3-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file better_dash_callback-0.1.3.tar.gz.

File metadata

  • Download URL: better_dash_callback-0.1.3.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for better_dash_callback-0.1.3.tar.gz
Algorithm Hash digest
SHA256 d7cc7e93c4ccfa297610a26c6f8aa31a235cfbeb3b43543ba98c47a4daebd5e4
MD5 bd3704a6877f1171e42a915be92e656d
BLAKE2b-256 6282b42b62eafee54907159e69b4ab7c62c4cbe4bb76dabb9a96169e427ea99b

See more details on using hashes here.

File details

Details for the file better_dash_callback-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for better_dash_callback-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f067e6b48823abaa47554ac7d5297ccc5e0ca550bdad5fc626216cbfede18fd9
MD5 76483d1d655b45afc427ad567c768676
BLAKE2b-256 ec01b49a35977ce53dc7b10e8bbeee483bbe319972d3a2743c28b479391368d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page