Push variables to user's namespace through JupyterLab Widgets

What is the standard way of sending data to ipython kernel from a JupyterLab Widget?
I have made an Jupyter Lab extension that adds a left sidebar to search for weather data for a city. Now I want that data to be pushed to the users namespace in the ipython kernel. I am not getting any satisfactory answer on the internet. Any help will be appreciated.

You have to get a hold on the correct kernel connection, then you can send an execute request to the kernel, or you can have them install a python-side package to listen to comm messages. You can find the current notebook’s kernel connection (whatever that notebook is) by requesting the notebook tracker in your plugin, then getting the active notebook widget’s sessionContext.session.kernel attribute.

That’s a lot, and a lot of jargon, but hopefully enough to get you started in the right direction.

I have already registerd a comm target using a python module. I just need to connect to the target and send data from the frontend. Its the frontend part that troubles me. I have figured out that I will need to use a notebook tracker for the same but there is no concrete examples on how to approach.

Here is how the notebook extension gets the current notebook from the tracker: https://github.com/jupyterlab/jupyterlab/blob/0fd510c7f429bf80cbe49244f2d2b95c2081d16b/packages/notebook-extension/src/index.ts#L883-L893

It would be great to have more concrete examples of patterns plugins can use, so we hope you can publish your source, or contribute to the documentation, to help people after you.