Skip to content

Commit

Permalink
javascript in plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ddPn08 committed May 29, 2023
1 parent 53d60b5 commit d381f45
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions api/models/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ class PluginMetaData(BaseModel):
version: str
author: Optional[str]
url: Optional[str]
javascript: Optional[str]
19 changes: 19 additions & 0 deletions javascripts/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @typedef PluginMeta
* @property {string} main
* @property {string} name
* @property {string} version
* @property {string} author
* @property {string} url
* @property {string} javascript
*/

export const loadPlugins = async () => {
/** @type {PluginMeta[]} */
const plugins = await fetch('/api/plugins').then((res) => res.json())
for (const dir in plugins) {
const plugin = plugins[dir]
if (!plugin.javascript) continue
await import(`/api/plugins/${dir}/js/${plugin.javascript}`)
}
}
3 changes: 3 additions & 0 deletions javascripts/radiata.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { gradioApp } from './utils'
import { loadPlugins } from './plugin'

window['__RADIATA_CONTEXT'] = {
executedOnLoaded: false,
Expand Down Expand Up @@ -26,6 +27,8 @@ export const callEventListeners = (type, ...args) => {
}

export const initialize = () => {
loadPlugins()

addEventListener('ready', () => {
const button = gradioApp().getElementById('inference-mode-reload-button')
button.click()
Expand Down
1 change: 0 additions & 1 deletion modules/components/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def ui():
add_model_button = gr.Button("💾", elem_classes=["tool-button"])
with gr.Row():
vae_id_dropdown = gr.Dropdown(
placeholder="VAE",
choices=vae.list_vae_models() + ["Auto"],
value="Auto",
show_label=False,
Expand Down
2 changes: 1 addition & 1 deletion modules/components/image_generation_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def plugin_options_ui():

with gr.Column():
for name, data in plugin_loader.plugin_store.items():
if "ui" not in data and data["ui"] is not None:
if "ui" not in data or data["ui"] is None:
continue
with gr.Accordion(name, open=False):
plugin_values[name] = data["ui"]()
Expand Down
32 changes: 32 additions & 0 deletions modules/plugin_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from glob import glob

import toml
from fastapi import HTTPException
from fastapi.responses import FileResponse

from api.events import event_handler
from api.events.common import PostAppLaunchEvent
from api.models.plugin import PluginMetaData

from .shared import ROOT_DIR
Expand Down Expand Up @@ -39,3 +43,31 @@ def register_plugin_ui(func):
raise ValueError("Plugin not found")

plugin_store[plugin_name]["ui"] = func


def api_get_plugins():
return {
plugin_name: plugin_store[plugin_name]["meta"] for plugin_name in plugin_store
}


def api_get_plugin_js(name: str, file_path: str):
if name not in plugin_store:
raise HTTPException(status_code=404, detail="Plugin not found")

dirname = os.path.dirname(file_path)
basename = os.path.basename(file_path)

if "." not in basename:
basename = f"{basename}.js"

return FileResponse(
path=os.path.join("plugins", name, "javascripts", dirname, basename)
)


@event_handler()
def on_app_post_load(e: PostAppLaunchEvent):
app = e.app
app.get("/api/plugins")(api_get_plugins)
app.get("/api/plugins/{name}/js/{file_path:path}")(api_get_plugin_js)

0 comments on commit d381f45

Please sign in to comment.