Py.Cafe

maartenbreddels/

ipyantd-company-styling

Mock Company Style Chooser - ipyantd

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# run as: $ solara run examples/demo.py
import ipyantd.components as antd
import ipyantd.icons.components as icons

import solara
import ipyreact


slider_range = solara.reactive([20, 70])
multi_select = solara.reactive(["apple", "pear"])
switch = solara.reactive(True)
style_index = solara.reactive(1)
message = solara.reactive(None)
dialog = solara.reactive(False)
slider = solara.reactive(0)
select = solara.reactive("pear")

styles = [
    {
        "colorPrimary": "#e50815",
        "borderRadius": 8,
        "dark": True,
        "name": "TubeFlix",
    },
    {
        "colorPrimary": "#76b900",
        "borderRadius": 8,
        "dark": False,
        "name": "FooBar",
    },
    {
        "colorPrimary": "#03857d",
        "name": "ACME",
    },
]






def dialog_open(event_data):
    message.value = "Modal opened"
    dialog.value = True


def dialog_close(event_data):
    message.value = "Modal closed"
    dialog.value = False


def report_click(event_data=None):
    message.value = "Clicked"


options = [
    dict(label="Apple", value="apple"),
    dict(label="Pear", value="pear"),
    dict(label="Cherry", value="cherry"),
]

menu_items = [
    dict(
        key="1",
        label=ipyreact.Widget.element(_type="div", children=["Click me 1"], events={"onClick": report_click}),
    ),
    dict(
        key="2",
        label=ipyreact.Widget.element(_type="div", children=["Click me 2"], events={"onClick": report_click}),
    ),
]
text = """Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. 
Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. 
Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. 
Duis semper.
Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue.
"""




def report_click(event_data=None):
    print("clicked")


options = [
    dict(label="Apple", value="apple"),
    dict(label="Pear", value="pear"),
    dict(label="Cherry", value="cherry"),
]



class ConfigProvider(ipyreact.ValueWidget):
    _esm = """
    import React from "react";
    import {ConfigProvider, theme} from "antd";

    export default ({borderRadius, colorPrimary, dark, children, ...rest}) => {
        return <ConfigProvider {...rest}
            theme={{
                algorithm: dark ? theme.darkAlgorithm : theme.defaultAlgorithm,
                token: {
                    // Seed Token
                    colorPrimary: colorPrimary,
                    borderRadius,

                    // Alias Token
                    //colorBgContainer: '#f6ffed',
                },
                }}
        >{children}</DatePicker>
    }
    
    """


class PreviewComponent(ipyreact.ValueWidget):
    _esm = """
import React from 'react';
import { Button, theme } from 'antd';

const { useToken } = theme;

const App: React.FC = ({selected, children, ...props}) => {
  const { token } = useToken();

  return (
    <div
      style={{
        backgroundColor: token.colorPrimaryBg,
        padding: token.padding,
        border: selected ? "1px solid #333" :  null,
        borderRadius: token.borderRadius,
        color: token.colorPrimaryText,
        fontSize: "40px",
        cursor: "pointer",
        margin: "5px",
        padding: "25px",
      }} {...props}
    >
      {...(children.length ? children : [children])}
    </div>
  );
};

export default App;
"""


@solara.component
def Div(children=[]):
    return ipyreact.Widget.element(_type="div", children=children)


@solara.component
def Text(text="", props={}):
    return ipyreact.Widget.element(_type="span", children=[text], props=props)


@solara.component
def Page():
    Div(children=["Pick a mock company style style:"])
    style = styles[style_index.value]
    with ConfigProvider.element(props=style):
        with antd.Row(
            props=dict(gutter=[1, 1], style={"padding": "10px", "margin": "20px"})
        ):
            for index, other_style in enumerate(styles):
                with ConfigProvider.element(props=other_style):

                    def pick(*ignore, index=index):
                        style_index.value = index

                    with PreviewComponent.element(
                        events={"onClick": pick},
                        props={"selected": index == style_index.value},
                        children=[],
                    ):
                        Text(other_style["name"])
        Div(children=["Style preview:"])
        background = "#222" if style.get("dark", False) else "white"
        with antd.Col(
            props=dict(
                style={
                    "margin": "20px",
                    "borderRadius": "20px",
                    "backgroundColor": background,
                    "border": "1px solid " + style["colorPrimary"],
                }
            )
        ):
                with antd.Row(props=dict(gutter=[48, 48], style={"padding": "100px"})):
                    # buttons
                    with antd.Col(props=dict(span=8, style={"boxSizing": "border-box"})):
                        with ipyreact.Widget.element(_type="div"):
                            antd.Button(children=["Button"], props=dict(type="primary"), events={"onClick": report_click})
                            antd.Button(children=["Button"])
                    with antd.Col(props=dict(span=8)):
                        antd.Button(children=["Button"], props=dict(type="primary", shape="round"))
                        antd.Button(props=dict(type="primary", shape="circle", icon=icons.Icon("SearchOutlined")))
                        antd.Button(props=dict(shape="circle", icon=icons.Icon("SearchOutlined")))
                    with antd.Col(props=dict(span=8)):
                        antd.Button(children=["Button"], props=dict(type="text"))
                    with antd.Col(props=dict(span=8)):
                        antd.Button(children=["Button"], props=dict(type="primary", size="small"))
                        antd.Button(children=["Button"], props=dict(size="small"))
                    with antd.Col(props=dict(span=8)):
                        antd.Button(children=["Button"], props=dict(type="primary", shape="round", size="small"))
                        antd.Button(props=dict(type="primary", shape="circle", icon=icons.Icon("SearchOutlined"), size="small"))
                        antd.Button(props=dict(shape="circle", icon=icons.Icon("SearchOutlined"), size="small"))
                    with antd.Col(props=dict(span=8)):
                        antd.Button(children=["Button"], props=dict(type="text", size="small"))

                    # sliders
                    with antd.Col(props=dict(span=8)):
                        antd.Slider(value=slider.value, on_value=slider.set, props=dict(min=0, max=100))
                    with antd.Col(props=dict(span=8)):
                        antd.Slider(value=slider_range.value, on_value=slider_range.set, props=dict(min=0, max=100, range=True))
                    with antd.Col(props=dict(span=8)):
                        antd.Slider(value=slider.value, on_value=slider.set, props=dict(min=0, max=100, tooltip=dict(open=True)))

                    # select
                    with antd.Col(props=dict(span=8)):
                        antd.Select(value=select.value, on_value=select.set, props=dict(options=options, style=dict(width="120px")))
                    with antd.Col(props=dict(span=8)):
                        antd.Select(value=multi_select.value, on_value=multi_select.set, props=dict(options=options, style=dict(width="220px"), mode="multiple"))
                    with antd.Col(props=dict(span=8)):
                        pass
                        # only supported in 5.31+
                        # antd.Select(value="pear", props=dict(options=options, variant="filled", style=dict(width="120px"))),

                    # misc
                    with antd.Col(props=dict(span=8)):
                        antd.Switch(value=switch.value, on_value=switch.set)
                    with antd.Col(props=dict(span=8)):
                        with antd.Dropdown(props=dict(menu=dict(items=menu_items), trigger=["click"])):
                            ipyreact.Widget.element(_type="span", children=["Menu"])
                    with antd.Col(props=dict(span=8)):
                        with antd.Modal(value=dialog.value, events={"onOk": dialog_close, "onCancel": dialog_close}):
                            ipyreact.Widget.element(_type="div", children=[text])
                        antd.Button(children=["Dialog"], props=dict(type="primary"), events={"onClick": dialog_open})