Add support for external events
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 24.4k
- Forks
- 2.3k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 13
Description
There are situations where may want to send events into dash from a part of the DOM not managed / generated by Dash. For example, DHTML may be generated by a backend and inserted into the DOM shared by Dash. If such content has events, would like to propagate specific events to Dash (and through react's event mechanism).
I have found a way to "hack" this in, but requesting a "proper" implementation. The hack is as follows:
- creating a hidden dash entity
- adding javascript to send an event to this hidden entity and trigger react
- listening on the value of this hidden Dash / DOM entity
The usage is as follows:
def create(...):
"""
Create the dash app layout
"""
return dbc.Container([
<dash layout structure>
...
# hidden element to receive external events inserted into dash DOM
ExternalEventRendezvous()])
@app.callback(Output(...), [ExternalInput(), ...])
def dosomething(event):
# event contains the text of the payload sent by the external event
...
In the external code, events are sent using the DOM onEvent triggers:
<button class="btn btn-sm" onclick='sendEvent("my-payload")'">click me</button>
Clicking this externally provided button would call the sendEvent() javascript and cause the hidden element to take on the value of the payload and trigger the react event.
The proposal would be to do away with the requirement for the hidden ExternalEventRendezvous() element (which is an Input element).
Here is the supporting javascript function:
var __redendezvous_count = 0;
var __rendezvouz_setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set;
/**
* Send synthetic event to application (from client)
*
* @param payload payload as string
*/
function sendEvent(payload) {
var rendezvous = document.getElementById("ExternalEvent");
var newvalue = payload + ":" + __redendezvous_count++;
__rendezvouz_setter.call(rendezvous, newvalue);
var ev = new Event('input', { bubbles: true });
rendezvous.dispatchEvent(ev);
}
And here are the python functions:
def ExternalEventRendezvous():
"""
Add external event rendevous event
"""
return dbc.Input(type='text', id='ExternalEvent', placeholder='', style={'visibility': 'hidden'})
def ExternalInput():
"""
External input
"""
return Input("ExternalEvent", "value")
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by examining the proposed sendEvent function and how externally inserted DOM events enter React's event mechanism. Compare that flow with the hidden ExternalEventRendezvous input and Python callback examples. Done means external events can reach Dash callbacks without requiring the hidden rendezvous element.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, python, react
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100