Coloring commited on
Commit
ba4a662
1 Parent(s): f3f2a15

feat: add Lifecycle component

Browse files
README-zh_CN.md CHANGED
@@ -29,4 +29,5 @@ pip install modelscope_studio
29
  - <tab-link component-tab="MultimodalInput">MultimodalInput</tab-link>
30
  - <tab-link component-tab="Markdown">Markdown</tab-link>
31
  - <tab-link component-tab="WaterfallGallery">WaterfallGallery</tab-link>
 
32
  - <tab-link component-tab="Flow">Flow</tab-link>
 
29
  - <tab-link component-tab="MultimodalInput">MultimodalInput</tab-link>
30
  - <tab-link component-tab="Markdown">Markdown</tab-link>
31
  - <tab-link component-tab="WaterfallGallery">WaterfallGallery</tab-link>
32
+ - <tab-link component-tab="Lifecycle">Lifecycle</tab-link>
33
  - <tab-link component-tab="Flow">Flow</tab-link>
README.md CHANGED
@@ -44,5 +44,6 @@ pip install modelscope_studio
44
  - <tab-link component-tab="Chatbot">Chatbot</tab-link>
45
  - <tab-link component-tab="MultimodalInput">MultimodalInput</tab-link>
46
  - <tab-link component-tab="Markdown">Markdown</tab-link>
 
47
  - <tab-link component-tab="WaterfallGallery">WaterfallGallery</tab-link>
48
  - <tab-link component-tab="Flow">Flow</tab-link>
 
44
  - <tab-link component-tab="Chatbot">Chatbot</tab-link>
45
  - <tab-link component-tab="MultimodalInput">MultimodalInput</tab-link>
46
  - <tab-link component-tab="Markdown">Markdown</tab-link>
47
+ - <tab-link component-tab="Lifecycle">Lifecycle</tab-link>
48
  - <tab-link component-tab="WaterfallGallery">WaterfallGallery</tab-link>
49
  - <tab-link component-tab="Flow">Flow</tab-link>
app.py CHANGED
@@ -3,6 +3,7 @@ from api.app import docs as api_docs
3
  from components.Chatbot.app import docs as chatbot_docs
4
  from components.Docs import Docs
5
  from components.Flow.app import docs as flow_docs
 
6
  from components.Markdown.app import docs as markdown_docs
7
  from components.MultimodalInput.app import docs as multimodel_input_docs
8
  from components.WaterfallGallery.app import docs as waterfall_gallery_docs
@@ -13,8 +14,9 @@ docs = [
13
  ["Quick Start", readme_docs],
14
  ["API", api_docs],
15
  ["Chatbot", chatbot_docs],
16
- ["Markdown", markdown_docs],
17
  ["MultimodalInput", multimodel_input_docs],
 
 
18
  ["WaterfallGallery", waterfall_gallery_docs],
19
  ["Flow", flow_docs],
20
  ]
 
3
  from components.Chatbot.app import docs as chatbot_docs
4
  from components.Docs import Docs
5
  from components.Flow.app import docs as flow_docs
6
+ from components.Lifecycle.app import docs as lifecycle_docs
7
  from components.Markdown.app import docs as markdown_docs
8
  from components.MultimodalInput.app import docs as multimodel_input_docs
9
  from components.WaterfallGallery.app import docs as waterfall_gallery_docs
 
14
  ["Quick Start", readme_docs],
15
  ["API", api_docs],
16
  ["Chatbot", chatbot_docs],
 
17
  ["MultimodalInput", multimodel_input_docs],
18
+ ["Markdown", markdown_docs],
19
+ ["Lifecycle", lifecycle_docs],
20
  ["WaterfallGallery", waterfall_gallery_docs],
21
  ["Flow", flow_docs],
22
  ]
components/Lifecycle/README-zh_CN.md ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Lifecycle
2
+
3
+ 生命周期组件,用于获取当前用户的环境信息。
4
+
5
+ - 获取当前用户的语言、页面主题、user agent 和屏幕状态。
6
+ - 监听页面行为并触发相应事件(页面加载、尺寸变化、页面关闭等)。
7
+
8
+ ## 如何使用
9
+
10
+ ### 基本使用
11
+
12
+ <demo name="basic"></demo>
13
+
14
+ ### 自动适配用户语言环境
15
+
16
+ <demo name="language_adaptation"></demo>
17
+
18
+ ### 根据用户界面主题返回不同权重内容
19
+
20
+ <demo name="theme_adaptation"></demo>
21
+
22
+ ## API 及参数列表
23
+
24
+ ### value
25
+
26
+ 接口定义:
27
+
28
+ ```python
29
+ class LifecycleScreenData(GradioModel):
30
+ width: float
31
+ height: float
32
+ scrollX: float
33
+ scrollY: float
34
+
35
+
36
+ class LifecycleData(GradioModel):
37
+ screen: LifecycleScreenData
38
+ language: str
39
+ theme: str
40
+ userAgent: str
41
+ ```
42
+
43
+ ### props
44
+
45
+ 该组件不支持传入 props。
46
+
47
+ ### event listeners
48
+
49
+ | 事件 | 描述 |
50
+ | -------------------------------- | --------------------------------------------------------------------- |
51
+ | `mgr.Lifecycle.mount(fn, ···)` | 用户页面加载时触发,EventData 为当前组件 value 的 dict 类型值。 |
52
+ | `mgr.Lifecycle.unmount(fn, ···)` | 用户页面关闭时触发,EventData 为当前组件 value 的 dict 类型值。 |
53
+ | `mgr.Lifecycle.resize(fn, ···)` | 自定义标签触发事件时触发,EventData 为当前组件 value 的 dict 类型值。 |
components/Lifecycle/README.md ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Lifecycle
2
+
3
+ A Lifecycle component for getting the current user's environment information.
4
+
5
+ - Get the current user's language, page theme, user agent, and screen state.
6
+ - Listen to page actions and trigger corresponding events (page loading, size changes, page closing, etc.).
7
+
8
+ ## How to Use
9
+
10
+ ### Basic Usage
11
+
12
+ <demo name="basic"></demo>
13
+
14
+ ### Automatically Adapt to User Language Environment
15
+
16
+ <demo name="language_adaptation"></demo>
17
+
18
+ ### Return Different Weighted Content Based on UI Theme
19
+
20
+ <demo name="theme_adaptation"></demo>
21
+
22
+ ## API and Parameter List
23
+
24
+ ### Value
25
+
26
+ Interface definition:
27
+
28
+ ```python
29
+ class LifecycleScreenData(GradioModel):
30
+ width: float
31
+ height: float
32
+ scrollX: float
33
+ scrollY: float
34
+
35
+
36
+ class LifecycleData(GradioModel):
37
+ screen: LifecycleScreenData
38
+ language: str
39
+ theme: str
40
+ userAgent: str
41
+ ```
42
+
43
+ ### Props
44
+
45
+ This component does not support passing in props.
46
+
47
+ ### Event Listeners
48
+
49
+ | Event | Description |
50
+ | -------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
51
+ | `mgr.Lifecycle.mount(fn, ···)` | Triggered when the user's page loads. The EventData is a dictionary type value of the current component's value. |
52
+ | `mgr.Lifecycle.unmount(fn, ···)` | Triggered when the user's page closes. The EventData is a dictionary type value of the current component's value. |
53
+ | `mgr.Lifecycle.resize(fn, ···)` | Triggered when custom labels trigger events. The EventData is a dictionary type value of the current component's value. |
components/Lifecycle/app.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ from components.Docs import Docs
2
+
3
+ docs = Docs(__file__)
4
+
5
+ if __name__ == "__main__":
6
+ docs.render().queue().launch()
components/Lifecycle/demos/basic.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import modelscope_studio as mgr
4
+
5
+
6
+ def mount(e: gr.EventData):
7
+ # current page state
8
+ print("onMount", e._data)
9
+
10
+
11
+ def resize(e: gr.EventData):
12
+ print("onResize", e._data)
13
+
14
+
15
+ def onUnmount(e: gr.EventData):
16
+ print("onUnmount", e._data)
17
+
18
+
19
+ with gr.Blocks() as demo:
20
+ gr.Markdown("The Lifecycle component will not be rendered on the page.")
21
+ lifecycle = mgr.Lifecycle()
22
+ # listen to the page lifecycle
23
+ lifecycle.mount(fn=mount)
24
+ lifecycle.resize(fn=resize)
25
+ lifecycle.unmount(fn=onUnmount)
26
+
27
+ if __name__ == "__main__":
28
+ demo.queue().launch()
components/Lifecycle/demos/language_adaptation.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+
3
+ import gradio as gr
4
+
5
+ import modelscope_studio as mgr
6
+
7
+ messages = {
8
+ 'en': {
9
+ "hello": "Hello"
10
+ },
11
+ 'en-US': {
12
+ "hello": "Hello"
13
+ },
14
+ 'zh-CN': {
15
+ "hello": "你好"
16
+ }
17
+ }
18
+
19
+ default_lang = "en"
20
+
21
+
22
+ def mount(_lifecycle, _state):
23
+ lang = _lifecycle.language
24
+ if (lang in messages):
25
+ _state["current_lang"] = lang
26
+ yield 'Switch Language...', _state
27
+ time.sleep(2)
28
+ yield messages[lang]["hello"], _state
29
+
30
+
31
+ with gr.Blocks() as demo:
32
+ lifecycle = mgr.Lifecycle()
33
+ state = gr.State({"current_lang": default_lang})
34
+ markdown = gr.Markdown(value=messages[default_lang]["hello"])
35
+
36
+ lifecycle.mount(fn=mount,
37
+ inputs=[lifecycle, state],
38
+ outputs=[markdown, state])
39
+
40
+ if __name__ == "__main__":
41
+ demo.queue().launch()
components/Lifecycle/demos/theme_adaptation.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import modelscope_studio as mgr
4
+
5
+
6
+ def mount(_lifecycle, _state):
7
+ _state["theme"] = _lifecycle.theme
8
+ yield _state
9
+
10
+
11
+ def fn(_state):
12
+ theme = _state["theme"]
13
+ color = '000/fff' if theme == 'dark' else 'fff/000'
14
+ yield gr.update(
15
+ value=f"https://dummyimage.com/200x100/{color}.png&text={theme}")
16
+
17
+
18
+ with gr.Blocks() as demo:
19
+ lifecycle = mgr.Lifecycle()
20
+ state = gr.State({"theme": "light"})
21
+ btn = gr.Button()
22
+ image = gr.Image()
23
+
24
+ lifecycle.mount(fn=mount, inputs=[lifecycle, state], outputs=[state])
25
+ btn.click(fn=fn, inputs=[state], outputs=[image])
26
+
27
+ if __name__ == "__main__":
28
+ demo.queue().launch()
modelscope_studio-0.3.0-py3-none-any.whl → modelscope_studio-0.4.0-py3-none-any.whl RENAMED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:b64a48534196fea70a43e9b1cebf7dc881ae8138d10a3e520fa59b5477480967
3
- size 10950399
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4ea6dc30b6a751eff8b0e7e99554af09ad2b6d13841e8896e5e5af544d9425b7
3
+ size 10995416
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
  modelscope_studio
2
- modelscope_studio-0.3.0-py3-none-any.whl
 
1
  modelscope_studio
2
+ modelscope_studio-0.4.0-py3-none-any.whl