Skip to content

Commit 1fe29ac

Browse files
feat: ui element for render react component
1 parent 74abfa1 commit 1fe29ac

File tree

7 files changed

+30
-40
lines changed

7 files changed

+30
-40
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ with ui.element("card", key="base_ele") as card:
6666

6767
<img width="735" alt="POC example" src="https://github.com/ObservedObserver/streamlit-shadcn-ui/assets/22167673/ace9670f-64a4-4417-973e-7f8a86c704e2">
6868

69+
## Reference
70+
+ [streamlit-shadcn-ui examples and docs repo](https://github.com/ObservedObserver/steamlit-shadcn-ui-docs)
71+
+ [Streamlit](https://streamlit.io/)
72+
+ [shadcn-ui](https://ui.shadcn.com/)
73+
6974
# License
7075
This repo is under MIT license. See [LICENSE](LICENSE) for details.
7176
`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.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "streamlit-shadcn-ui"
7-
version = "0.1.10"
7+
version = "0.1.11"
88
readme = "README.md"
99
keywords = ["streamlit", "shadcn", "ui", "components"]
1010
description = "Using shadcn components in Streamlit"

streamlit_shadcn_ui/components/packages/frontend/src/App.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ function App(props: ComponentProps<{comp: string; props: any; [key: string]: any
5858
const { args, width, disabled, theme } = props;
5959
const container = useRef(null);
6060

61-
console.log(props)
62-
6361
useEffect(() => {
6462
if (container.current) {
6563
Streamlit.setFrameHeight(container.current.offsetHeight + 10);

streamlit_shadcn_ui/components/packages/frontend/src/components/streamlit/card.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ interface StCardProps {
1111
content?: string;
1212
description?: string;
1313
children?: React.ReactNode;
14+
className?: string;
1415
}
1516
export const StCard = forwardRef<HTMLDivElement, StCardProps>(
1617
(props: StCardProps, ref) => {
17-
const { title, content, description, children } = props;
18+
const { title, content, description, children, className } = props;
1819
return (
19-
<Card ref={ref}>
20+
<Card className={`m-2 ${className}`} ref={ref}>
2021
<CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
2122
<CardTitle className="text-sm font-medium">
2223
{title}

streamlit_shadcn_ui/components/packages/frontend/src/components/streamlit/element/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ function dfsRender (tree: IElementTree | string) {
1616
}
1717
const children = tree.children?.map(child => typeof child === "string" ? child : dfsRender(child)) ?? [];
1818
const ele = getComponent(tree.name) || tree.name
19-
console.log('ele', ele)
2019
return createElement(ele, tree.props, ...children)
2120
}
2221
export const ElementRenderer = forwardRef<HTMLDivElement, ElementRendererProps>((props, ref) => {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const ComponentCollection = {
1010
} as const;
1111

1212
export function getComponent (componentName: string) {
13-
console.log(componentName, ComponentCollection)
1413
if (componentName in ComponentCollection) {
1514
return ComponentCollection[componentName]
1615
}
Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,42 @@
1+
from typing import List, Optional, Any, Dict
12
from streamlit_shadcn_ui.py_components.utils.declare import declare_component
23
from .context import get_context
3-
component_func = declare_component("element_renderer", release=False)
4+
5+
__RELEASE = True
6+
component_func = declare_component("element_renderer", release=__RELEASE)
47

58
class UIElement:
6-
def __init__(self, name: str, props=None, key=None):
9+
def __init__(self, name: str, props: Optional[Dict[str, Any]] = None, key: Optional[str] = None):
710
self.key = key
8-
self.props = props
11+
self.props = props if props is not None else {}
912
self.name = name
10-
self.children = []
13+
self.children: List['UIElement'] = []
1114
self.is_root = False
1215

13-
def renderTree(self, tree):
14-
c = component_func(comp="element_renderer", props={
15-
"tree": tree,
16-
}, key=self.key, default=None)
17-
return c
16+
def render_tree(self, tree: Dict[str, Any]) -> Any:
17+
return component_func(comp="element_renderer", props={"tree": tree}, key=self.key, default=None)
1818

19-
def render(self):
20-
tree = {
19+
def render(self) -> Dict[str, Any]:
20+
return {
2121
"name": self.name,
2222
"props": self.props,
23-
"children": []
23+
"children": [child.render() for child in self.children]
2424
}
25-
for child in self.children:
26-
tree["children"].append(child.render())
27-
print(tree)
28-
return tree
2925

30-
def __enter__(self):
26+
def __enter__(self) -> 'UIElement':
3127
ctx = get_context()
32-
if (ctx["in_render"] is False):
28+
if not ctx["in_render"]:
3329
ctx["in_render"] = True
3430
self.is_root = True
3531
return self
3632

37-
def __exit__(self, exc_type, exc_val, exc_tb):
38-
tree = self.render()
39-
ctx = get_context()
40-
if (self.is_root):
41-
ctx["in_render"] = False
42-
return self.renderTree(tree)
43-
return
33+
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
34+
if self.is_root:
35+
get_context()["in_render"] = False
36+
self.render_tree(self.render())
4437

45-
def add_child(self, child):
38+
def add_child(self, child: 'UIElement') -> None:
4639
self.children.append(child)
4740

48-
49-
50-
# def ui_element():
51-
# case 1. 任意的shadcn ui组件
52-
53-
def element(name: str, key=None, **props):
54-
return UIElement(name=name, props=props, key=key)
41+
def element(name: str, key: Optional[str] = None, **props) -> UIElement:
42+
return UIElement(name=name, props=props, key=key)

0 commit comments

Comments
 (0)