Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Listeners API #10

Merged
merged 5 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 81 additions & 7 deletions docs/demo/demo-notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -44,7 +44,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -94,7 +94,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -115,27 +115,27 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"{\"__timestamp__\": \"2022-08-11T05:06:20.339412Z\", \"__schema__\": \"myapplication.org/example-event\", \"__schema_version__\": 1, \"__metadata_version__\": 1, \"name\": \"My Event\"}\n"
"{\"__timestamp__\": \"2022-08-11T22:46:22.248281Z\", \"__schema__\": \"myapplication.org/example-event\", \"__schema_version__\": 1, \"__metadata_version__\": 1, \"name\": \"My Event\"}\n"
]
},
{
"data": {
"text/plain": [
"{'__timestamp__': '2022-08-11T05:06:20.339412Z',\n",
"{'__timestamp__': '2022-08-11T22:46:22.248281Z',\n",
" '__schema__': 'myapplication.org/example-event',\n",
" '__schema_version__': 1,\n",
" '__metadata_version__': 1,\n",
" 'name': 'My Event'}"
]
},
"execution_count": 29,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -149,6 +149,80 @@
")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's demo adding a listener to the already registered event."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def my_listener(data):\n",
" print(\"hello, from my_listener!\")\n",
" print(data)\n",
"\n",
"logger.add_listener(schema_id=\"myapplication.org/example-event\", version=1, listener=my_listener)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we emit the event again, you'll see our listener \"sees\" the event and executes some code:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"{\"__timestamp__\": \"2022-08-11T22:46:28.947996Z\", \"__schema__\": \"myapplication.org/example-event\", \"__schema_version__\": 1, \"__metadata_version__\": 1, \"name\": \"My Event\"}\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world\n",
"EventListenerData(event_logger=<jupyter_events.logger.EventLogger object at 0x7fd07085dfd0>, schema_id='myapplication.org/example-event', version=1, data={'name': 'My Event'})\n",
"hello world\n",
"EventListenerData(event_logger=<jupyter_events.logger.EventLogger object at 0x7fd07085dfd0>, schema_id='myapplication.org/example-event', version=1, data={'name': 'My Event'})\n"
]
},
{
"data": {
"text/plain": [
"{'__timestamp__': '2022-08-11T22:46:28.947996Z',\n",
" '__schema__': 'myapplication.org/example-event',\n",
" '__schema_version__': 1,\n",
" '__metadata_version__': 1,\n",
" 'name': 'My Event'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"logger.emit(\n",
" schema_id=\"myapplication.org/example-event\",\n",
" version=1,\n",
" data={\n",
" \"name\": \"My Event\"\n",
" }\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
1 change: 1 addition & 0 deletions docs/user_guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ defining-schema
configure
application
modifiers
listeners
```
24 changes: 24 additions & 0 deletions docs/user_guide/listeners.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Adding event listeners

Event listeners are callback functions/methods that are executed when an event is emitted.

Listeners can be used by extension authors to trigger custom logic every time an event occurs.

## Basic usage

Define a listener function:

```python
from jupyter_events.logger import EventLogger

def my_listener(logger: EventLogger, schema_id: str, data: dict) -> None:
print("hello, from my listener")
```

Hook this listener to a specific event type:

```python
event_logger.add_listener("http://event.jupyter.org/my-event", listener=my_listener)
```

Now, every time a `"http://event.jupyter.org/test"` event is emitted from the EventLogger, this listener will be called.
Loading