Jupyterpost: post from jupyterhub to a chat server (mattermost)

Hi all,

The tool

I’ve freshly implemented a simple JupyterHub service jupyterpost (pypi, repo) that converts this code

from jupyterpost import post
from matplotlib import pyplot
pyplot.plot([0, 1, 0])
post(
    "Hello from Jupyterhub :)",  # Your message
    "@anton-akhmerov",  # The channel, can be @username
    pyplot.gcf(),  # Either matplotlib figure or bytes of the png image
)

into this chat message:
image

Right now jupyterpost only supports mattermost chat, but generalizing to more services (say slack) is within the project scope.

Implementation notes

I found that adding a JupyterHub service requires changing a lot of moving parts:

  • The service needs to be added to the hub
  • Proper credentials need to be passed to the service downstream
  • Users and in my case user servers need to get a permission to access it
  • User servers need to know where to find this service

To simplify adding the service I implemented a convenience function for modifying the hub config in place, so that all of the above points are achieved by this code after all the other configuration is done:

from jupyterpost import configure_jupyterhub

configure_jupyterhub(
    c,
    mattermost_token="your mattermost token",
    mattermost_url="https://your.mattermost.server/api/v4/",
    mattermost_team="your mattermost team name",
    jupyterpost_url="https://services.your.jupyterhub/services/jupyterpost",
)

Questions

  • Unfortunately I didn’t figure out how to compute the service URL (including protocol and domain) from the instance of hub config, that’s why I provide an additional config parameter. Any advice there?
  • Is there any practical advice for testing hub services?
3 Likes

Hi @Anton_Akhmerov,
that is a cool project!
Here are some ideas where this could go further:

  • How about not only posting the output to mattermost, but also the code?
  • Also, wouldn’t it be nice to skip the step of byte conversion with pyplot.gcf() ?
    Then one could stream a cell output and its generating code like it’s normally used without any modification.

A way that could be achieved would be by using cellmagic in the first line, e.g.

%%cellstream --message "Hello from Jupyterhub :)" --channel "@anton-akhmerov" 
import matplotlib.pyplot as plt
plt.plot([1,2], [10,20], c = "orange")

I’ve already implemented a very similar cell magic in the package jupyter_capture_output.
It can capture code, as well as text, image and video outputs.

1 Like