Skip to content

Commit 9abf9a9

Browse files
feat(poc): py render proposal
1 parent bd83960 commit 9abf9a9

File tree

4 files changed

+58
-43
lines changed

4 files changed

+58
-43
lines changed

Home.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from streamlit_shadcn_ui import slider, input, textarea, radio_group, switch
99
from streamlit_extras.stylable_container import stylable_container
1010

11-
ui.element("test")
12-
1311
ui_result = ui.button("Button", key="btn")
1412
st.write("UI Button Clicked:", ui_result)
1513

@@ -49,4 +47,11 @@
4947

5048
st.header("Button Component")
5149
trigger_btn = ui.button(text="Trigger Button", key="trigger_btn")
52-
ui.alert_dialog(show=trigger_btn, title="Alert Dialog", description="This is an alert dialog", confirm_label="OK", cancel_label="Cancel", key="alert_dialog1")
50+
ui.alert_dialog(show=trigger_btn, title="Alert Dialog", description="This is an alert dialog", confirm_label="OK", cancel_label="Cancel", key="alert_dialog1")
51+
52+
with ui.element("card", key="base_ele") as card:
53+
with ui.element("card", key="base_ele2") as card2:
54+
card2.add_child(ui.element("input", key="nst2_input", label="Value"))
55+
card2.add_child(ui.element("button", key="nst2_btn", text="Nest Submmit", variant="outline"))
56+
card.add_child(card2)
57+
card.add_child(ui.element("button", key="nst_btn", text="Hello World"))

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ Check docs and compoenent examples in [![Streamlit App](https://static.streamlit
5050
+ [x] badges
5151
+ [x] link_button
5252

53+
## One more thing
54+
There is a new component in testing, it will allows you to nest all streamlit-shadcn-ui components together.
55+
56+
POC:
57+
```py
58+
with ui.element("card", key="base_ele") as card:
59+
with ui.element("card", key="base_ele2") as card2:
60+
card2.add_child(ui.element("input", key="nst2_input"))
61+
card2.add_child(ui.element("button", key="nst2_btn", text="Nest Submmit", variant="outline"))
62+
card.add_child(card2)
63+
card.add_child(ui.element("button", key="nst_btn", text="Hello World"))
64+
```
65+
5366
# License
5467
This repo is under MIT license. See [LICENSE](LICENSE) for details.
5568
`streamlit_shadcn_ui/components/packages/streamlit-components-lib` is under its original Apache-2.0 license. It is a temporal patch for streamlit-components-lib in react 18.

streamlit_shadcn_ui/components/packages/frontend/src/components/streamlit/element/registerComponents.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11

2+
import { StButton } from "../button";
23
import { StCard } from "../card"
4+
import { StInput } from "../input";
35

46
const ComponentCollection = {
5-
card: StCard
7+
card: StCard,
8+
button: StButton,
9+
input: StInput,
610
} as const;
711

812
export function getComponent (componentName: string) {
Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,45 @@
11
from streamlit_shadcn_ui.py_components.utils.declare import declare_component
22

3+
component_func = declare_component("element_renderer", release=False)
34

45
class UIElement:
5-
def __init__(self, key, value):
6+
def __init__(self, name: str, props=None, key=None):
67
self.key = key
7-
self.value = value
8+
self.props = props
9+
self.name = name
10+
self.children = []
11+
12+
def renderTree(self, tree):
13+
c = component_func(comp="element_renderer", props={
14+
"tree": tree,
15+
}, key=self.key, default=None)
16+
return c
817

918
def render(self):
10-
raise NotImplementedError
11-
12-
def get_key(self):
13-
return self.key
14-
15-
def get_value(self):
16-
return self.value
19+
tree = {
20+
"name": self.name,
21+
"props": self.props,
22+
"children": []
23+
}
24+
for child in self.children:
25+
tree["children"].append(child.render())
26+
print(tree)
27+
return tree
28+
29+
def __enter__(self):
30+
return self
31+
32+
def __exit__(self, exc_type, exc_val, exc_tb):
33+
tree = self.render()
34+
return self.renderTree(tree)
35+
36+
def add_child(self, child):
37+
self.children.append(child)
1738

18-
def set_value(self, value):
19-
self.value = value
20-
21-
def set_key(self, key):
22-
self.key = key
23-
24-
def __str__(self):
25-
return f"{self.key}: {self.value}"
26-
27-
def __repr__(self):
28-
return f"{self.key}: {self.value}"
29-
30-
component_func = declare_component("element_renderer", release=False)
3139

3240

3341
# def ui_element():
3442
# case 1. 任意的shadcn ui组件
3543

36-
def element(key):
37-
tree = {
38-
"name": "card",
39-
"props": {
40-
"className": "bg-red-500"
41-
},
42-
"children": ["Hello World", {
43-
"name": "input",
44-
"props": {
45-
"className": "bg-yellow-500"
46-
}
47-
}]
48-
}
49-
c = component_func(comp="element_renderer", props={
50-
"tree": tree,
51-
}, key=key, default=None)
52-
return c
44+
def element(name: str, key=None, **props):
45+
return UIElement(name=name, props=props, key=key)

0 commit comments

Comments
 (0)