-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Each and context value handler
- Loading branch information
Showing
43 changed files
with
598 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
|
||
def on_custom(e: gr.EventData): | ||
print(e) | ||
print(e._data) | ||
|
||
|
||
with gr.Blocks() as demo: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Div | ||
|
||
同 HTML 中的`div`标签,用于辅助布局,可以绑定常用的 HTML 事件,具体事件请查看`EVENTS`属性。 | ||
|
||
## 示例 | ||
|
||
<demo name="basic"></demo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Div | ||
|
||
Same as the `div` tag in HTML, used for layout assistance, can bind common HTML events, please refer to the `EVENTS` property for specific events. | ||
|
||
## Examples | ||
|
||
<demo name="basic"></demo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from helper.Docs import Docs | ||
|
||
docs = Docs(__file__) | ||
|
||
if __name__ == "__main__": | ||
docs.render().queue().launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import gradio as gr | ||
|
||
import modelscope_studio.components.antd as antd | ||
import modelscope_studio.components.base as ms | ||
|
||
with gr.Blocks() as demo: | ||
with ms.Application(): | ||
with ms.Div(elem_style=dict(color="red", fontSize=22)): | ||
ms.Text("Hello Div") | ||
|
||
if __name__ == "__main__": | ||
demo.queue().launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Each | ||
|
||
辅助渲染组件,该组件支持传入列表作为参数,并会在前端遍历被包裹的组件,将列表的每一项注入当前遍历的上下文中,被包裹的组件属性会在前端被相应的上下文属性替换(只支持从`modelscope_studio`中导出的组件)。 | ||
|
||
## 何时使用 | ||
|
||
- 当被遍历的组件中全部都是`modelscope_studio`组件或非`modelscope_studio`的组件值不与被遍历的值绑定时; | ||
- 当需要遍历展示的列表长度无法确定时; | ||
|
||
## 示例 | ||
|
||
<demo name="basic"></demo> | ||
|
||
`modelscope_studio`中的每个组件都有一个`as_item`参数(包括`Each`组件本身),指定该参数值后可以直接基于上下文的值做过滤,逻辑上类似`ctx_value = ctx_value["as_item"]`。该特性通常在需要遍历多个组件时使用,可以有效避免属性冲突。 | ||
|
||
<demo name="use_as_item" title="使用 as_item 参数"></demo> | ||
|
||
如果您需要为所有的列表项的组件都添加某些统一的属性,您还可以传入`context_value`参数,该参数会与列表项的上下文深度合并,共同传递给前端组件。 | ||
|
||
<demo name="use_context_value" title="使用 context_value 参数"></demo> | ||
|
||
### API | ||
|
||
| 属性 | 类型 | 默认值 | 描述 | | ||
| ------------- | ---- | ------ | ---------------- | | ||
| value | list | None | 组件的数据源 | | ||
| context_value | dict | None | 统一的上下文参数 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Each | ||
|
||
A helper rendering component that supports passing a list as a parameter and will traverse the wrapped components on the front end, injecting each item of the list into the current traversal context. The attributes of the wrapped components will be replaced by corresponding context properties on the front end (only supports components exported from `modelscope_studio`). | ||
|
||
## When to Use | ||
|
||
- When all components being traversed are `modelscope_studio` components or non-`modelscope_studio` components whose values do not bind to the traversed values. | ||
- When the length of the list to be traversed is uncertain. | ||
|
||
## Examples | ||
|
||
<demo name="basic"></demo> | ||
|
||
Each component within `modelscope_studio` has an `as_item` parameter (including the `Each` component itself). Specifying this parameter value allows filtering based directly on the context value, similar logically to `ctx_value = ctx_value["as_item"]`. This feature is typically used when multiple components need to be iterated over, effectively avoiding property conflicts. | ||
|
||
<demo name="use_as_item" title="Using the as_item Parameter"></demo> | ||
|
||
If you need to add certain unified properties to all list item components, you can also pass in the `context_value` parameter. This parameter will be deeply merged with the list item's context and passed together to the front-end component. | ||
|
||
<demo name="use_context_value" title="Using the context_value Parameter"></demo> | ||
|
||
### API | ||
|
||
| Attribute | Type | Default Value | Description | | ||
| ------------- | ---- | ------------- | ----------------------------- | | ||
| value | list | None | Data source for the component | | ||
| context_value | dict | None | Unified context parameters | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from helper.Docs import Docs | ||
|
||
docs = Docs(__file__) | ||
|
||
if __name__ == "__main__": | ||
docs.render().queue().launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import gradio as gr | ||
|
||
import modelscope_studio.components.antd as antd | ||
import modelscope_studio.components.base as ms | ||
|
||
data = [{ | ||
"message": "Success Text", | ||
"description": | ||
"Success Description Success Description Success Description Success Description", | ||
"type": "success" | ||
}, { | ||
"message": "Info Text", | ||
"description": | ||
"Info Description Info Description Info Description Info Description", | ||
"type": "info" | ||
}, { | ||
"message": "Warning Text", | ||
"description": | ||
"Warning Description Warning Description Warning Description Warning Description", | ||
"type": "warning" | ||
}, { | ||
"message": "Error Text", | ||
"description": | ||
"Error Description Error Description Error Description Error Description", | ||
"type": "error" | ||
}] | ||
|
||
with gr.Blocks() as demo: | ||
with ms.Application(): | ||
with antd.Space(direction="vertical"): | ||
with ms.Each(value=data): | ||
antd.Alert() | ||
|
||
if __name__ == "__main__": | ||
demo.queue().launch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import gradio as gr | ||
|
||
import modelscope_studio.components.antd as antd | ||
import modelscope_studio.components.base as ms | ||
|
||
data = [{ | ||
"card": { | ||
"title": "Success Card Title", | ||
}, | ||
"each": { | ||
"value": [{ | ||
"value": "Card Content" | ||
}, { | ||
"value": "Card Content" | ||
}, { | ||
"value": "Card Content" | ||
}] | ||
} | ||
}, { | ||
"card": { | ||
"title": "Info Card Title", | ||
}, | ||
"each": { | ||
"value": [{ | ||
"value": "Card Content" | ||
}, { | ||
"value": "Card Content" | ||
}, { | ||
"value": "Card Content" | ||
}] | ||
} | ||
}, { | ||
"card": { | ||
"title": "Warning Card Title", | ||
}, | ||
"each": { | ||
"value": [{ | ||
"value": "Card Content" | ||
}, { | ||
"value": "Card Content" | ||
}, { | ||
"value": "Card Content" | ||
}] | ||
} | ||
}, { | ||
"card": { | ||
"title": "Error Card Title", | ||
}, | ||
"each": { | ||
"value": [{ | ||
"value": "Card Content" | ||
}, { | ||
"value": "Card Content" | ||
}, { | ||
"value": "Card Content" | ||
}] | ||
} | ||
}] | ||
|
||
with gr.Blocks() as demo: | ||
with ms.Application(): | ||
with ms.Each(value=data): | ||
with antd.Card(as_item="card"): | ||
# Inner loop | ||
with ms.Each(as_item="each"): | ||
antd.Typography.Paragraph() | ||
|
||
if __name__ == "__main__": | ||
demo.queue().launch() |
Oops, something went wrong.