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

Own plugins for Jupyverse #439

Closed
KenKi0 opened this issue Sep 23, 2024 · 5 comments
Closed

Own plugins for Jupyverse #439

KenKi0 opened this issue Sep 23, 2024 · 5 comments

Comments

@KenKi0
Copy link

KenKi0 commented Sep 23, 2024

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
Copy link
Collaborator

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)

@KenKi0
Copy link
Author

KenKi0 commented Sep 27, 2024

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)

Ok, I have a plugin as a module that lies next to the main project. How will Jupyverse understand that it needs to pull it up? Do I need to install my plugin as a package as a dependency in the main pyproject.toml, where jupyverse also lies as a dependency, or is it possible to run jupyverse with some flag?

@davidbrochart
Copy link
Collaborator

Jupyverse will start all components that are registered in the "jupyverse.components" entry point, that's why you need to have this in your pyproject.toml:

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

@KenKi0
Copy link
Author

KenKi0 commented Sep 30, 2024

Jupyverse will start all components that are registered in the "jupyverse.components" entry point, that's why you need to have this in your pyproject.toml:

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

In your first answer you said that you need to add these fields to pyproject.toml of the package itself. Or should it be in pyproject.toml of the main application, where my_package will be in the dependencies of this project next to jupyverse?

@davidbrochart
Copy link
Collaborator

The entry points should be defined in the pyproject.toml of the plugin.

@KenKi0 KenKi0 closed this as completed Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants