GitHub user dosubot[bot] added a comment to the discussion: custom event hooks

<!-- Greeting -->
Hi @meytar14! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the 
superset team.

<!-- Answer -->
Yes, you can create custom event hooks in Superset for actions like creating 
dashboards or charts. Superset uses SQLAlchemy event listeners on its models 
(such as Dashboard and Slice for charts), so you can register functions to run 
after events like insert, update, or delete. For example, you can hook into the 
`after_insert` event to trigger your code when a dashboard or chart is created. 
See the implementation in 
[`superset/tags/core.py`](https://github.com/apache/superset/blob/master/superset/tags/core.py):

```python
import sqlalchemy as sqla
from superset.models.dashboard import Dashboard

def my_dashboard_created_hook(mapper, connection, target):
    # Your custom code here
    print(f"Dashboard created: {target.id}")

sqla.event.listen(Dashboard, "after_insert", my_dashboard_created_hook)
```

However, for upgrade-safe side effects (like logging or external calls), 
Superset recommends using Flask AppBuilder view hooks (`after_model_create`, 
`after_model_change`, etc.), which run after the transaction is committed and 
are less risky than SQLAlchemy listeners. More details on this pattern are 
discussed [here](https://github.com/apache/superset/issues/35594).

If you want to trigger external systems, you can also use webhooks for alerts 
and reports, which send info to your endpoint when a report or alert is created 
for a chart or dashboard. See the webhook feature 
[here](https://github.com/apache/superset/pull/30044).

Frontend event listeners are also possible via Superset's extension APIs, if 
you need to react to user actions in the UI. For async events, check out 
[`superset-frontend/src/middleware/asyncEvent.ts`](https://github.com/apache/superset/blob/master/superset-frontend/src/middleware/asyncEvent.ts).

Let me know if you want a more detailed example for any of these approaches!


<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*

---
<sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/030e9bab-b0e3-4f27-bb02-9bd4893b03ba?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/030e9bab-b0e3-4f27-bb02-9bd4893b03ba?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/030e9bab-b0e3-4f27-bb02-9bd4893b03ba?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/030e9bab-b0e3-4f27-bb02-9bd4893b03ba?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/030e9bab-b0e3-4f27-bb02-9bd4893b03ba?feedback_type=hallucination)
 | [Report 
šŸ›](https://app.dosu.dev/response-feedback/030e9bab-b0e3-4f27-bb02-9bd4893b03ba?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/030e9bab-b0e3-4f27-bb02-9bd4893b03ba?feedback_type=other)</sup>

[![Chat with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github)&nbsp;[![Open
 in 
Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=Yes%2C%20you%20can%20create%20custom%20event%20hooks%20in%20Superset%20for%20actions%20like%20creating%20dashboards%20or%20charts.%20Superset%20uses%20SQLAlchemy%20event%20listeners%20on%20its%20models%20%28such%20as%20Dashboard%20and%20Slice%20for%20charts%29%2C%20so%20you%20can%20register%20functions%20to%20run%20after%20events%20like%20insert%2C%20update%2C%20or%20delete.%20For%20example%2C%20you%20can%20hook%20into%20the%20%60after_insert%60%20event%20to%20trigger%20your%20code%20when%20a%20dashboard%20or%20chart%20is%20created.%20See%20the%20implementation%20in%20%5B%60superset/tags/core.py%60%5D%28https%3A//github.com/apache/superset/blob/master/superset/tags/core.py%29%3A%0A%0A%60%60%60python%0Aimport%20sqlalchemy%20as%20sqla%0Afrom%20superset.
 
models.dashboard%20import%20Dashboard%0A%0Adef%20my_dashboard_created_hook%28mapper%2C%20connection%2C%20target%29%3A%0A%20%20%20%20%23%20Your%20custom%20code%20here%0A%20%20%20%20print%28f%22Dashboard%20created%3A%20%7Btarget.id%7D%22%29%0A%0Asqla.event.listen%28Dashboard%2C%20%22after_insert%22%2C%20my_dashboard_created_hook%29%0A%60%60%60%0A%0AHowever%2C%20for%20upgrade-safe%20side%20effects%20%28like%20logging%20or%20external%20calls%29%2C%20Superset%20recommends%20using%20Flask%20AppBuilder%20view%20hooks%20%28%60after_model_create%60%2C%20%60after_model_change%60%2C%20etc.%29%2C%20which%20run%20after%20the%20transaction%20is%20committed%20and%20are%20less%20risky%20than%20SQLAlchemy%20listeners.%20More%20details%20on%20this%20pattern%20are%20discussed%20%5Bhere%5D%28https%3A//github.com/apache/superset/issues/35594%29.%0A%0AIf%20you%20want%20to%20trigger%20external%20systems%2C%20you%20can%20also%20use%20webhooks%20for%20alerts%20and%20reports%2C%20which%20send%20info%20to%20y
 
our%20endpoint%20when%20a%20report%20or%20alert%20is%20created%20for%20a%20chart%20or%20dashboard.%20See%20the%20webhook%20feature%20%5Bhere%5D%28https%3A//github.com/apache/superset/pull/30044%29.%0A%0AFrontend%20event%20listeners%20are%20also%20possible%20via%20Superset%27s%20extension%20APIs%2C%20if%20you%20need%20to%20react%20to%20user%20actions%20in%20the%20UI.%20For%20async%20events%2C%20check%20out%20%5B%60superset-frontend/src/middleware/asyncEvent.ts%60%5D%28https%3A//github.com/apache/superset/blob/master/superset-frontend/src/middleware/asyncEvent.ts%29.%0A%0ALet%20me%20know%20if%20you%20want%20a%20more%20detailed%20example%20for%20any%20of%20these%20approaches%21)&nbsp;[![Join
 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share
 on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/a
 pache/superset/discussions/37429)

GitHub link: 
https://github.com/apache/superset/discussions/37429#discussioncomment-15598054

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to