Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/great-places-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modelscope-studio/pro': patch
'modelscope_studio': patch
---

fix: drop container of MulimodalInput
6 changes: 6 additions & 0 deletions .changeset/violet-candles-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modelscope-studio/pro': patch
'modelscope_studio': patch
---

feat: add `custom` event to allow sending notifications from sandbox to Python
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class ModelScopeProWebSandbox(ModelScopeLayoutComponent):
EventListener("render_error",
callback=lambda block: block._internal.update(
bind_renderError_event=True)),
EventListener("custom",
callback=lambda block: block._internal.update(
bind_custom_event=True)),
]

# supported slots
Expand Down
15 changes: 10 additions & 5 deletions docs/components/pro/web_sandbox/README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

<demo name="html"></demo>

### 自定义 Sandbox 事件

<demo name="custom_sandbox_event"></demo>

### 处理错误

<demo name="error_handling"></demo>
Expand All @@ -43,11 +47,12 @@

### 事件

| 事件 | 描述 |
| ----------------------------------------- | --------------------------- |
| `pro.WebSandbox.compile_success(fn, ···)` | 当 Sandbox 编译成功时触发。 |
| `pro.WebSandbox.compile_error(fn, ···)` | 当 Sandbox 编译失败时触发。 |
| `pro.WebSandbox.render_error(fn, ···)` | 当 Sandbox 渲染抛错时触发。 |
| 事件 | 描述 |
| ----------------------------------------- | ------------------------------------------------------------------------------------------- |
| `pro.WebSandbox.compile_success(fn, ···)` | 当 Sandbox 编译成功时触发。 |
| `pro.WebSandbox.compile_error(fn, ···)` | 当 Sandbox 编译失败时触发。 |
| `pro.WebSandbox.render_error(fn, ···)` | 当 Sandbox 渲染抛错时触发。 |
| `pro.WebSandbox.custom(fn, ···)` | 在 Sandbox 内触发的自定义事件,在 Sandbox 内通过 JavaScript 调用 `window.dispatch` 时触发。 |

### 插槽

Expand Down
15 changes: 10 additions & 5 deletions docs/components/pro/web_sandbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ When `template` is set to `react`, the following dependencies will be automatica

<demo name="html"></demo>

### Custom Sandbox Event

<demo name="custom_sandbox_event"></demo>

### Error Handling

<demo name="error_handling"></demo>
Expand All @@ -43,11 +47,12 @@ When `template` is set to `react`, the following dependencies will be automatica

### Events

| Event | Description |
| ----------------------------------------- | ------------------------------------------------- |
| `pro.WebSandbox.compile_success(fn, ···)` | Triggered when Sandbox compilation succeeds. |
| `pro.WebSandbox.compile_error(fn, ···)` | Triggered when Sandbox compilation fails. |
| `pro.WebSandbox.render_error(fn, ···)` | Triggered when Sandbox rendering throws an error. |
| Event | Description |
| ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `pro.WebSandbox.compile_success(fn, ···)` | Triggered when Sandbox compilation succeeds. |
| `pro.WebSandbox.compile_error(fn, ···)` | Triggered when Sandbox compilation fails. |
| `pro.WebSandbox.render_error(fn, ···)` | Triggered when Sandbox rendering throws an error. |
| `pro.WebSandbox.custom(fn, ···)` | Custom events triggered within the Sandbox are invoked via JavaScript when `window.dispatch` is called within the Sandbox. |

### Slots

Expand Down
26 changes: 26 additions & 0 deletions docs/components/pro/web_sandbox/demos/custom_sandbox_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import gradio as gr
import modelscope_studio.components.antd as antd
import modelscope_studio.components.base as ms
import modelscope_studio.components.pro as pro


def on_custom(e: gr.EventData):
print(e._data)


with gr.Blocks() as demo, ms.Application(), antd.ConfigProvider():
sandbox = pro.WebSandbox(value={
"index.tsx":
"""
export default function App() {
return <button onClick={() => {
window.dispatch({ type: 'custom', payload: { value: 'test' } })
} }>Click to emit custom event to Python</button>;
}
"""
},
template="react")
sandbox.custom(fn=on_custom)

if __name__ == "__main__":
demo.queue().launch()
10 changes: 7 additions & 3 deletions frontend/pro/multimodal-input/multimodal-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,13 @@ export const MultimodalInput = sveltify<
...uploadConfig?.imageProps,
}}
disabled={uploadDisabled}
getDropContainer={() => {
return uploadConfig?.fullscreenDrop ? document.body : null;
}}
getDropContainer={
uploadConfig?.fullscreenDrop
? () => {
return document.body;
}
: undefined
}
items={validFileList}
placeholder={(type) => {
const isDrop = type === 'drop';
Expand Down
6 changes: 6 additions & 0 deletions frontend/pro/web-sandbox/web-sandbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface WebSandboxProps {
onCompileError?: (message: string) => void;
onCompileSuccess?: () => void;
onRenderError?: (message: string) => void;
onCustom?: (...args: any[]) => void;
height?: string | number;
themeMode: string;
setSlotParams: SetSlotParams;
Expand All @@ -53,6 +54,7 @@ export const WebSandbox = sveltify<WebSandboxProps, ['compileErrorRender']>(
onRenderError,
onCompileSuccess,
compileErrorRender,
onCustom,
}) => {
const iframeRef = React.useRef<HTMLIFrameElement>(null);
const divRef = React.useRef<HTMLDivElement>(null);
Expand All @@ -75,6 +77,7 @@ export const WebSandbox = sveltify<WebSandboxProps, ['compileErrorRender']>(
const onCompileErrorMemoized = useMemoizedFn(onCompileError);
const onRenderErrorMemoized = useMemoizedFn(onRenderError);
const onCompileSuccessMemoized = useMemoizedFn(onCompileSuccess);
const onCustomMemoized = useMemoizedFn(onCustom);

// Build import map (only includes third-party dependencies)
const resolvedImportMap = useMemo(() => {
Expand Down Expand Up @@ -248,6 +251,8 @@ export const WebSandbox = sveltify<WebSandboxProps, ['compileErrorRender']>(
try {
// Inject theme
(iframeRef.current.contentWindow as any).gradio_theme = themeMode;
(iframeRef.current.contentWindow as any).dispatch =
onCustomMemoized;
} catch {
//
}
Expand Down Expand Up @@ -288,6 +293,7 @@ export const WebSandbox = sveltify<WebSandboxProps, ['compileErrorRender']>(
iframeUrl,
notificationApi,
onCompileSuccessMemoized,
onCustomMemoized,
onRenderErrorMemoized,
showRenderError,
themeMode,
Expand Down