-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements the function to issue notification using ntfy
- Loading branch information
1 parent
4a3709a
commit cb874d3
Showing
3 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { ISessionContext } from '@jupyterlab/apputils'; | ||
import { Kernel, KernelAPI, KernelMessage } from '@jupyterlab/services'; | ||
|
||
export async function ensureSessionContextKernelActivated( | ||
sessionContext: ISessionContext | ||
): Promise<void> { | ||
if (sessionContext.hasNoKernel) { | ||
await sessionContext.initialize() | ||
.then(async (value) => { | ||
if (value) { | ||
const py3kernel = await KernelAPI.startNew({ name: 'python3' }); | ||
await sessionContext.changeKernel(py3kernel); | ||
} | ||
}) | ||
.catch((reason) => { | ||
console.error(`Failed to initialize the session in jupyterlab-notifications.\n${reason}`); | ||
});; | ||
} | ||
} | ||
|
||
export async function issueNtfyNotification( | ||
title: string, | ||
notificationPayload: { body: string }, | ||
sessionContext: ISessionContext, | ||
): Promise<Kernel.IShellFuture<KernelMessage.IExecuteRequestMsg, KernelMessage.IExecuteReplyMsg>> { | ||
const { body } = notificationPayload; | ||
await ensureSessionContextKernelActivated(sessionContext); | ||
if (!sessionContext || !sessionContext.session?.kernel) { | ||
return; | ||
} | ||
const titleEscaped = title.replace(/"/g, '\\"'); | ||
const bodyEscaped = body.replace(/"/g, '\\"'); | ||
const code = `from ntfy import notify; notify("${bodyEscaped}", "${titleEscaped}")`; | ||
return sessionContext.session?.kernel?.requestExecute({ code }); | ||
} |