MartinT commited on
Commit
dc40b29
1 Parent(s): 4912181

feat: Init.

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. Dockerfile +12 -0
  3. app.py +287 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN python -m venv venv
7
+ RUN ./venv/bin/pip install h2o-wave
8
+
9
+ ENV H2O_WAVE_LISTEN=":7860"
10
+ ENV H2O_WAVE_ADDRESS="http://127.0.0.1:7860"
11
+
12
+ CMD ["wave run app.py"]
app.py ADDED
@@ -0,0 +1,287 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Theme generator
2
+ # Use theme generator to quickly generate custom color schemes for your app.
3
+ # #theme_generator
4
+ # ---
5
+ import math
6
+ from typing import Tuple
7
+
8
+ from h2o_wave import Q, app, data, main, ui
9
+
10
+
11
+ def to_grayscale(color: float) -> float:
12
+ color /= 255
13
+ return color / 12.92 if color <= 0.03928 else math.pow((color + 0.055) / 1.055, 2.4)
14
+
15
+
16
+ def get_luminance(r: float, g: float, b: float) -> float:
17
+ return to_grayscale(r) * 0.2126 + to_grayscale(g) * 0.7152 + to_grayscale(b) * 0.0722
18
+
19
+
20
+ # Source: https://www.delftstack.com/howto/python/python-hex-to-rgb/.
21
+ def hex_to_rgb(hex_color: str) -> Tuple[int, ...]:
22
+ if len(hex_color) == 3:
23
+ hex_color = f'{hex_color[0]}{hex_color[0]}{hex_color[1]}{hex_color[1]}{hex_color[2]}{hex_color[2]}'
24
+ return tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4))
25
+
26
+
27
+ # Source: https://stackoverflow.com/questions/9733288/how-to-programmatically-calculate-the-contrast-ratio-between-two-colors. # noqa
28
+ def update_contrast_check(color1: str, color2: str, q: Q, min_contrast=4.5):
29
+ rgb1 = hex_to_rgb(q.client[color1].lstrip('#'))
30
+ rgb2 = hex_to_rgb(q.client[color2].lstrip('#'))
31
+ lum1 = get_luminance(rgb1[0], rgb1[1], rgb1[2])
32
+ lum2 = get_luminance(rgb2[0], rgb2[1], rgb2[2])
33
+ brightest = max(lum1, lum2)
34
+ darkest = min(lum1, lum2)
35
+ contrast = (brightest + 0.05) / (darkest + 0.05)
36
+ message_bar_mobile = q.page['meta'][f'{color1}_{color2}']
37
+ message_bar = q.page['form'][f'{color1}_{color2}']
38
+ if contrast < min_contrast:
39
+ message_bar.type = message_bar_mobile.type = 'error'
40
+ message_bar.text = message_bar_mobile.text = f'Improve contrast between **{color1}** and **{color2}**.'
41
+ else:
42
+ message_bar.type = message_bar_mobile.type = 'success'
43
+ message_bar.text = message_bar_mobile.text = f'Contrast between **{color1}** and **{color2}** is great!'
44
+
45
+
46
+ def get_theme_code(q: Q):
47
+ return f'''
48
+ ```py
49
+ ui.theme(
50
+ name='<theme-name>',
51
+ primary='{q.client.primary}',
52
+ text='{q.client.text}',
53
+ card='{q.client.card}',
54
+ page='{q.client.page}',
55
+ )
56
+ ```
57
+ '''
58
+
59
+
60
+ image = 'https://images.pexels.com/photos/220453/pexels-photo-220453.jpeg?auto=compress&h=750&w=1260'
61
+ mobile_items = [
62
+ ui.text_xl(content='Sample App to show colors'),
63
+ ui.text('Click the top right button to change the theme. 👆'),
64
+ ui.inline(justify='between', items=[
65
+ ui.persona(title='John Doe', subtitle='Data Scientist', size='s', image=image),
66
+ ui.toggle(name='toggle', label='Toggle', value=True),
67
+ ]),
68
+ ui.inline([
69
+ ui.stepper(name='stepper', width='500px', items=[
70
+ ui.step(label='Step 1', icon='MailLowImportance'),
71
+ ui.step(label='Step 2', icon='TaskManagerMirrored'),
72
+ ui.step(label='Step 3', icon='Cafe'),
73
+ ]),
74
+ ]),
75
+ ui.progress(label='A progress bar'),
76
+ ui.inline(justify='between', items=[
77
+ ui.tabs(name='menu', value='email', items=[
78
+ ui.tab(name='email', label='Mail', icon='Mail'),
79
+ ui.tab(name='events', label='Events', icon='Calendar'),
80
+ ui.tab(name='spam', label='Spam'),
81
+ ]),
82
+ ui.link(label='Link'),
83
+ ]),
84
+ ui.slider(name='slider', label='Slider', value=70),
85
+ ui.date_picker(name='date_picker', label='Date picker'),
86
+ ui.picker(name='picker', label='Picker', choices=[
87
+ ui.choice('choice1', label='Choice 1'),
88
+ ui.choice('choice2', label='Choice 2'),
89
+ ui.choice('choice3', label='Choice 3'),
90
+ ]),
91
+ ui.combobox(name='combobox', label='Combobox', choices=['Choice 1', 'Choice 2', 'Choice 3']),
92
+ ui.checkbox(name='checkbox1', label='Checkbox 1', value=True),
93
+ ui.checkbox(name='checkbox2', label='Checkbox 2'),
94
+ ui.checkbox(name='checkbox3', label='Checkbox 3'),
95
+ ui.inline(direction='column', items=[
96
+ ui.table(
97
+ name='table',
98
+ width='100%',
99
+ columns=[
100
+ ui.table_column(name='name', label='Name', min_width='80px'),
101
+ ui.table_column(name='surname', label='Surname', filterable=True, max_width='90px'),
102
+ ui.table_column(name='progress', label='Progress', max_width='80px',
103
+ cell_type=ui.progress_table_cell_type(color='$themePrimary')),
104
+ ],
105
+ rows=[
106
+ ui.table_row(name='row1', cells=['John', 'Doe', '0.90']),
107
+ ui.table_row(name='row2', cells=['Ann', 'Doe', '0.75']),
108
+ ],
109
+ ),
110
+ ui.visualization(
111
+ width='100%',
112
+ data=data('profession salary', 5, rows=[
113
+ ('medicine', 23000),
114
+ ('fire fighting', 18000),
115
+ ('pedagogy', 24000),
116
+ ('psychology', 22500),
117
+ ('computer science', 36000),
118
+ ], pack=True),
119
+ plot=ui.plot([ui.mark(type='interval', x='=profession', y='=salary', y_min=0)])
120
+ ),
121
+ ]),
122
+ ui.buttons([
123
+ ui.button(name='primary_button', label='Primary', primary=True),
124
+ ui.button(name='standard_button', label='Standard'),
125
+ ui.button(name='standard_disabled_button', label='Disabled', disabled=True),
126
+ ui.button(name='icon_button', icon='Heart', caption='Tooltip text'),
127
+ ]),
128
+ ]
129
+ desktop_items = [
130
+ ui.text_xl(content='Sample App to show colors'),
131
+ ui.progress(label='A progress bar'),
132
+ ui.inline([
133
+ ui.checkbox(name='checkbox1', label='Checkbox 1', value=True),
134
+ ui.checkbox(name='checkbox2', label='Checkbox 2'),
135
+ ui.checkbox(name='checkbox3', label='Checkbox 3'),
136
+ ui.toggle(name='toggle', label='Toggle', value=True),
137
+ ]),
138
+ ui.inline([
139
+ ui.date_picker(name='date_picker', label='Date picker'),
140
+ ui.picker(name='picker', label='Picker', choices=[
141
+ ui.choice('choice1', label='Choice 1'),
142
+ ui.choice('choice2', label='Choice 2'),
143
+ ui.choice('choice3', label='Choice 3'),
144
+ ]),
145
+ ui.combobox(name='combobox', label='Combobox', choices=['Choice 1', 'Choice 2', 'Choice 3']),
146
+ ui.persona(title='John Doe', subtitle='Data Scientist', size='s', image=image),
147
+ ]),
148
+ ui.slider(name='slider', label='Slider', value=70),
149
+ ui.link(label='Link'),
150
+ ui.inline(justify='between', items=[
151
+ ui.stepper(name='stepper', width='500px', items=[
152
+ ui.step(label='Step 1', icon='MailLowImportance'),
153
+ ui.step(label='Step 2', icon='TaskManagerMirrored'),
154
+ ui.step(label='Step 3', icon='Cafe'),
155
+ ]),
156
+ ui.tabs(name='menu', value='email', items=[
157
+ ui.tab(name='email', label='Mail', icon='Mail'),
158
+ ui.tab(name='events', label='Events', icon='Calendar'),
159
+ ui.tab(name='spam', label='Spam'),
160
+ ]),
161
+ ]),
162
+ ui.inline(items=[
163
+ ui.table(
164
+ name='table',
165
+ width='50%',
166
+ columns=[
167
+ ui.table_column(name='name', label='Name', min_width='80px'),
168
+ ui.table_column(name='surname', label='Surname', filterable=True),
169
+ ui.table_column(name='age', label='Age', sortable=True, max_width='80px'),
170
+ ui.table_column(name='progress', label='Progress',
171
+ cell_type=ui.progress_table_cell_type(color='$themePrimary')),
172
+ ],
173
+ rows=[
174
+ ui.table_row(name='row1', cells=['John', 'Doe', '25', '0.90']),
175
+ ui.table_row(name='row2', cells=['Ann', 'Doe', '35', '0.75']),
176
+ ui.table_row(name='row3', cells=['Casey', 'Smith', '40', '0.33']),
177
+ ],
178
+ height='330px',
179
+ ),
180
+ ui.visualization(
181
+ width='50%',
182
+ data=data('profession salary', 5, rows=[
183
+ ('medicine', 23000),
184
+ ('fire fighting', 18000),
185
+ ('pedagogy', 24000),
186
+ ('psychology', 22500),
187
+ ('computer science', 36000),
188
+ ], pack=True),
189
+ plot=ui.plot([ui.mark(type='interval', x='=profession', y='=salary', y_min=0)])
190
+ ),
191
+ ]),
192
+ ui.buttons([
193
+ ui.button(name='primary_button', label='Primary', primary=True),
194
+ ui.button(name='standard_button', label='Standard'),
195
+ ui.button(name='standard_disabled_button', label='Disabled', disabled=True),
196
+ ui.button(name='icon_button', icon='Heart', caption='Tooltip text'),
197
+ ]),
198
+ ]
199
+
200
+
201
+ @app('/')
202
+ async def serve(q: Q):
203
+ if not q.client.initialized:
204
+ q.client.primary = '#000000'
205
+ q.client.page = '#e2e2e2'
206
+ q.client.card = '#ffffff'
207
+ q.client.text = '#000000'
208
+ q.page['meta'] = ui.meta_card(box='', theme='custom', layouts=[
209
+ ui.layout(
210
+ breakpoint='xs',
211
+ zones=[
212
+ ui.zone('mobile_content'),
213
+ ui.zone('footer')
214
+ ]
215
+ ),
216
+ ui.layout(
217
+ breakpoint='817px',
218
+ zones=[
219
+ ui.zone('header'),
220
+ ui.zone('content', direction=ui.ZoneDirection.ROW, zones=[
221
+ ui.zone('colors', size='30%'),
222
+ ui.zone('preview', size='70%')
223
+ ]),
224
+ ui.zone('footer')
225
+ ]
226
+ ),
227
+ ])
228
+ q.page['header'] = ui.header_card(box='header', title='Theme generator', subtitle='Color your app easily',
229
+ icon='Color', icon_color='$card')
230
+ q.page['mobile_header'] = ui.header_card(
231
+ box='mobile_content',
232
+ icon='Color',
233
+ title='Theme generator',
234
+ subtitle='Color your app easily',
235
+ items=[ui.button(name='show_side_panel', label=' ', icon='Color')]
236
+ )
237
+ q.client.color_items = [
238
+ ui.color_picker(name='primary', label='Primary', trigger=True, alpha=False, inline=True,
239
+ value=q.client.primary),
240
+ ui.color_picker(name='text', label='Text', trigger=True, alpha=False, inline=True, value=q.client.text),
241
+ ui.color_picker(name='card', label='Card', trigger=True, alpha=False, inline=True, value=q.client.card),
242
+ ui.color_picker(name='page', label='Page', trigger=True, alpha=False, inline=True, value=q.client.page),
243
+ ui.text_xl('Check contrast'),
244
+ ui.message_bar(name='text_card', type='success', text='Contrast between **text** and **card** is great!'),
245
+ ui.message_bar(name='card_primary', type='success',
246
+ text='Contrast between **card** and **primary** is great!'),
247
+ ui.message_bar(name='text_page', type='success', text='Contrast between **text** and **page** is great!'),
248
+ ui.message_bar(name='page_primary', type='success',
249
+ text='Contrast between **page** and **primary** is great!'),
250
+ ui.text_xl('Copy code'),
251
+ ui.text(name='code', content=get_theme_code(q)),
252
+ ]
253
+ q.page['form'] = ui.form_card(box='colors', items=q.client.color_items)
254
+ q.page['sample'] = ui.form_card(box='preview', items=desktop_items)
255
+ q.page['sample_mobile'] = ui.form_card(box='mobile_content', items=mobile_items)
256
+ q.page['footer'] = ui.footer_card(box='footer', caption='Made with 💛 by H2O Wave Team.')
257
+ q.client.themes = [ui.theme(name='custom', text=q.client.text, card=q.client.card,
258
+ page=q.client.page, primary=q.client.primary)]
259
+ q.client.initialized = True
260
+
261
+ if q.args.show_side_panel:
262
+ q.page['meta'].side_panel = ui.side_panel(
263
+ title='Adjust theme colors',
264
+ items=q.client.color_items,
265
+ closable=True,
266
+ width='min(75%, 420px)'
267
+ )
268
+ if q.args.primary:
269
+ q.client.themes[0].primary = q.args.primary
270
+ q.client.primary = q.args.primary
271
+ if q.args.text:
272
+ q.client.themes[0].text = q.args.text
273
+ q.client.text = q.args.text
274
+ if q.args.card:
275
+ q.client.themes[0].card = q.args.card
276
+ q.client.card = q.args.card
277
+ if q.args.page:
278
+ q.client.themes[0].page = q.args.page
279
+ q.client.page = q.args.page
280
+
281
+ q.page['meta'].themes = q.client.themes
282
+ update_contrast_check('text', 'card', q)
283
+ update_contrast_check('card', 'primary', q)
284
+ update_contrast_check('text', 'page', q)
285
+ update_contrast_check('page', 'primary', q)
286
+ q.page['form'].code.content = get_theme_code(q)
287
+ await q.page.save()