jupyter-server / jupyverse

A Jupyter server based on FastAPI :rocket:
https://jupyter-server.github.io/jupyverse
Other
231 stars 28 forks source link

Own plugins for Jupyverse #439

Open KenKi0 opened 1 day ago

KenKi0 commented 1 day ago

Hello everyone, there was a need to write my own additional plugins for Jupuverse, there is no documentation for this as such, depending on how the main plugins are written, it seems to be clear how they are written, basically new handles seem to be added there, and the logic of these handles is written. But how to add your own plugin to Jupyverse is not entirely clear; do you need to fork it and add it to plugins and rewrite the source code, or are there more convenient ways to add your own plugins?

davidbrochart commented 1 day ago

There is no need to add plugins to this repository, all you need is to create a package which has the following in its pyproject.toml:

[project.entry-points]
"asphalt.components"   = {my_package = "my_package.main:MyComponent"}
"jupyverse.components" = {my_package = "my_package.main:MyComponent"}

And something like the following in main.py:

from asphalt.core import Component, Context
from fastapi import APIRouter, Depends
from jupyverse_api import Router
from jupyverse_api.app import App
from jupyverse_api.auth import Auth, User

class MyComponent(Component):
    async def start(
        self,
        ctx: Context,
    ) -> None:
        app = await ctx.request_resource(App)
        auth = await ctx.request_resource(Auth)

        resource = Resource(app, auth)
        ctx.add_resource(resource)

class Resource(Router):
    def __init__(
        self,
        app: App,
        auth: Auth,
    ) -> None:
        super().__init__(app=app, auth=auth)

        router = APIRouter()

        @router.get("/")
        async def get_root(
            user: User = Depends(auth.current_user())
        ) -> str:
            return "Hello"

        self.include_router(router)