-
Notifications
You must be signed in to change notification settings - Fork 335
/
Copy path__init__.py
647 lines (460 loc) · 18.9 KB
/
__init__.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
"""
Component definitions.
All CamelCase names in the namespace should be components.
"""
import typing as _t
import pydantic as _p
import typing_extensions as _te
from pydantic_core import core_schema as _core_schema
from .. import class_name as _class_name
from .. import events
from .. import types as _types
from ..base import BaseModel
from .display import Details, Display
from .forms import (
Form,
FormField,
FormFieldBoolean,
FormFieldFile,
FormFieldInput,
FormFieldSelect,
FormFieldSelectSearch,
ModelForm,
)
from .tables import Pagination, Table
__all__ = (
# first we include all components from this file
'Text',
'Paragraph',
'PageTitle',
'Div',
'Page',
'Heading',
'Markdown',
'Code',
'Json',
'Button',
'Link',
'LinkList',
'Navbar',
'Modal',
'ServerLoad',
'Image',
'Iframe',
'FireEvent',
'Error',
'Spinner',
'Toast',
'Custom',
'DarkMode',
# then we include components from other files
'Table',
'Pagination',
'Display',
'Details',
'Form',
'FormField',
'ModelForm',
'Footer',
# then `AnyComponent` itself
'AnyComponent',
# then the other form field types which are included in `AnyComponent` via the `FormField` union
'FormFieldBoolean',
'FormFieldFile',
'FormFieldInput',
'FormFieldSelect',
'FormFieldSelectSearch',
)
class Text(BaseModel, extra='forbid'):
"""Text component that displays a string."""
text: str
"""The text to display."""
type: _t.Literal['Text'] = 'Text'
"""The type of the component. Always 'Text'."""
class Paragraph(BaseModel, extra='forbid'):
"""Paragraph component that displays a string as a paragraph."""
text: str
"""The text to display."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the paragraph's HTML component."""
type: _t.Literal['Paragraph'] = 'Paragraph'
"""The type of the component. Always 'Paragraph'."""
class PageTitle(BaseModel, extra='forbid'):
"""Sets the title of the HTML page via the `document.title` property."""
text: str
"""The text to set as the page title."""
type: _t.Literal['PageTitle'] = 'PageTitle'
"""The type of the component. Always 'PageTitle'."""
class Div(BaseModel, extra='forbid'):
"""A generic container component."""
components: '_t.List[AnyComponent]'
"""List of components to render inside the div."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the div's HTML component."""
type: _t.Literal['Div'] = 'Div'
"""The type of the component. Always 'Div'."""
class Page(BaseModel, extra='forbid'):
"""Similar to `container` in many UI frameworks, this acts as a root component for most pages."""
components: '_t.List[AnyComponent]'
"""List of components to render on the page."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the page's HTML component."""
type: _t.Literal['Page'] = 'Page'
"""The type of the component. Always 'Page'."""
class Heading(BaseModel, extra='forbid'):
"""Heading component."""
text: str
"""The text to display in the heading."""
level: _t.Literal[1, 2, 3, 4, 5, 6] = 1
"""The level of the heading. 1 is the largest, 6 is the smallest."""
html_id: _t.Union[str, None] = None
"""Optional HTML ID to apply to the heading's HTML component."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the page's HTML component."""
type: _t.Literal['Heading'] = 'Heading'
"""The type of the component. Always 'Heading'."""
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: _core_schema.CoreSchema, handler: _p.GetJsonSchemaHandler
) -> _t.Any:
# until https://github.com/pydantic/pydantic/issues/8413 is fixed
json_schema = handler(core_schema)
json_schema['required'].append('level')
return json_schema
CodeStyle = _te.Annotated[_t.Union[str, None], _p.Field(serialization_alias='codeStyle')]
"""
Code style to apply to a `Code` component.
Attributes:
codeStyle: The code style to apply. If None, no style is applied.
See Also:
- [PrismJS Themes](https://github.com/PrismJS/prism-themes)
- [PrismJS Theme Index](https://cdn.jsdelivr.net/npm/[email protected]/dist/esm/styles/prism/index.js)
"""
class Markdown(BaseModel, extra='forbid'):
"""Markdown component that renders markdown text."""
text: str
"""The markdown text to render."""
code_style: CodeStyle = None
"""Optional code style to apply to the markdown text."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the page's HTML component."""
type: _t.Literal['Markdown'] = 'Markdown'
"""The type of the component. Always 'Markdown'."""
class Code(BaseModel, extra='forbid'):
"""Code component that renders code with syntax highlighting."""
text: str
"""The code to render."""
language: _t.Union[str, None] = None
"""Optional language of the code. If None, no syntax highlighting is applied."""
code_style: CodeStyle = None
"""Optional code style to apply to the code."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the page's HTML component."""
type: _t.Literal['Code'] = 'Code'
"""The type of the component. Always 'Code'."""
class Json(BaseModel, extra='forbid'):
"""JSON component that renders JSON data."""
value: _types.JsonData
"""The JSON data to render."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the page's HTML component."""
type: _t.Literal['JSON'] = 'JSON'
"""The type of the component. Always 'JSON'."""
class Button(BaseModel, extra='forbid'):
"""Button component."""
text: str
"""The text to display on the button."""
on_click: _t.Union[events.AnyEvent, None] = None
"""Optional event to trigger when the button is clicked."""
html_type: _t.Union[_t.Literal['button', 'reset', 'submit'], None] = None
"""Optional HTML type of the button. If None, defaults to 'button'."""
named_style: _class_name.NamedStyleField = None
"""Optional named style to apply to the button."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the button's HTML component."""
type: _t.Literal['Button'] = 'Button'
"""The type of the component. Always 'Button'."""
class Link(BaseModel, extra='forbid'):
"""Link component."""
components: '_t.List[AnyComponent]'
"""List of components to render attached to the link."""
on_click: _t.Union[events.AnyEvent, None] = None
"""Optional event to trigger when the link is clicked."""
mode: _t.Union[_t.Literal['navbar', 'footer', 'tabs', 'vertical', 'pagination'], None] = None
"""Optional mode of the link."""
active: _t.Union[str, bool, None] = None
"""Optional active state of the link."""
locked: _t.Union[bool, None] = None
"""Optional locked state of the link."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the link's HTML component."""
type: _t.Literal['Link'] = 'Link'
"""The type of the component. Always 'Link'."""
class LinkList(BaseModel, extra='forbid'):
"""List of Link components."""
links: _t.List[Link]
"""List of links to render."""
mode: _t.Union[_t.Literal['tabs', 'vertical', 'pagination'], None] = None
"""Optional mode of the link list."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the link list's HTML component."""
type: _t.Literal['LinkList'] = 'LinkList'
"""The type of the component. Always 'LinkList'."""
class Navbar(BaseModel, extra='forbid'):
"""Navbar component used for moving between pages."""
title: _t.Union[str, None] = None
"""Optional title to display in the navbar."""
title_event: _t.Union[events.AnyEvent, None] = None
"""Optional event to trigger when the title is clicked. Often used to navigate to the home page."""
start_links: _t.List[Link] = []
"""List of links to render at the start of the navbar."""
end_links: _t.List[Link] = []
"""List of links to render at the end of the navbar."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the navbar's HTML component."""
type: _t.Literal['Navbar'] = 'Navbar'
"""The type of the component. Always 'Navbar'."""
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: _core_schema.CoreSchema, handler: _p.GetJsonSchemaHandler
) -> _t.Any:
# until https://github.com/pydantic/pydantic/issues/8413 is fixed
json_schema = handler(core_schema)
json_schema.setdefault('required', []).extend(['startLinks', 'endLinks'])
return json_schema
class Footer(BaseModel, extra='forbid'):
"""Footer component."""
links: _t.List[Link]
"""List of links to render in the footer."""
extra_text: _t.Union[str, None] = None
"""Optional extra text to display in the footer."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the footer's HTML component."""
type: _t.Literal['Footer'] = 'Footer'
"""The type of the component. Always 'Footer'."""
class Modal(BaseModel, extra='forbid'):
"""Modal component that displays a modal dialog."""
title: str
"""The text displayed on the modal trigger button."""
body: '_t.List[AnyComponent]'
"""List of components to render in the modal body."""
footer: '_t.Union[_t.List[AnyComponent], None]' = None
"""Optional list of components to render in the modal footer."""
open_trigger: _t.Union[events.PageEvent, None] = None
"""Optional event to trigger when the modal is opened."""
open_context: _t.Union[events.ContextType, None] = None
"""Optional context to pass to the open trigger event."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the modal's HTML component."""
type: _t.Literal['Modal'] = 'Modal'
"""The type of the component. Always 'Modal'."""
class ServerLoad(BaseModel, extra='forbid'):
"""A component that will be replaced by the server with the component returned by the given URL."""
path: str
"""The URL to load the component from."""
load_trigger: _t.Union[events.PageEvent, None] = None
"""Optional event to trigger when the component is loaded."""
components: '_t.Union[_t.List[AnyComponent], None]' = None
"""Optional list of components to render while the server is loading the new component(s)."""
sse: _t.Union[bool, None] = None
"""Optional flag to enable server-sent events (SSE) for the server load."""
sse_retry: _t.Union[int, None] = None
"""Optional time in milliseconds to retry the SSE connection."""
method: _t.Union[_t.Literal['GET', 'POST', 'PATCH', 'PUT', 'DELETE'], None] = None
"""Optional HTTP method to use when loading the component."""
type: _t.Literal['ServerLoad'] = 'ServerLoad'
"""The type of the component. Always 'ServerLoad'."""
class Image(BaseModel, extra='forbid'):
"""Image container component."""
src: str
"""The URL of the image to display."""
alt: _t.Union[str, None] = None
"""Optional alt text for the image."""
width: _t.Union[str, int, None] = None
"""Optional width used to display the image."""
height: _t.Union[str, int, None] = None
"""Optional height used to display the image."""
referrer_policy: _t.Union[
_t.Literal[
'no-referrer',
'no-referrer-when-downgrade',
'origin',
'origin-when-cross-origin',
'same-origin',
'strict-origin',
'strict-origin-when-cross-origin',
'unsafe-url',
],
None,
] = None
"""Optional referrer policy for the image. Specifies what information to send when fetching the image.
For more info, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy."""
loading: _t.Union[_t.Literal['eager', 'lazy'], None] = None
"""Optional loading strategy for the image."""
on_click: _t.Union[events.AnyEvent, None] = None
"""Optional event to trigger when the image is clicked."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the image's HTML component."""
type: _t.Literal['Image'] = 'Image'
"""The type of the component. Always 'Image'."""
class Iframe(BaseModel, extra='forbid'):
"""Iframe component that displays content from a URL."""
src: _p.HttpUrl
"""The URL of the content to display."""
title: _t.Union[str, None] = None
"""Optional title for the iframe."""
width: _t.Union[str, int, None] = None
"""Optional width used to display the iframe."""
height: _t.Union[str, int, None] = None
"""Optional height used to display the iframe."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the iframe's HTML component."""
srcdoc: _t.Union[str, None] = None
"""Optional HTML content to display in the iframe."""
sandbox: _t.Union[str, None] = None
"""Optional sandbox policy for the iframe. Specifies restrictions on the HTML content in the iframe."""
type: _t.Literal['Iframe'] = 'Iframe'
"""The type of the component. Always 'Iframe'."""
class Video(BaseModel, extra='forbid'):
"""Video component that displays a video or multiple videos."""
sources: _t.List[_p.AnyUrl]
"""List of URLs to the video sources."""
autoplay: _t.Union[bool, None] = None
"""Optional flag to enable autoplay for the video."""
controls: _t.Union[bool, None] = None
"""Optional flag to enable controls (pause, play, etc) for the video."""
loop: _t.Union[bool, None] = None
"""Optional flag to enable looping for the video."""
muted: _t.Union[bool, None] = None
"""Optional flag to mute the video."""
poster: _t.Union[_p.AnyUrl, None] = None
"""Optional URL to an image to display as the video poster (what is shown when the video is loading or until the user plays it)."""
width: _t.Union[str, int, None] = None
"""Optional width used to display the video."""
height: _t.Union[str, int, None] = None
"""Optional height used to display the video."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the video's HTML component."""
type: _t.Literal['Video'] = 'Video'
"""The type of the component. Always 'Video'."""
class FireEvent(BaseModel, extra='forbid'):
"""Fire an event."""
event: events.AnyEvent
"""The event to fire."""
message: _t.Union[str, None] = None
"""Optional message to display when the event is fired. Defaults to a blank message."""
type: _t.Literal['FireEvent'] = 'FireEvent'
"""The type of the component. Always 'FireEvent'."""
class Error(BaseModel, extra='forbid'):
"""Utility component used to display an error."""
title: str
"""The title of the error."""
description: str
"""The description of the error."""
status_code: _t.Union[int, None] = None
"""Optional status code of the error."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the error's HTML component."""
type: _t.Literal['Error'] = 'Error'
"""The type of the component. Always 'Error'."""
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: _core_schema.CoreSchema, handler: _p.GetJsonSchemaHandler
) -> _t.Any:
# add `children` to the schema so it can be used in the client
json_schema = handler(core_schema)
json_schema['properties']['children'] = {'tsType': 'ReactNode'}
return json_schema
class Spinner(BaseModel, extra='forbid'):
"""Spinner component that displays a loading spinner."""
text: _t.Union[str, None] = None
"""Optional text to display with the spinner."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the spinner's HTML component."""
type: _t.Literal['Spinner'] = 'Spinner'
"""The type of the component. Always 'Spinner'."""
class Toast(BaseModel, extra='forbid'):
"""Toast component that displays a toast message (small temporary message)."""
title: str
"""The title of the toast."""
body: '_t.List[AnyComponent]'
"""List of components to render in the toast body."""
# TODO: change these before the release (top left, center, end, etc). Can be done with the toast bug fix.
position: _t.Union[
_t.Literal[
'top-start',
'top-center',
'top-end',
'middle-start',
'middle-center',
'middle-end',
'bottom-start',
'bottom-center',
'bottom-end',
],
None,
] = None
"""Optional position of the toast."""
open_trigger: _t.Union[events.PageEvent, None] = None
"""Optional event to trigger when the toast is opened."""
open_context: _t.Union[events.ContextType, None] = None
"""Optional context to pass to the open trigger event."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the toast's HTML component."""
type: _t.Literal['Toast'] = 'Toast'
"""The type of the component. Always 'Toast'."""
class Custom(BaseModel, extra='forbid'):
"""Custom component that allows for special data to be rendered."""
data: _types.JsonData
"""The data to render in the custom component."""
sub_type: str
"""The sub-type of the custom component."""
library: _t.Union[str, None] = None
"""Optional library to use for the custom component."""
class_name: _class_name.ClassNameField = None
"""Optional class name to apply to the custom component's HTML component."""
type: _t.Literal['Custom'] = 'Custom'
"""The type of the component. Always 'Custom'."""
class DarkMode(BaseModel, extra='forbid'):
"""DarkMode component"""
class_name: _class_name.ClassNameField = None
type: _t.Literal['DarkMode'] = 'DarkMode'
AnyComponent = _te.Annotated[
_t.Union[
Text,
Paragraph,
PageTitle,
Div,
Page,
Heading,
Markdown,
Code,
Json,
Button,
Link,
LinkList,
Navbar,
Footer,
Modal,
ServerLoad,
Image,
Iframe,
Video,
FireEvent,
Error,
Spinner,
Custom,
DarkMode,
Table,
Pagination,
Display,
Details,
Form,
FormField,
ModelForm,
Toast,
],
_p.Field(discriminator='type'),
]
"""Union of all components.
Pydantic discriminator field is set to 'type' to allow for efficient serialization and deserialization of the components."""