Dash library for managing component IDs
Project description
dash-prefix
Plotly/Dash utility library that allows component IDs to use prefixed namespaces. This greatly reduces Dash component ID conflicts. A component ID is only defined once when the component is created. It is then used by reference in associated Dash callbacks:
pip install dash-prefix
prefix() Returns a lambda that will prefix all component IDs with the given prefix. If no prefix is given one will be assigned.
from dash import html, callback
from dash_prefix import prefix
pfx = prefix()
user_name = dbc.Input(id=pfx('user'), placeholder="Enter name")
password = dhc.PasswordInput("Password", name='password', id=pfx('password'), placeholder="Enter password")
btn = html.Button('Enter', id=pfx('enter'), disabled=True)
@callback(btn.output.disabled, user_name.input.value, password.input.value)
def _cb_enter(user_name, password):
return not db_validate_user(user_name, password)
isTriggered() Returns true if the given dash component was the reason for the enclosing callback being triggered
app = dash.Dash(__name__)
btn1 = html.Button("Button 1", id='btn1')
btn2 = html.Button("Button 2", id='btn3')
title = html.H1('Title', id='title')
@app.callback(title.output.children, btn1.input.n_clicks, btn2.input.n_clicks)
def _update(btn1_clicks, btn2_clicks):
if isTriggered(btn1.input.n_clicks):
return "Button1 Clicked"
if isTriggered(btn2.input.n_clicks):
return "Button2 Clicked"
return NOUPDATE
match() and trigger_index() Are Pattern-Matching Callback helpers
Match = match({'type': 'buttons', 'idx': ALL})
btn1 = html.Button('Button 1', id=Match.idx(1))
btn2 = html.Button('Button 2', id=Match.idx(2))
btn2 = html.Button('Button 3', id=Match.idx(3))
title = html.Div(id='title')
@callback(title.output.children, Match.input.n_clicks)
def _update_(button_clicks):
idx = trigger_index()
if idx is not None:
return f"Button {idx} triggered {button_clicks[idx]} times!"
return NOUPDATE
css_id All dash components are injected with a css_id
property. This makes testing
with Dash Duo far less taxing.
title = html.Div('Title', id='title')
_title = dash_duo.find_element(title.css_id)
Can also be used with dictionary IDs
Match = match({'type': 'buttons', 'idx': ALL})
btn1 = html.Button('Button 1', id=Match.idx(1))
btn2 = html.Button('Button 2', id=Match.idx(2))
btn3 = html.Button('Button 3', id=Match.idx(3))
_btn3 = dash_duo.find_element(btn3.css_id)
Build the project
The dash-prefix package is available on pypi. If needed, to create a local tarball, first change the release version in dash_spa/_version.py, then:
rm -rf dist dash_spa.egg-info build
python setup.py sdist bdist_wheel
The tarball is in dist/dash_spa-.tar.gz
To install the tarball in a dash project:
pip install dash_spa-<version>.tar.gz
Testing
Pytest and Dash Duo are used for testing. To run these tests both the Chrome browser and Chrome driver must be installed.
To run the tests:
pytest
Publish
twine upload dist/*
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
Built Distribution
File details
Details for the file dash-prefix-0.0.4.tar.gz
.
File metadata
- Download URL: dash-prefix-0.0.4.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.11.4 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65ffacf8a12a2a0b32acc7982474d0c808e4e5b24c852575b02c26b825659bb6 |
|
MD5 | cc39cadb0cafbb91c549bc5ead9abe1b |
|
BLAKE2b-256 | baaf78b697bd84462a3ed52a4f4a19085f916c3db01a714ab9730d39539d036f |
File details
Details for the file dash_prefix-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: dash_prefix-0.0.4-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.11.4 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fc3423a1d79d8ac4cfca22b1fb317fb3b90ae9a326f144e8205e84492024a8c |
|
MD5 | 1a81e7909fab33c605c6ff364c76d672 |
|
BLAKE2b-256 | 4ace92e52630a0e85fbd0403b4bdfe19444226f5aa9c023eb9856677e02510d7 |