neutralino-python 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- neutralino/__init__.py +88 -0
- neutralino/_catalog.py +297 -0
- neutralino/_data/LICENSE.neutralino-js-client +21 -0
- neutralino/_data/LICENSE.neutralinojs +1066 -0
- neutralino/_data/docs/cli.md +315 -0
- neutralino/_data/docs/custom-components.md +107 -0
- neutralino/_data/docs/custom-frontends.md +80 -0
- neutralino/_data/docs/desktop-helpers.md +296 -0
- neutralino/_data/docs/guide.md +131 -0
- neutralino/_data/docs/llms-full.txt +3921 -0
- neutralino/_data/docs/llms.txt +43 -0
- neutralino/_data/docs/manifests/components.json +20128 -0
- neutralino/_data/docs/manifests/examples.json +52 -0
- neutralino/_data/docs/manifests/native.json +3024 -0
- neutralino/_data/docs/manifests/schema.json +43 -0
- neutralino/_data/docs/manual-checklist.md +189 -0
- neutralino/_data/docs/packaging.md +252 -0
- neutralino/_data/docs/performance.md +15 -0
- neutralino/_data/docs/platforms.md +15 -0
- neutralino/_data/docs/reference/components.md +1685 -0
- neutralino/_data/docs/reference/native.md +148 -0
- neutralino/_data/docs/security.md +60 -0
- neutralino/_data/docs/ui.md +249 -0
- neutralino/_data/docs/updates.md +366 -0
- neutralino/_data/examples/README.md +6 -0
- neutralino/_data/examples/crud/README.md +36 -0
- neutralino/_data/examples/crud/app.py +243 -0
- neutralino/_data/examples/dashboard/README.md +36 -0
- neutralino/_data/examples/dashboard/app.py +367 -0
- neutralino/_data/examples/editor/README.md +35 -0
- neutralino/_data/examples/editor/app.py +190 -0
- neutralino/_data/examples/file-manager/README.md +40 -0
- neutralino/_data/examples/file-manager/app.py +120 -0
- neutralino/_data/examples/file-manager/frontend/index.html +20 -0
- neutralino/_data/examples/file-manager/frontend/main.js +55 -0
- neutralino/_data/examples/file-manager/frontend/neutralino.js +1 -0
- neutralino/_data/examples/file-manager/frontend/python-bridge.d.ts +207 -0
- neutralino/_data/examples/file-manager/frontend/python-bridge.mjs +854 -0
- neutralino/_data/examples/file-manager/frontend/style.css +4 -0
- neutralino/_data/examples/hybrid/README.md +48 -0
- neutralino/_data/examples/hybrid/app.py +80 -0
- neutralino/_data/examples/hybrid/components/counter.mjs +37 -0
- neutralino/_data/examples/hybrid/components/widget.mjs +23 -0
- neutralino/_data/examples/hybrid/styles.css +21 -0
- neutralino/_data/examples/hybrid/widget/package.json +20 -0
- neutralino/_data/examples/hybrid/widget/src/Widget.jsx +25 -0
- neutralino/_data/examples/hybrid/widget/vite.config.mjs +19 -0
- neutralino/_data/examples/monitor/README.md +36 -0
- neutralino/_data/examples/monitor/app.py +375 -0
- neutralino/_data/examples/react/README.md +90 -0
- neutralino/_data/examples/react/app.py +68 -0
- neutralino/_data/examples/react/frontend/.gitignore +5 -0
- neutralino/_data/examples/react/frontend/index.html +17 -0
- neutralino/_data/examples/react/frontend/package-lock.json +1232 -0
- neutralino/_data/examples/react/frontend/package.json +25 -0
- neutralino/_data/examples/react/frontend/scripts/prepare-bridge.mjs +49 -0
- neutralino/_data/examples/react/frontend/src/App.tsx +157 -0
- neutralino/_data/examples/react/frontend/src/backend.ts +21 -0
- neutralino/_data/examples/react/frontend/src/calls.test.ts +206 -0
- neutralino/_data/examples/react/frontend/src/calls.ts +306 -0
- neutralino/_data/examples/react/frontend/src/globals.d.ts +14 -0
- neutralino/_data/examples/react/frontend/src/hooks.ts +45 -0
- neutralino/_data/examples/react/frontend/src/main.tsx +13 -0
- neutralino/_data/examples/react/frontend/src/styles.css +11 -0
- neutralino/_data/examples/react/frontend/tsconfig.json +18 -0
- neutralino/_data/examples/react/frontend/vite.config.ts +9 -0
- neutralino/_data/examples/settings/README.md +38 -0
- neutralino/_data/examples/settings/app.py +440 -0
- neutralino/_data/examples/updater/.gitignore +2 -0
- neutralino/_data/examples/updater/README.md +82 -0
- neutralino/_data/examples/updater/app.py +160 -0
- neutralino/_data/examples/updater/app.toml +21 -0
- neutralino/_data/examples/updater/demo.py +107 -0
- neutralino/_data/examples/updater/requirements.txt +2 -0
- neutralino/_data/examples/updater/sources/github-private.toml +21 -0
- neutralino/_data/examples/updater/sources/github-public.toml +21 -0
- neutralino/_data/examples/vanilla/README.md +35 -0
- neutralino/_data/examples/vanilla/app.py +46 -0
- neutralino/_data/examples/vanilla/frontend/index.html +17 -0
- neutralino/_data/examples/vanilla/frontend/main.js +29 -0
- neutralino/_data/examples/vanilla/frontend/neutralino.js +1 -0
- neutralino/_data/examples/vanilla/frontend/python-bridge.d.ts +207 -0
- neutralino/_data/examples/vanilla/frontend/python-bridge.mjs +854 -0
- neutralino/_data/examples/vanilla/frontend/style.css +4 -0
- neutralino/_data/explorer/app.py +1328 -0
- neutralino/_data/explorer/catalogue.json +48179 -0
- neutralino/_data/explorer/explorer_previews.py +247 -0
- neutralino/_data/explorer/sources/native/always_on_top.py +55 -0
- neutralino/_data/explorer/sources/native/binary.py +125 -0
- neutralino/_data/explorer/sources/native/borderless.py +99 -0
- neutralino/_data/explorer/sources/native/broadcast.py +90 -0
- neutralino/_data/explorer/sources/native/config.py +78 -0
- neutralino/_data/explorer/sources/native/controls.py +140 -0
- neutralino/_data/explorer/sources/native/directory.py +163 -0
- neutralino/_data/explorer/sources/native/displays.py +75 -0
- neutralino/_data/explorer/sources/native/env.py +98 -0
- neutralino/_data/explorer/sources/native/events.py +124 -0
- neutralino/_data/explorer/sources/native/exec_command.py +79 -0
- neutralino/_data/explorer/sources/native/folder.py +70 -0
- neutralino/_data/explorer/sources/native/icon_badge.py +101 -0
- neutralino/_data/explorer/sources/native/image.py +119 -0
- neutralino/_data/explorer/sources/native/lifecycle.py +99 -0
- neutralino/_data/explorer/sources/native/locale.py +70 -0
- neutralino/_data/explorer/sources/native/main_menu.py +103 -0
- neutralino/_data/explorer/sources/native/message_box.py +101 -0
- neutralino/_data/explorer/sources/native/mouse.py +70 -0
- neutralino/_data/explorer/sources/native/multi_window.py +134 -0
- neutralino/_data/explorer/sources/native/notification.py +74 -0
- neutralino/_data/explorer/sources/native/open_external.py +81 -0
- neutralino/_data/explorer/sources/native/open_file.py +102 -0
- neutralino/_data/explorer/sources/native/paths.py +77 -0
- neutralino/_data/explorer/sources/native/print_snapshot.py +84 -0
- neutralino/_data/explorer/sources/native/read_write.py +192 -0
- neutralino/_data/explorer/sources/native/resources.py +110 -0
- neutralino/_data/explorer/sources/native/save_file.py +101 -0
- neutralino/_data/explorer/sources/native/server_mount.py +106 -0
- neutralino/_data/explorer/sources/native/sound.py +84 -0
- neutralino/_data/explorer/sources/native/spawn_process.py +128 -0
- neutralino/_data/explorer/sources/native/states.py +122 -0
- neutralino/_data/explorer/sources/native/stats_permissions.py +147 -0
- neutralino/_data/explorer/sources/native/storage.py +108 -0
- neutralino/_data/explorer/sources/native/system_info.py +115 -0
- neutralino/_data/explorer/sources/native/text.py +133 -0
- neutralino/_data/explorer/sources/native/trash.py +98 -0
- neutralino/_data/explorer/sources/native/tray.py +109 -0
- neutralino/_data/explorer/sources/native/watcher.py +167 -0
- neutralino/_data/explorer/sources/patterns/content_blocks.py +119 -0
- neutralino/_data/explorer/sources/patterns/dynamic_dashboard.py +121 -0
- neutralino/_data/explorer/sources/patterns/dynamic_form.py +120 -0
- neutralino/_data/explorer/sources/patterns/dynamic_table.py +109 -0
- neutralino/_data/explorer/sources/patterns/error_reporting.mjs +24 -0
- neutralino/_data/explorer/sources/patterns/error_reporting.py +124 -0
- neutralino/_data/explorer/sources/patterns/errors_timeouts.py +108 -0
- neutralino/_data/explorer/sources/patterns/feature_flags.py +99 -0
- neutralino/_data/explorer/sources/patterns/file_drop.py +126 -0
- neutralino/_data/explorer/sources/patterns/http_from_python.py +152 -0
- neutralino/_data/explorer/sources/patterns/keyboard_shortcuts.py +153 -0
- neutralino/_data/explorer/sources/patterns/live_config_reload.py +160 -0
- neutralino/_data/explorer/sources/patterns/logging.py +184 -0
- neutralino/_data/explorer/sources/patterns/long_tasks.py +121 -0
- neutralino/_data/explorer/sources/patterns/multi_window_sync.py +68 -0
- neutralino/_data/explorer/sources/patterns/navigation_from_backend.py +109 -0
- neutralino/_data/explorer/sources/patterns/onboarding.py +123 -0
- neutralino/_data/explorer/sources/patterns/optimistic_updates.py +89 -0
- neutralino/_data/explorer/sources/patterns/persistence.py +114 -0
- neutralino/_data/explorer/sources/patterns/python_calls_page.mjs +10 -0
- neutralino/_data/explorer/sources/patterns/python_calls_page.py +64 -0
- neutralino/_data/explorer/sources/patterns/python_push.py +84 -0
- neutralino/_data/explorer/sources/patterns/recurring_events.py +114 -0
- neutralino/_data/explorer/sources/patterns/roles_permissions.py +104 -0
- neutralino/_data/explorer/sources/patterns/scheduled_jobs.py +194 -0
- neutralino/_data/explorer/sources/patterns/settings_page.py +165 -0
- neutralino/_data/explorer/sources/patterns/sqlite.py +181 -0
- neutralino/_data/explorer/sources/patterns/streaming.py +102 -0
- neutralino/_data/explorer/sources/patterns/texts_i18n.py +81 -0
- neutralino/_data/explorer/sources/patterns/theme_from_backend.py +86 -0
- neutralino/_data/explorer/sources/patterns/updater.py +232 -0
- neutralino/_data/explorer/sources/workspace/calendar/app.py +547 -0
- neutralino/_data/explorer/sources/workspace/customers/app.py +467 -0
- neutralino/_data/explorer/sources/workspace/dashboard/README.md +36 -0
- neutralino/_data/explorer/sources/workspace/dashboard/app.py +367 -0
- neutralino/_data/explorer/sources/workspace/documents/app.py +511 -0
- neutralino/_data/explorer/sources/workspace/inbox/app.py +439 -0
- neutralino/_data/explorer/sources/workspace/orders/app.py +491 -0
- neutralino/_data/explorer/sources/workspace/reports/app.py +386 -0
- neutralino/_data/explorer/sources/workspace/settings/README.md +38 -0
- neutralino/_data/explorer/sources/workspace/settings/app.py +440 -0
- neutralino/_data/explorer/sources/workspace/system/README.md +36 -0
- neutralino/_data/explorer/sources/workspace/system/app.py +375 -0
- neutralino/_data/explorer/sources/workspace/tasks/app.py +462 -0
- neutralino/_data/renderer/LICENSES/@babel__runtime@7.29.17/LICENSE +22 -0
- neutralino/_data/renderer/LICENSES/@codemirror__autocomplete@6.20.4/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__commands@6.11.2/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__lang-css@6.3.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__lang-html@6.4.12/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__lang-javascript@6.2.5/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__lang-json@6.0.2/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__lang-markdown@6.5.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__lang-python@6.2.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__lang-sql@6.10.0/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__lang-yaml@6.1.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__language@6.13.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__search@6.7.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__state@6.7.6/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@codemirror__view@6.44.2/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@dnd-kit__accessibility@3.1.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@dnd-kit__core@6.3.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@dnd-kit__sortable@10.0.0/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@dnd-kit__utilities@3.2.2/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@emoji-mart__data@1.2.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@floating-ui__core@1.8.0/LICENSE +20 -0
- neutralino/_data/renderer/LICENSES/@floating-ui__dom@1.8.0/LICENSE +20 -0
- neutralino/_data/renderer/LICENSES/@floating-ui__react-dom@2.1.9/LICENSE +20 -0
- neutralino/_data/renderer/LICENSES/@floating-ui__react@0.27.20/LICENSE +20 -0
- neutralino/_data/renderer/LICENSES/@floating-ui__utils@0.2.12/LICENSE +20 -0
- neutralino/_data/renderer/LICENSES/@lezer__common@1.5.7/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@lezer__css@1.3.9/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@lezer__highlight@1.2.5/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@lezer__html@1.3.13/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@lezer__javascript@1.5.7/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@lezer__json@1.0.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@lezer__lr@1.4.13/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@lezer__markdown@1.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@lezer__python@1.1.21/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@lezer__yaml@1.0.4/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__carousel@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__charts@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__code-highlight@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__core@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__dates@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__dropzone@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__emoji@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__hooks@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__lightbox@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__notifications@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__nprogress@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__schedule@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__spotlight@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__store@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@mantine__tiptap@9.8.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@marijn__find-cluster-break@1.0.4/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@reduxjs__toolkit@2.16.0/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@tabler__icons-react@3.55.0/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@tanstack__react-virtual@3.14.32/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@tanstack__virtual-core@3.19.17/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__core@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-blockquote@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-bold@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-code-block@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-code@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-document@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-hard-break@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-heading@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-horizontal-rule@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-italic@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-link@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-list@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-paragraph@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-strike@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-text@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extension-underline@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__extensions@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__react@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/@tiptap__starter-kit@3.35.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/attr-accept@4.0.1/LICENSE +22 -0
- neutralino/_data/renderer/LICENSES/clsx@2.1.1/license +9 -0
- neutralino/_data/renderer/LICENSES/crelt@1.0.7/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/d3-array@3.2.5/LICENSE +13 -0
- neutralino/_data/renderer/LICENSES/d3-color@3.1.1/LICENSE +13 -0
- neutralino/_data/renderer/LICENSES/d3-format@3.1.3/LICENSE +13 -0
- neutralino/_data/renderer/LICENSES/d3-interpolate@3.0.2/LICENSE +13 -0
- neutralino/_data/renderer/LICENSES/d3-path@3.1.1/LICENSE +13 -0
- neutralino/_data/renderer/LICENSES/d3-scale@4.0.3/LICENSE +13 -0
- neutralino/_data/renderer/LICENSES/d3-shape@3.2.1/LICENSE +13 -0
- neutralino/_data/renderer/LICENSES/d3-time-format@4.1.1/LICENSE +13 -0
- neutralino/_data/renderer/LICENSES/d3-time@3.1.1/LICENSE +13 -0
- neutralino/_data/renderer/LICENSES/dayjs@1.11.23/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/decimal.js-light@2.5.1/LICENCE.md +23 -0
- neutralino/_data/renderer/LICENSES/diff@9.0.0/LICENSE +29 -0
- neutralino/_data/renderer/LICENSES/dompurify@3.5.1/LICENSE +202 -0
- neutralino/_data/renderer/LICENSES/dompurify@3.5.1/LICENSE-MPL +373 -0
- neutralino/_data/renderer/LICENSES/embla-carousel-react@8.6.0/MIT-embla-carousel.txt +21 -0
- neutralino/_data/renderer/LICENSES/embla-carousel-reactive-utils@8.6.0/MIT-embla-carousel.txt +21 -0
- neutralino/_data/renderer/LICENSES/embla-carousel@8.6.0/MIT-embla-carousel.txt +21 -0
- neutralino/_data/renderer/LICENSES/es-toolkit@1.53.2/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/es-toolkit@1.53.2/NOTICE +39 -0
- neutralino/_data/renderer/LICENSES/eventemitter3@5.0.4/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/fast-equals@5.4.7/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/file-selector@5.0.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/get-nonce@1.0.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/immer@11.2.14/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/linkifyjs@4.4.0/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/marked@18.4.0/LICENSE +44 -0
- neutralino/_data/renderer/LICENSES/neutralino.js@6.11.0/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/orderedmap@2.1.1/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/packages.json +1838 -0
- neutralino/_data/renderer/LICENSES/prosemirror-commands@1.7.2/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/prosemirror-dropcursor@1.8.4/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/prosemirror-gapcursor@1.4.1/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/prosemirror-history@1.5.2/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/prosemirror-keymap@1.2.3/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/prosemirror-model@1.25.13/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/prosemirror-schema-list@1.6.1/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/prosemirror-state@1.4.4/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/prosemirror-transform@1.12.7/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/prosemirror-view@1.44.0/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/react-dom@19.3.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/react-dropzone@20.1.4/LICENSE +22 -0
- neutralino/_data/renderer/LICENSES/react-is@19.3.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/react-number-format@5.4.5/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/react-redux@9.3.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/react-remove-scroll-bar@2.3.8/MIT-react-remove-scroll-bar.txt +21 -0
- neutralino/_data/renderer/LICENSES/react-remove-scroll@2.7.2/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/react-style-singleton@2.2.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/react-transition-group@4.4.5/LICENSE +30 -0
- neutralino/_data/renderer/LICENSES/react@19.3.1/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/recharts@3.11.3/LICENSE +22 -0
- neutralino/_data/renderer/LICENSES/redux-thunk@3.1.0/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/redux@5.0.1/LICENSE.md +21 -0
- neutralino/_data/renderer/LICENSES/reselect@5.2.0/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/rolldown@1.3.4/LICENSE +25 -0
- neutralino/_data/renderer/LICENSES/rope-sequence@1.3.4/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/rrule@2.8.1/LICENCE +65 -0
- neutralino/_data/renderer/LICENSES/scheduler@0.28.0/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/style-mod@4.1.5/LICENSE +19 -0
- neutralino/_data/renderer/LICENSES/tiny-invariant@1.3.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/tslib@2.9.1/LICENSE.txt +12 -0
- neutralino/_data/renderer/LICENSES/use-callback-ref@1.3.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/use-sidecar@1.1.3/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/use-sync-external-store@1.7.0/LICENSE +21 -0
- neutralino/_data/renderer/LICENSES/victory-vendor@38.1.1/LICENSE.txt +150 -0
- neutralino/_data/renderer/LICENSES/vite@8.4.1/LICENSE.md +2320 -0
- neutralino/_data/renderer/LICENSES/w3c-keyname@2.2.8/LICENSE +19 -0
- neutralino/_data/renderer/assets/IconGripVertical-BlJrWVuo.js +1 -0
- neutralino/_data/renderer/assets/carousel-D1j1SkWR.css +1 -0
- neutralino/_data/renderer/assets/carousel-DniHwTLK.js +1 -0
- neutralino/_data/renderer/assets/chunk-CCkLOSkX.css +1 -0
- neutralino/_data/renderer/assets/chunk-Du0h7lB-.js +9 -0
- neutralino/_data/renderer/assets/codehighlight-CCe_blv1.js +10 -0
- neutralino/_data/renderer/assets/codehighlight-THJMNDh8.css +1 -0
- neutralino/_data/renderer/assets/codemirror-CinrNxVJ.js +2 -0
- neutralino/_data/renderer/assets/css-CMUk25uq.js +1 -0
- neutralino/_data/renderer/assets/dist-70Le0T2N.js +1 -0
- neutralino/_data/renderer/assets/dist-BCHbQlae.js +11 -0
- neutralino/_data/renderer/assets/dist-BXts23GT.js +1 -0
- neutralino/_data/renderer/assets/dist-D2RX2UgP.js +23 -0
- neutralino/_data/renderer/assets/dist-DikJK2lp.js +1 -0
- neutralino/_data/renderer/assets/dist-acbbRM1z.js +1 -0
- neutralino/_data/renderer/assets/dnd-DaaaVala.js +5 -0
- neutralino/_data/renderer/assets/dropzone-CeqF5SZx.js +1 -0
- neutralino/_data/renderer/assets/dropzone-aPCBPjDN.css +1 -0
- neutralino/_data/renderer/assets/embla-carousel-react.esm-CbyYjwMN.js +1 -0
- neutralino/_data/renderer/assets/emoji-BfRPfoVw.js +1 -0
- neutralino/_data/renderer/assets/emoji-DV8OzU1E.css +1 -0
- neutralino/_data/renderer/assets/esm-C5ZYjSBQ.js +82 -0
- neutralino/_data/renderer/assets/highlight-CB6hGb5A.js +3 -0
- neutralino/_data/renderer/assets/highlight-Dlb5DJwL.css +1 -0
- neutralino/_data/renderer/assets/html-Dad3kwIa.js +1 -0
- neutralino/_data/renderer/assets/icon-chunk-X3Kr0nh-.js +1 -0
- neutralino/_data/renderer/assets/index-CU0HB-J7.js +69 -0
- neutralino/_data/renderer/assets/index-TWpF4Lef.css +1 -0
- neutralino/_data/renderer/assets/javascript-BQb8vpw3.js +1 -0
- neutralino/_data/renderer/assets/json-BCFmxlML.js +1 -0
- neutralino/_data/renderer/assets/lightbox-BHtgbsUV.js +1 -0
- neutralino/_data/renderer/assets/lightbox-BzKDpkAD.css +1 -0
- neutralino/_data/renderer/assets/markdown-DSuAylvy.js +5 -0
- neutralino/_data/renderer/assets/markdown-DjaNWMgH.js +77 -0
- neutralino/_data/renderer/assets/python-pdbIAveZ.js +9 -0
- neutralino/_data/renderer/assets/richtext-DIAsHMuK.css +1 -0
- neutralino/_data/renderer/assets/richtext-Z6fG9G2_.js +137 -0
- neutralino/_data/renderer/assets/spotlight-Ccstaz1-.css +1 -0
- neutralino/_data/renderer/assets/spotlight-knlyJw_A.js +1 -0
- neutralino/_data/renderer/assets/sql-DLoQFEde.js +2 -0
- neutralino/_data/renderer/assets/with-selector-BOM9_OlN.js +1 -0
- neutralino/_data/renderer/assets/yaml-BJNzokSe.js +1 -0
- neutralino/_data/renderer/index.html +22 -0
- neutralino/_data/renderer/manifest.json +202 -0
- neutralino/_data/renderer/neutralino.js +1 -0
- neutralino/_data/renderer/shared/mantine-core.mjs +5 -0
- neutralino/_data/renderer/shared/mantine-hooks.mjs +5 -0
- neutralino/_data/renderer/shared/react-dom-client.mjs +5 -0
- neutralino/_data/renderer/shared/react-dom.mjs +6 -0
- neutralino/_data/renderer/shared/react-jsx-runtime.mjs +5 -0
- neutralino/_data/renderer/shared/react.mjs +6 -0
- neutralino/_data/skills/README.md +6 -0
- neutralino/_data/skills/neutralino-app-patterns/SKILL.md +101 -0
- neutralino/_data/skills/neutralino-app-patterns/references/dashboards-and-background-work.md +55 -0
- neutralino/_data/skills/neutralino-app-patterns/references/storage-and-settings.md +57 -0
- neutralino/_data/skills/neutralino-app-patterns/references/tables-and-crud.md +81 -0
- neutralino/_data/skills/neutralino-frontend/SKILL.md +121 -0
- neutralino/_data/skills/neutralino-frontend/references/bridge.md +63 -0
- neutralino/_data/skills/neutralino-frontend/references/custom-components.md +84 -0
- neutralino/_data/skills/neutralino-frontend/references/frameworks.md +61 -0
- neutralino/_data/skills/neutralino-maintenance/SKILL.md +85 -0
- neutralino/_data/skills/neutralino-maintenance/references/platforms-and-gates.md +35 -0
- neutralino/_data/skills/neutralino-maintenance/references/records.md +38 -0
- neutralino/_data/skills/neutralino-maintenance/references/review-process.md +36 -0
- neutralino/_data/skills/neutralino-native/SKILL.md +107 -0
- neutralino/_data/skills/neutralino-native/references/api-conventions.md +72 -0
- neutralino/_data/skills/neutralino-native/references/desktop-helpers.md +53 -0
- neutralino/_data/skills/neutralino-native/references/permissions-windows.md +53 -0
- neutralino/_data/skills/neutralino-packaging/SKILL.md +99 -0
- neutralino/_data/skills/neutralino-packaging/references/build-layout.md +51 -0
- neutralino/_data/skills/neutralino-packaging/references/managed-install.md +59 -0
- neutralino/_data/skills/neutralino-packaging/references/templates-and-config.md +63 -0
- neutralino/_data/skills/neutralino-python/SKILL.md +109 -0
- neutralino/_data/skills/neutralino-python/references/architecture.md +53 -0
- neutralino/_data/skills/neutralino-python/references/cli.md +60 -0
- neutralino/_data/skills/neutralino-python/references/platforms-security.md +44 -0
- neutralino/_data/skills/neutralino-rpc/SKILL.md +114 -0
- neutralino/_data/skills/neutralino-rpc/references/errors-and-limits.md +44 -0
- neutralino/_data/skills/neutralino-rpc/references/expose-and-context.md +74 -0
- neutralino/_data/skills/neutralino-rpc/references/streams-and-cancellation.md +53 -0
- neutralino/_data/skills/neutralino-ui/SKILL.md +115 -0
- neutralino/_data/skills/neutralino-ui/references/components-and-tree.md +74 -0
- neutralino/_data/skills/neutralino-ui/references/state-forms-events.md +85 -0
- neutralino/_data/skills/neutralino-ui/references/themes-profiles-assets.md +105 -0
- neutralino/_data/skills/neutralino-updates/SKILL.md +94 -0
- neutralino/_data/skills/neutralino-updates/references/config-and-api.md +80 -0
- neutralino/_data/skills/neutralino-updates/references/releases-and-install.md +77 -0
- neutralino/_data/skills/neutralino-updates/references/security-rollback.md +48 -0
- neutralino/_data/ui/contracts.json +32927 -0
- neutralino/_data/ui/emoji.json +1 -0
- neutralino/_data/upstream-lock.json +97 -0
- neutralino/_managed.py +1099 -0
- neutralino/_paths.py +220 -0
- neutralino/_platform.py +150 -0
- neutralino/_scaffold.py +344 -0
- neutralino/api/_base.py +99 -0
- neutralino/api/_custom.py +1350 -0
- neutralino/api/_dec.py +21 -0
- neutralino/api/_enc.py +80 -0
- neutralino/api/app.py +119 -0
- neutralino/api/clipboard.py +101 -0
- neutralino/api/computer.py +261 -0
- neutralino/api/custom.py +36 -0
- neutralino/api/debug.py +41 -0
- neutralino/api/extensions.py +57 -0
- neutralino/api/filesystem.py +633 -0
- neutralino/api/net.py +22 -0
- neutralino/api/os.py +508 -0
- neutralino/api/resources.py +124 -0
- neutralino/api/server.py +66 -0
- neutralino/api/storage.py +98 -0
- neutralino/api/types.py +342 -0
- neutralino/api/updater.py +22 -0
- neutralino/api/window.py +397 -0
- neutralino/application.py +1260 -0
- neutralino/bundle.py +187 -0
- neutralino/cli.py +629 -0
- neutralino/client.py +496 -0
- neutralino/config.py +671 -0
- neutralino/devtools.py +1973 -0
- neutralino/errors.py +142 -0
- neutralino/events.py +579 -0
- neutralino/execution.py +293 -0
- neutralino/extension.py +134 -0
- neutralino/frontend/python-bridge.d.ts +207 -0
- neutralino/frontend/python-bridge.mjs +854 -0
- neutralino/protocol.py +456 -0
- neutralino/py.typed +0 -0
- neutralino/rpc.py +1625 -0
- neutralino/runtime.py +591 -0
- neutralino/supervisor.py +866 -0
- neutralino/sync_client.py +376 -0
- neutralino/templates/default/app.py +32 -0
- neutralino/templates/default/frontend/app.js +28 -0
- neutralino/templates/default/frontend/index.html +26 -0
- neutralino/templates/default/frontend/neutralino.js +1 -0
- neutralino/templates/default/frontend/styles.css +8 -0
- neutralino/templates/default/gitignore.txt +3 -0
- neutralino/templates/default/neutralino.config.json +25 -0
- neutralino/templates/hybrid/README.md +17 -0
- neutralino/templates/hybrid/app.py +49 -0
- neutralino/templates/hybrid/components/counter.mjs +37 -0
- neutralino/templates/hybrid/styles.css +21 -0
- neutralino/templates/python-ui/README.md +15 -0
- neutralino/templates/python-ui/app.py +44 -0
- neutralino/templates/python-ui/gitignore.txt +3 -0
- neutralino/templates/python-ui/requirements.txt +4 -0
- neutralino/templates/react/README.md +28 -0
- neutralino/templates/react/app.py +52 -0
- neutralino/templates/react/components/widget.mjs +23 -0
- neutralino/templates/react/widget/package.json +20 -0
- neutralino/templates/react/widget/src/Widget.jsx +25 -0
- neutralino/templates/react/widget/vite.config.mjs +19 -0
- neutralino/transport.py +661 -0
- neutralino/types.py +93 -0
- neutralino/ui/__init__.py +139 -0
- neutralino/ui/_app.py +1529 -0
- neutralino/ui/_assets.py +407 -0
- neutralino/ui/_bases.py +1210 -0
- neutralino/ui/_behaviours.py +1066 -0
- neutralino/ui/_charts.py +732 -0
- neutralino/ui/_component.py +882 -0
- neutralino/ui/_dates.py +168 -0
- neutralino/ui/_desktop.py +879 -0
- neutralino/ui/_events.py +192 -0
- neutralino/ui/_filters.py +427 -0
- neutralino/ui/_forms.py +282 -0
- neutralino/ui/_protocol.py +201 -0
- neutralino/ui/_schedule.py +543 -0
- neutralino/ui/_security.py +106 -0
- neutralino/ui/_session.py +969 -0
- neutralino/ui/_state.py +199 -0
- neutralino/ui/_tables.py +256 -0
- neutralino/ui/_theme.py +154 -0
- neutralino/ui/blocks/__init__.py +19 -0
- neutralino/ui/blocks/_data.py +2239 -0
- neutralino/ui/blocks/_interactive.py +622 -0
- neutralino/ui/blocks/_layout.py +2967 -0
- neutralino/ui/components/__init__.py +81 -0
- neutralino/ui/components/_dates.py +879 -0
- neutralino/ui/components/_editors.py +217 -0
- neutralino/ui/components/_extensions.py +790 -0
- neutralino/ui/components/_extras.py +303 -0
- neutralino/ui/components/_generated.py +30565 -0
- neutralino/ui/components/_types.py +17 -0
- neutralino/ui/schema.py +824 -0
- neutralino/ui/validators.py +362 -0
- neutralino/updates/__init__.py +96 -0
- neutralino/updates/_http.py +320 -0
- neutralino/updates/_layout.py +762 -0
- neutralino/updates/archive.py +216 -0
- neutralino/updates/config.py +256 -0
- neutralino/updates/errors.py +97 -0
- neutralino/updates/install.sh.in +201 -0
- neutralino/updates/launcher.py +18 -0
- neutralino/updates/manager.py +808 -0
- neutralino/updates/manifest.py +196 -0
- neutralino/updates/release.py +314 -0
- neutralino/updates/semver.py +84 -0
- neutralino/updates/sources.py +392 -0
- neutralino/windows.py +316 -0
- neutralino_python-0.1.0.dist-info/METADATA +121 -0
- neutralino_python-0.1.0.dist-info/RECORD +521 -0
- neutralino_python-0.1.0.dist-info/WHEEL +4 -0
- neutralino_python-0.1.0.dist-info/entry_points.txt +3 -0
- neutralino_python-0.1.0.dist-info/licenses/LICENSE +21 -0
- neutralino_python-0.1.0.dist-info/licenses/THIRD_PARTY_NOTICES.md +16 -0
- neutralino_python-0.1.0.dist-info/licenses/src/neutralino/_data/renderer/LICENSES/packages.json +1838 -0
|
@@ -0,0 +1,2239 @@
|
|
|
1
|
+
"""Data and card blocks (P04.07): metrics, cards, people tables, tables, grids.
|
|
2
|
+
|
|
3
|
+
Every block is a plain composition of ``neutralino.ui`` components with typed
|
|
4
|
+
constructor arguments; none adds a renderer adapter, a page command or global
|
|
5
|
+
state. The data comes from the application: values are pushed with
|
|
6
|
+
``set_stats``/``set_items``/``set_rows`` (Python to page) and user intent comes
|
|
7
|
+
back as typed events (``StatsEvent``, ``CardEvent``, ``UserEvent``) or the
|
|
8
|
+
inner ``DataTable``'s own events. Large data is always answered by a
|
|
9
|
+
``DataTable`` provider in bounded pages (a local ``rows=`` list is served
|
|
10
|
+
through the same bounded provider path); nothing sends a whole dataset.
|
|
11
|
+
|
|
12
|
+
Variants (``StatsCard(variant="ring")`` ...) are selected through parameters,
|
|
13
|
+
not copies.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import datetime as _dt
|
|
19
|
+
import inspect
|
|
20
|
+
import math
|
|
21
|
+
import re
|
|
22
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
23
|
+
from dataclasses import dataclass, field
|
|
24
|
+
from typing import Any, ClassVar, Literal, cast
|
|
25
|
+
|
|
26
|
+
from ...errors import ValidationError
|
|
27
|
+
from ...types import UNSET, Unset
|
|
28
|
+
from .._bases import Provider, TablePage, TableQuery
|
|
29
|
+
from .._component import Component, Container, EventSpec
|
|
30
|
+
from .._events import Event, Handler, RowActionEvent, TableEvent
|
|
31
|
+
from .._filters import FilterPanel
|
|
32
|
+
from .._tables import Column, RowAction
|
|
33
|
+
from ..components import (
|
|
34
|
+
ActionIcon,
|
|
35
|
+
Anchor,
|
|
36
|
+
Avatar,
|
|
37
|
+
Badge,
|
|
38
|
+
Box,
|
|
39
|
+
Button,
|
|
40
|
+
Card,
|
|
41
|
+
CardSection,
|
|
42
|
+
Center,
|
|
43
|
+
Collapse,
|
|
44
|
+
DataTable,
|
|
45
|
+
Grid,
|
|
46
|
+
GridColumn,
|
|
47
|
+
Group,
|
|
48
|
+
Icon,
|
|
49
|
+
Image,
|
|
50
|
+
Loader,
|
|
51
|
+
Modal,
|
|
52
|
+
NumberInput,
|
|
53
|
+
Pagination,
|
|
54
|
+
Paper,
|
|
55
|
+
Progress,
|
|
56
|
+
RingProgress,
|
|
57
|
+
SegmentedControl,
|
|
58
|
+
Select,
|
|
59
|
+
SimpleGrid,
|
|
60
|
+
Skeleton,
|
|
61
|
+
Stack,
|
|
62
|
+
Text,
|
|
63
|
+
Textarea,
|
|
64
|
+
TextInput,
|
|
65
|
+
ThemeIcon,
|
|
66
|
+
UnstyledButton,
|
|
67
|
+
)
|
|
68
|
+
from ..components import EmptyState as _EmptyState
|
|
69
|
+
|
|
70
|
+
__all__ = [
|
|
71
|
+
"CardEvent",
|
|
72
|
+
"DashboardGrid",
|
|
73
|
+
"EmptyState",
|
|
74
|
+
"FilterPanel",
|
|
75
|
+
"GridAction",
|
|
76
|
+
"InfoCard",
|
|
77
|
+
"StatItem",
|
|
78
|
+
"StatsCard",
|
|
79
|
+
"StatsEvent",
|
|
80
|
+
"StatsGrid",
|
|
81
|
+
"TableBlock",
|
|
82
|
+
"UserCard",
|
|
83
|
+
"UserEvent",
|
|
84
|
+
"UsersTable",
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
_MAX_TEXT = 500
|
|
88
|
+
_MAX_ITEMS = 200
|
|
89
|
+
_MAX_ROWS = 100_000
|
|
90
|
+
_EMAIL = re.compile(r"^[^@\s]{1,100}@[^@\s]{1,100}\.[^@\s]{1,60}$")
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# ------------------------------------------------------------------ helpers
|
|
94
|
+
def _finite(path: str, v: Any, *, allow_none: bool = False) -> float | None:
|
|
95
|
+
if v is None and allow_none:
|
|
96
|
+
return None
|
|
97
|
+
if isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v):
|
|
98
|
+
raise ValidationError(f"{path}: expected a finite number")
|
|
99
|
+
return v
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _number(path: str, v: Any) -> float:
|
|
103
|
+
return cast("float", _finite(path, v))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _text(path: str, v: Any, *, limit: int = _MAX_TEXT) -> str:
|
|
107
|
+
if not isinstance(v, str) or len(v) > limit:
|
|
108
|
+
raise ValidationError(f"{path}: expected text of at most {limit} characters")
|
|
109
|
+
return v
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _choice(path: str, v: Any, allowed: Sequence[str]) -> str:
|
|
113
|
+
if not isinstance(v, str) or v not in allowed:
|
|
114
|
+
raise ValidationError(f"{path}: one of {list(allowed)}")
|
|
115
|
+
return v
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _iso(path: str, v: Any) -> _dt.date:
|
|
119
|
+
if isinstance(v, _dt.datetime):
|
|
120
|
+
raise ValidationError(f"{path}: expected a date, not a datetime")
|
|
121
|
+
if isinstance(v, _dt.date):
|
|
122
|
+
return v
|
|
123
|
+
if isinstance(v, str):
|
|
124
|
+
try:
|
|
125
|
+
return _dt.date.fromisoformat(v)
|
|
126
|
+
except ValueError:
|
|
127
|
+
pass
|
|
128
|
+
raise ValidationError(f"{path}: expected an ISO date (YYYY-MM-DD)")
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _num(v: float, decimals: int = 0, prefix: str = "", suffix: str = "") -> str:
|
|
132
|
+
return f"{prefix}{v:,.{decimals}f}{suffix}"
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _percent(value: float, goal: float | None) -> float:
|
|
136
|
+
if goal is None or goal <= 0:
|
|
137
|
+
return 0.0
|
|
138
|
+
return max(0.0, min(100.0, value / goal * 100))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _list(path: str, values: Sequence[Any]) -> list[Any]:
|
|
142
|
+
if isinstance(values, (str, Mapping)) or not isinstance(values, Sequence): # pyright: ignore[reportUnnecessaryIsInstance]
|
|
143
|
+
raise ValidationError(f"{path}: expected a list")
|
|
144
|
+
out = list(values)
|
|
145
|
+
if len(out) > _MAX_ITEMS:
|
|
146
|
+
raise ValidationError(f"{path}: at most {_MAX_ITEMS}")
|
|
147
|
+
return out
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _strings(path: str, values: Sequence[str], limit: int = 100) -> list[str]:
|
|
151
|
+
return [_text(path, v, limit=limit) for v in _list(path, values)]
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _pairs(path: str, values: Sequence[tuple[str, str]]) -> list[tuple[str, str]]:
|
|
155
|
+
return [(_text(path, a, limit=100), _text(path, b, limit=100)) for a, b in _list(path, values)]
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _str(v: Any) -> str:
|
|
159
|
+
return v if isinstance(v, str) else ""
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _icon(name: str, **kw: Any) -> Icon:
|
|
163
|
+
"""An ``Icon`` by registry name (unknown names show a placeholder)."""
|
|
164
|
+
return Icon(cast("Any", name), **kw)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class _Emits(Component):
|
|
168
|
+
"""Blocks emit typed events from Python (the page never sends them)."""
|
|
169
|
+
|
|
170
|
+
def wire_events(self) -> list[str]:
|
|
171
|
+
return []
|
|
172
|
+
|
|
173
|
+
def _on_client_event(self, name: str, payload: Mapping[str, Any]) -> Any:
|
|
174
|
+
raise ValidationError(f"{self.TYPE}.{name}: not a page event")
|
|
175
|
+
|
|
176
|
+
def _emit(self, event: Event) -> None:
|
|
177
|
+
w = self._window
|
|
178
|
+
if w is not None and w.live:
|
|
179
|
+
w.dispatch(self, event)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _specs(*names: str, cls: str) -> Mapping[str, EventSpec]:
|
|
183
|
+
return {n: EventSpec(n, cls) for n in names}
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
# ------------------------------------------------------------------- events
|
|
187
|
+
@dataclass
|
|
188
|
+
class StatsEvent(Event):
|
|
189
|
+
"""``goal_change`` (accepted goal), ``period_change``, ``metric_change``,
|
|
190
|
+
``step`` (the stepper moved to ``date``) and ``action`` (a card button)."""
|
|
191
|
+
|
|
192
|
+
goal: float | None = None
|
|
193
|
+
period: str | None = None
|
|
194
|
+
metric: str | None = None
|
|
195
|
+
date: str | None = None
|
|
196
|
+
action: str = ""
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
@dataclass
|
|
200
|
+
class CardEvent(Event):
|
|
201
|
+
"""``action`` of a card/tile/empty state: ``action`` names it, ``key`` the
|
|
202
|
+
tile, ``value`` a typed value (favourite flag, note text, page ...)."""
|
|
203
|
+
|
|
204
|
+
action: str = ""
|
|
205
|
+
key: Any = None
|
|
206
|
+
value: Any = None
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
@dataclass
|
|
210
|
+
class UserEvent(Event):
|
|
211
|
+
"""UsersTable events: ``save`` (action ``add``/``edit``), ``role_change``
|
|
212
|
+
and ``action`` (message, note, terminate, delete ...) about row ``key``;
|
|
213
|
+
``values`` carries the validated fields."""
|
|
214
|
+
|
|
215
|
+
action: str = ""
|
|
216
|
+
key: Any = None
|
|
217
|
+
values: dict[str, Any] = field(default_factory=dict[str, Any])
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
@dataclass(frozen=True)
|
|
221
|
+
class StatItem:
|
|
222
|
+
"""One metric of a StatsGrid / stepper StatsCard. ``delta`` is a percent
|
|
223
|
+
change (sign sets the trend colour/arrow); ``share`` a percent for
|
|
224
|
+
``segments``; ``goal`` enables value/goal displays."""
|
|
225
|
+
|
|
226
|
+
key: str
|
|
227
|
+
label: str
|
|
228
|
+
value: float
|
|
229
|
+
goal: float | None = None
|
|
230
|
+
delta: float | None = None
|
|
231
|
+
caption: str | None = None
|
|
232
|
+
icon: str | None = None
|
|
233
|
+
color: str | None = None
|
|
234
|
+
share: float | None = None
|
|
235
|
+
prefix: str = ""
|
|
236
|
+
suffix: str = ""
|
|
237
|
+
decimals: int = 0
|
|
238
|
+
|
|
239
|
+
def __post_init__(self) -> None:
|
|
240
|
+
_text("StatItem.key", self.key, limit=64)
|
|
241
|
+
_text("StatItem.label", self.label, limit=200)
|
|
242
|
+
_finite("StatItem.value", self.value)
|
|
243
|
+
for n in ("goal", "delta", "share"):
|
|
244
|
+
_finite(f"StatItem.{n}", getattr(self, n), allow_none=True)
|
|
245
|
+
if not 0 <= self.decimals <= 6:
|
|
246
|
+
raise ValidationError("StatItem.decimals: 0..6")
|
|
247
|
+
|
|
248
|
+
def show(self, v: float | None = None) -> str:
|
|
249
|
+
return _num(self.value if v is None else v, self.decimals, self.prefix, self.suffix)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _items(path: str, values: Sequence[StatItem]) -> list[StatItem]:
|
|
253
|
+
out = _list(path, values)
|
|
254
|
+
if not all(isinstance(i, StatItem) for i in out):
|
|
255
|
+
raise ValidationError(f"{path}: expected StatItem objects")
|
|
256
|
+
items = cast("list[StatItem]", out)
|
|
257
|
+
if len({i.key for i in items}) != len(items):
|
|
258
|
+
raise ValidationError(f"{path}: duplicate keys")
|
|
259
|
+
return items
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _trend(delta: float | None) -> tuple[str, str]:
|
|
263
|
+
"""(icon, colour) of a delta; flat or missing is neutral."""
|
|
264
|
+
if delta is None or delta == 0:
|
|
265
|
+
return "minus", "gray"
|
|
266
|
+
return ("arrow-up", "teal") if delta > 0 else ("arrow-down", "red")
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _delta_text(delta: float | None) -> str:
|
|
270
|
+
return "" if delta is None else f"{delta:+g}%"
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
# =================================================================== StatsCard
|
|
274
|
+
_STATS_VARIANTS = ("plain", "progress", "progress-colored", "challenge", "stepper", "ring")
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
class StatsCard(_Emits, Paper):
|
|
278
|
+
"""One metric card; ``variant`` picks the layout.
|
|
279
|
+
|
|
280
|
+
* ``plain``: title, value, delta arrow, caption (dashboards).
|
|
281
|
+
* ``progress``: value / goal bar with an editable, validated goal
|
|
282
|
+
(``on_goal_change``; ``validate_goal(goal) -> error text | None``).
|
|
283
|
+
* ``progress-colored``: coloured goal card; ``metrics``/``periods`` selectors
|
|
284
|
+
emit ``metric_change``/``period_change`` so Python derives the goal.
|
|
285
|
+
* ``challenge``: delivered/goal with a ``days_left`` badge, period selector.
|
|
286
|
+
* ``stepper``: ``items`` (``StatItem`` with goals) for the day ``date``;
|
|
287
|
+
the arrows emit ``step`` (never past ``max_date``).
|
|
288
|
+
* ``ring``: ring progress with a button (``action`` event).
|
|
289
|
+
|
|
290
|
+
Push new numbers with ``set_stats(...)``/``set_items``; ``select_period``/
|
|
291
|
+
``select_metric`` move the selectors from Python without an event."""
|
|
292
|
+
|
|
293
|
+
EVENTS: ClassVar[Mapping[str, EventSpec]] = _specs(
|
|
294
|
+
"goal_change", "period_change", "metric_change", "step", "action", cls="StatsEvent"
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
def __init__(
|
|
298
|
+
self,
|
|
299
|
+
*,
|
|
300
|
+
variant: Literal[
|
|
301
|
+
"plain", "progress", "progress-colored", "challenge", "stepper", "ring"
|
|
302
|
+
] = "plain",
|
|
303
|
+
title: str = "",
|
|
304
|
+
value: float = 0,
|
|
305
|
+
goal: float | None = None,
|
|
306
|
+
delta: float | None = None,
|
|
307
|
+
caption: str | None = None,
|
|
308
|
+
prefix: str = "",
|
|
309
|
+
suffix: str = "",
|
|
310
|
+
decimals: int = 0,
|
|
311
|
+
unit: str = "orders",
|
|
312
|
+
icon: str | None = None,
|
|
313
|
+
color: str | None = None,
|
|
314
|
+
period: str | None = None,
|
|
315
|
+
periods: Sequence[str] = (),
|
|
316
|
+
metric: str | None = None,
|
|
317
|
+
metrics: Sequence[str] = (),
|
|
318
|
+
items: Sequence[StatItem] = (),
|
|
319
|
+
date: str | _dt.date | None = None,
|
|
320
|
+
max_date: str | _dt.date | None = None,
|
|
321
|
+
days_left: int | None = None,
|
|
322
|
+
button_label: str | None = None,
|
|
323
|
+
validate_goal: Callable[[float], str | None] | None = None,
|
|
324
|
+
on_goal_change: Handler[StatsEvent] | None = None,
|
|
325
|
+
on_period_change: Handler[StatsEvent] | None = None,
|
|
326
|
+
on_metric_change: Handler[StatsEvent] | None = None,
|
|
327
|
+
on_step: Handler[StatsEvent] | None = None,
|
|
328
|
+
on_action: Handler[StatsEvent] | None = None,
|
|
329
|
+
with_border: bool = True,
|
|
330
|
+
visible: bool | Unset = UNSET,
|
|
331
|
+
classes: str | Sequence[str] | Unset = UNSET,
|
|
332
|
+
styles: Mapping[str, str | int | float] | Unset = UNSET,
|
|
333
|
+
aria_label: str | Unset = UNSET,
|
|
334
|
+
) -> None:
|
|
335
|
+
_choice("StatsCard.variant", variant, _STATS_VARIANTS)
|
|
336
|
+
super().__init__(
|
|
337
|
+
with_border=with_border,
|
|
338
|
+
p="md",
|
|
339
|
+
radius="md",
|
|
340
|
+
bg=(color or "blue.7") if variant == "progress-colored" else UNSET,
|
|
341
|
+
visible=visible,
|
|
342
|
+
classes=classes,
|
|
343
|
+
styles=styles,
|
|
344
|
+
aria_label=aria_label,
|
|
345
|
+
)
|
|
346
|
+
self._variant = variant
|
|
347
|
+
self._title = _text("title", title, limit=200)
|
|
348
|
+
self._value = _number("value", value)
|
|
349
|
+
self._goal = _finite("goal", goal, allow_none=True)
|
|
350
|
+
self._delta = _finite("delta", delta, allow_none=True)
|
|
351
|
+
self._caption = None if caption is None else _text("caption", caption)
|
|
352
|
+
self._prefix = _text("prefix", prefix, limit=20)
|
|
353
|
+
self._suffix = _text("suffix", suffix, limit=40)
|
|
354
|
+
if not 0 <= decimals <= 6:
|
|
355
|
+
raise ValidationError("decimals: 0..6")
|
|
356
|
+
self._decimals = decimals
|
|
357
|
+
self._unit = _text("unit", unit, limit=40)
|
|
358
|
+
self._icon, self._color = icon, color
|
|
359
|
+
self._days_left = days_left
|
|
360
|
+
self._validate_goal = validate_goal
|
|
361
|
+
self._periods = _strings("periods", periods, 60)
|
|
362
|
+
self._metrics = _strings("metrics", metrics, 60)
|
|
363
|
+
self._period = (
|
|
364
|
+
period if period is not None else (self._periods[0] if self._periods else None)
|
|
365
|
+
)
|
|
366
|
+
self._metric = (
|
|
367
|
+
metric if metric is not None else (self._metrics[0] if self._metrics else None)
|
|
368
|
+
)
|
|
369
|
+
for n, v, allowed in (
|
|
370
|
+
("period", self._period, self._periods),
|
|
371
|
+
("metric", self._metric, self._metrics),
|
|
372
|
+
):
|
|
373
|
+
if v is not None and v not in allowed:
|
|
374
|
+
raise ValidationError(f"{n}: one of {allowed}")
|
|
375
|
+
self._items = _items("items", items)
|
|
376
|
+
self._date = _iso("date", date) if date is not None else None
|
|
377
|
+
self._max_date = _iso("max_date", max_date) if max_date is not None else None
|
|
378
|
+
if variant == "stepper" and self._date is None:
|
|
379
|
+
raise ValidationError("the stepper variant needs date")
|
|
380
|
+
self._button_label = button_label
|
|
381
|
+
# component handles (set by the variant builder)
|
|
382
|
+
self.value_text: Text | None = None
|
|
383
|
+
self.delta_text: Text | None = None
|
|
384
|
+
self.caption_text: Text | None = None
|
|
385
|
+
self.title_text: Text | None = None
|
|
386
|
+
self.percent_text: Text | None = None
|
|
387
|
+
self.day_text: Text | None = None
|
|
388
|
+
self.month_text: Text | None = None
|
|
389
|
+
self.delta_icon: Icon | None = None
|
|
390
|
+
self.progress: Progress | None = None
|
|
391
|
+
self.ring: RingProgress | None = None
|
|
392
|
+
self.goal_input: NumberInput | None = None
|
|
393
|
+
self.save_button: Button | None = None
|
|
394
|
+
self.action_button: Button | None = None
|
|
395
|
+
self.period_control: SegmentedControl | None = None
|
|
396
|
+
self.metric_control: SegmentedControl | None = None
|
|
397
|
+
self.refresh_button: ActionIcon | None = None
|
|
398
|
+
self.days_badge: Badge | None = None
|
|
399
|
+
self.prev_button: UnstyledButton | None = None
|
|
400
|
+
self.next_button: UnstyledButton | None = None
|
|
401
|
+
self._cells: list[tuple[StatItem, Text, Text]] = []
|
|
402
|
+
self._cell_box: Group | None = None
|
|
403
|
+
with self:
|
|
404
|
+
getattr(self, "_build_" + variant.replace("-", "_"))()
|
|
405
|
+
for ev, fn in (
|
|
406
|
+
("goal_change", on_goal_change),
|
|
407
|
+
("period_change", on_period_change),
|
|
408
|
+
("metric_change", on_metric_change),
|
|
409
|
+
("step", on_step),
|
|
410
|
+
("action", on_action),
|
|
411
|
+
):
|
|
412
|
+
if fn is not None:
|
|
413
|
+
self.on(ev, fn)
|
|
414
|
+
self._refresh()
|
|
415
|
+
|
|
416
|
+
# ------------------------------------------------------------ read API
|
|
417
|
+
@property
|
|
418
|
+
def variant(self) -> str:
|
|
419
|
+
return self._variant
|
|
420
|
+
|
|
421
|
+
@property
|
|
422
|
+
def value(self) -> float:
|
|
423
|
+
return self._value
|
|
424
|
+
|
|
425
|
+
@property
|
|
426
|
+
def goal(self) -> float | None:
|
|
427
|
+
return self._goal
|
|
428
|
+
|
|
429
|
+
@property
|
|
430
|
+
def delta(self) -> float | None:
|
|
431
|
+
return self._delta
|
|
432
|
+
|
|
433
|
+
@property
|
|
434
|
+
def period(self) -> str | None:
|
|
435
|
+
return self._period
|
|
436
|
+
|
|
437
|
+
@property
|
|
438
|
+
def metric(self) -> str | None:
|
|
439
|
+
return self._metric
|
|
440
|
+
|
|
441
|
+
@property
|
|
442
|
+
def date(self) -> _dt.date | None:
|
|
443
|
+
return self._date
|
|
444
|
+
|
|
445
|
+
@property
|
|
446
|
+
def percent(self) -> float:
|
|
447
|
+
return _percent(self._value, self._goal)
|
|
448
|
+
|
|
449
|
+
@property
|
|
450
|
+
def stat_items(self) -> tuple[StatItem, ...]:
|
|
451
|
+
return tuple(self._items)
|
|
452
|
+
|
|
453
|
+
def _fmt(self, v: float) -> str:
|
|
454
|
+
return _num(v, self._decimals, self._prefix, self._suffix)
|
|
455
|
+
|
|
456
|
+
# ----------------------------------------------------------- builders
|
|
457
|
+
def _head(self) -> None:
|
|
458
|
+
light = self._variant == "progress-colored"
|
|
459
|
+
self.title_text = Text(
|
|
460
|
+
self._title, size="xs", fw=700, dimmed=not light, c="white" if light else UNSET
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
def _build_plain(self) -> None:
|
|
464
|
+
with Stack(gap=4):
|
|
465
|
+
with Group(justify="space-between"):
|
|
466
|
+
self._head()
|
|
467
|
+
if self._icon:
|
|
468
|
+
_icon(self._icon, size=20)
|
|
469
|
+
with Group(gap="xs", align="flex-end"):
|
|
470
|
+
self.value_text = Text("", size="xl", fw=700)
|
|
471
|
+
self.delta_text = Text("", size="sm", fw=700)
|
|
472
|
+
self.delta_icon = _icon("minus", size=16)
|
|
473
|
+
self.caption_text = Text("", size="xs", dimmed=True)
|
|
474
|
+
|
|
475
|
+
def _build_progress(self) -> None:
|
|
476
|
+
with Stack(gap="sm"):
|
|
477
|
+
self._head()
|
|
478
|
+
self.value_text = Text("", size="lg", fw=500)
|
|
479
|
+
self.progress = Progress(value=0, size="lg", radius="xl", aria_label="Goal progress")
|
|
480
|
+
with Group(align="flex-start", gap="xs", wrap="nowrap"):
|
|
481
|
+
self.goal_input = NumberInput(
|
|
482
|
+
label="Change goal",
|
|
483
|
+
placeholder="e.g. 80000",
|
|
484
|
+
min=0,
|
|
485
|
+
prefix=self._prefix or UNSET,
|
|
486
|
+
thousand_separator=",",
|
|
487
|
+
hide_controls=True,
|
|
488
|
+
debounce=0,
|
|
489
|
+
)
|
|
490
|
+
self.save_button = Button("Save goal", variant="default", on_click=self._save_goal)
|
|
491
|
+
|
|
492
|
+
def _build_progress_colored(self) -> None:
|
|
493
|
+
with Stack(gap="sm"):
|
|
494
|
+
with Group(gap="xs"):
|
|
495
|
+
if self._metrics:
|
|
496
|
+
self.metric_control = SegmentedControl(
|
|
497
|
+
value=self._metric,
|
|
498
|
+
data=list(self._metrics),
|
|
499
|
+
size="xs",
|
|
500
|
+
aria_label="Metric",
|
|
501
|
+
on_change=self._metric_changed,
|
|
502
|
+
)
|
|
503
|
+
if self._periods:
|
|
504
|
+
self.period_control = SegmentedControl(
|
|
505
|
+
value=self._period,
|
|
506
|
+
data=list(self._periods),
|
|
507
|
+
size="xs",
|
|
508
|
+
aria_label="Period",
|
|
509
|
+
on_change=self._period_changed,
|
|
510
|
+
)
|
|
511
|
+
self._head()
|
|
512
|
+
self.value_text = Text("", size="lg", fw=500, c="white")
|
|
513
|
+
self.progress = Progress(
|
|
514
|
+
value=0, size="lg", radius="xl", color="gray.0", aria_label="Goal progress"
|
|
515
|
+
)
|
|
516
|
+
|
|
517
|
+
def _build_challenge(self) -> None:
|
|
518
|
+
with Stack(gap="xs"):
|
|
519
|
+
with Group(gap="xs", justify="space-between"):
|
|
520
|
+
if self._periods:
|
|
521
|
+
self.period_control = SegmentedControl(
|
|
522
|
+
value=self._period,
|
|
523
|
+
data=list(self._periods),
|
|
524
|
+
size="xs",
|
|
525
|
+
aria_label="Period",
|
|
526
|
+
on_change=self._period_changed,
|
|
527
|
+
)
|
|
528
|
+
self.refresh_button = ActionIcon(
|
|
529
|
+
icon="refresh",
|
|
530
|
+
variant="default",
|
|
531
|
+
aria_label="Refresh",
|
|
532
|
+
on_click=lambda: self._emit_stats("action", action="refresh"),
|
|
533
|
+
)
|
|
534
|
+
with Center():
|
|
535
|
+
with ThemeIcon(size=60, radius=60):
|
|
536
|
+
_icon(self._icon or "truck", size=32)
|
|
537
|
+
self.title_text = Text(self._title, ta="center", fw=700)
|
|
538
|
+
self.caption_text = Text("", ta="center", dimmed=True, size="sm")
|
|
539
|
+
with Group(justify="space-between"):
|
|
540
|
+
Text("Progress", size="sm", dimmed=True)
|
|
541
|
+
self.percent_text = Text("0%", size="sm", dimmed=True)
|
|
542
|
+
self.progress = Progress(value=0, aria_label="Progress")
|
|
543
|
+
with Group(justify="space-between"):
|
|
544
|
+
self.value_text = Text("", size="sm")
|
|
545
|
+
self.days_badge = Badge("", size="sm")
|
|
546
|
+
|
|
547
|
+
def _build_stepper(self) -> None:
|
|
548
|
+
with Group(gap="md", align="center", wrap="nowrap"):
|
|
549
|
+
with Stack(gap=2, align="center"):
|
|
550
|
+
self.next_button = UnstyledButton(
|
|
551
|
+
children=[_icon("chevron-up", size=16)],
|
|
552
|
+
aria_label="Next day",
|
|
553
|
+
on_click=lambda: self._step(1),
|
|
554
|
+
)
|
|
555
|
+
self.day_text = Text("", size="xl", fw=700)
|
|
556
|
+
self.month_text = Text("", size="xs", dimmed=True)
|
|
557
|
+
self.prev_button = UnstyledButton(
|
|
558
|
+
children=[_icon("chevron-down", size=16)],
|
|
559
|
+
aria_label="Previous day",
|
|
560
|
+
on_click=lambda: self._step(-1),
|
|
561
|
+
)
|
|
562
|
+
self._cell_box = Group(gap="sm")
|
|
563
|
+
self._build_cells()
|
|
564
|
+
|
|
565
|
+
def _build_cells(self) -> None:
|
|
566
|
+
assert self._cell_box is not None
|
|
567
|
+
self._cell_box.clear()
|
|
568
|
+
self._cells = []
|
|
569
|
+
with self._cell_box:
|
|
570
|
+
for item in self._items:
|
|
571
|
+
with Paper(shadow="md", p="xs", radius="md", aria_label=item.label):
|
|
572
|
+
with Group(gap="sm", wrap="nowrap"):
|
|
573
|
+
if item.icon:
|
|
574
|
+
_icon(item.icon, size=32)
|
|
575
|
+
with Stack(gap=0):
|
|
576
|
+
Text(item.label, size="sm", fw=700)
|
|
577
|
+
head = Text("", size="xs", fw=700)
|
|
578
|
+
tail = Text("", size="xs", dimmed=True)
|
|
579
|
+
self._cells.append((item, head, tail))
|
|
580
|
+
|
|
581
|
+
def _build_ring(self) -> None:
|
|
582
|
+
with Group(gap="md", wrap="nowrap"):
|
|
583
|
+
self.ring = RingProgress(
|
|
584
|
+
sections=[{"value": 0, "color": self._color or "blue"}], size=90, thickness=8
|
|
585
|
+
)
|
|
586
|
+
with Stack(gap=4):
|
|
587
|
+
self._head()
|
|
588
|
+
self.value_text = Text("", size="lg", fw=700)
|
|
589
|
+
self.caption_text = Text("", size="xs", dimmed=True)
|
|
590
|
+
if self._button_label:
|
|
591
|
+
self.action_button = Button(
|
|
592
|
+
self._button_label,
|
|
593
|
+
variant="light",
|
|
594
|
+
size="xs",
|
|
595
|
+
on_click=lambda: self._emit_stats("action", action="primary"),
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
# ------------------------------------------------------------ rendering
|
|
599
|
+
def _refresh(self) -> None:
|
|
600
|
+
v, g = self._value, self._goal
|
|
601
|
+
pct = _percent(v, g)
|
|
602
|
+
if self.value_text is not None:
|
|
603
|
+
if self._variant == "challenge":
|
|
604
|
+
text = f"{_num(v)} / {_num(g or 0)} {self._unit}"
|
|
605
|
+
elif self._variant in ("plain", "ring") or g is None:
|
|
606
|
+
text = self._fmt(v)
|
|
607
|
+
else:
|
|
608
|
+
text = f"{self._fmt(v)} / {self._fmt(g)}"
|
|
609
|
+
self.value_text.update(text=text)
|
|
610
|
+
if self.progress is not None:
|
|
611
|
+
self.progress.update(value=round(pct, 2))
|
|
612
|
+
if self.ring is not None:
|
|
613
|
+
self.ring.update(
|
|
614
|
+
sections=[{"value": round(pct, 2), "color": self._color or "blue"}],
|
|
615
|
+
label=f"{pct:.0f}%",
|
|
616
|
+
)
|
|
617
|
+
if self.percent_text is not None:
|
|
618
|
+
self.percent_text.update(text=f"{pct:.0f}%")
|
|
619
|
+
icon, colour = _trend(self._delta)
|
|
620
|
+
if self.delta_text is not None:
|
|
621
|
+
self.delta_text.update(text=_delta_text(self._delta), c=colour)
|
|
622
|
+
if self.delta_icon is not None:
|
|
623
|
+
self.delta_icon.update(name=icon, color=colour)
|
|
624
|
+
if self.caption_text is not None:
|
|
625
|
+
if self._variant == "challenge":
|
|
626
|
+
what = self._caption or f"delivered {self._unit}"
|
|
627
|
+
per = f" / {self._period}" if self._period else ""
|
|
628
|
+
self.caption_text.update(text=f"{_num(g or 0)} {what}{per}")
|
|
629
|
+
else:
|
|
630
|
+
self.caption_text.update(text=self._caption or "")
|
|
631
|
+
if self.days_badge is not None:
|
|
632
|
+
self.days_badge.update(text=f"{self._days_left or 0} days left")
|
|
633
|
+
if self._variant == "stepper":
|
|
634
|
+
assert self._date is not None
|
|
635
|
+
if self.day_text is not None:
|
|
636
|
+
self.day_text.update(text=f"{self._date.day:02d}")
|
|
637
|
+
if self.month_text is not None:
|
|
638
|
+
self.month_text.update(text=self._date.strftime("%B %Y"))
|
|
639
|
+
if self.next_button is not None:
|
|
640
|
+
self.next_button.update(
|
|
641
|
+
disabled=self._max_date is not None and self._date >= self._max_date
|
|
642
|
+
)
|
|
643
|
+
for item, head, tail in self._cells:
|
|
644
|
+
head.update(text=item.show())
|
|
645
|
+
tail.update(text="" if item.goal is None else f"of {item.show(item.goal)}")
|
|
646
|
+
|
|
647
|
+
def set_stats(
|
|
648
|
+
self,
|
|
649
|
+
*,
|
|
650
|
+
value: float | Unset = UNSET,
|
|
651
|
+
goal: float | Unset | None = UNSET,
|
|
652
|
+
delta: float | Unset | None = UNSET,
|
|
653
|
+
caption: str | Unset | None = UNSET,
|
|
654
|
+
days_left: int | Unset = UNSET,
|
|
655
|
+
title: str | Unset = UNSET,
|
|
656
|
+
) -> None:
|
|
657
|
+
"""Replace numbers shown by the card (typed, finite) and redraw."""
|
|
658
|
+
if value is not UNSET:
|
|
659
|
+
self._value = _number("value", value)
|
|
660
|
+
if goal is not UNSET:
|
|
661
|
+
self._goal = _finite("goal", goal, allow_none=True)
|
|
662
|
+
if delta is not UNSET:
|
|
663
|
+
self._delta = _finite("delta", delta, allow_none=True)
|
|
664
|
+
if caption is not UNSET:
|
|
665
|
+
self._caption = None if caption is None else _text("caption", caption)
|
|
666
|
+
if days_left is not UNSET:
|
|
667
|
+
self._days_left = days_left
|
|
668
|
+
if title is not UNSET:
|
|
669
|
+
self._title = _text("title", title, limit=200)
|
|
670
|
+
if self.title_text is not None:
|
|
671
|
+
self.title_text.update(text=self._title)
|
|
672
|
+
self._refresh()
|
|
673
|
+
|
|
674
|
+
def set_items(self, items: Sequence[StatItem], *, date: str | _dt.date | None = None) -> None:
|
|
675
|
+
"""Stepper: replace the metric cards (and optionally the shown day)."""
|
|
676
|
+
if self._variant != "stepper":
|
|
677
|
+
raise ValidationError("set_items: only the stepper variant has items")
|
|
678
|
+
self._items = _items("items", items)
|
|
679
|
+
if date is not None:
|
|
680
|
+
self._date = _iso("date", date)
|
|
681
|
+
self._build_cells()
|
|
682
|
+
self._refresh()
|
|
683
|
+
|
|
684
|
+
def select_period(self, period: str) -> None:
|
|
685
|
+
if self.period_control is None or period not in self._periods:
|
|
686
|
+
raise ValidationError(f"period: one of {self._periods}")
|
|
687
|
+
self._period = period
|
|
688
|
+
self.period_control.value = period
|
|
689
|
+
self._refresh()
|
|
690
|
+
|
|
691
|
+
def select_metric(self, metric: str) -> None:
|
|
692
|
+
if self.metric_control is None or metric not in self._metrics:
|
|
693
|
+
raise ValidationError(f"metric: one of {self._metrics}")
|
|
694
|
+
self._metric = metric
|
|
695
|
+
self.metric_control.value = metric
|
|
696
|
+
|
|
697
|
+
# -------------------------------------------------------------- events
|
|
698
|
+
def _emit_stats(self, name: str, **kw: Any) -> None:
|
|
699
|
+
self._emit(StatsEvent(name=name, sender=self, window=self._window, **kw))
|
|
700
|
+
|
|
701
|
+
def _period_changed(self, ev: Any) -> None:
|
|
702
|
+
if ev.value in self._periods:
|
|
703
|
+
self._period = ev.value
|
|
704
|
+
self._refresh()
|
|
705
|
+
self._emit_stats("period_change", period=ev.value)
|
|
706
|
+
|
|
707
|
+
def _metric_changed(self, ev: Any) -> None:
|
|
708
|
+
if ev.value in self._metrics:
|
|
709
|
+
self._metric = ev.value
|
|
710
|
+
self._emit_stats("metric_change", metric=ev.value)
|
|
711
|
+
|
|
712
|
+
def _step(self, days: int) -> None:
|
|
713
|
+
assert self._date is not None
|
|
714
|
+
nxt = self._date + _dt.timedelta(days=days)
|
|
715
|
+
if self._max_date is not None and nxt > self._max_date:
|
|
716
|
+
return
|
|
717
|
+
self._date = nxt
|
|
718
|
+
self._refresh()
|
|
719
|
+
self._emit_stats("step", date=nxt.isoformat())
|
|
720
|
+
|
|
721
|
+
def _save_goal(self) -> None:
|
|
722
|
+
"""Validate the typed goal; only an accepted one changes the card."""
|
|
723
|
+
assert self.goal_input is not None
|
|
724
|
+
raw = self.goal_input.value
|
|
725
|
+
err: str | None = None
|
|
726
|
+
goal = 0.0
|
|
727
|
+
if not isinstance(raw, (int, float)) or isinstance(raw, bool):
|
|
728
|
+
err = "Enter a goal"
|
|
729
|
+
else:
|
|
730
|
+
goal = float(raw)
|
|
731
|
+
if not math.isfinite(goal) or goal < 0:
|
|
732
|
+
err = "Goal must be a number of at least 0"
|
|
733
|
+
elif self._validate_goal is not None:
|
|
734
|
+
err = self._validate_goal(goal)
|
|
735
|
+
if err:
|
|
736
|
+
self.goal_input.update(error=err)
|
|
737
|
+
return
|
|
738
|
+
self._goal = goal
|
|
739
|
+
self.goal_input.update(error="", value=None)
|
|
740
|
+
self._refresh()
|
|
741
|
+
self._emit_stats("goal_change", goal=goal)
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
# =================================================================== StatsGrid
|
|
745
|
+
_GRID_VARIANTS = ("plain", "icons", "group", "ring", "segments")
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
class StatsGrid(_Emits, Stack):
|
|
749
|
+
"""KPI strip from one list of ``StatItem``; ``variant``: ``plain`` (label,
|
|
750
|
+
value, trend), ``icons`` (trend ThemeIcon), ``group`` (headline numbers on
|
|
751
|
+
a coloured band), ``ring`` (value/goal ring) or ``segments`` (shares of a
|
|
752
|
+
total with ``total``/``delta``/``caption``). ``periods`` adds a selector
|
|
753
|
+
that emits ``period_change`` (:class:`StatsEvent`). ``set_items`` updates
|
|
754
|
+
the cards in place when the keys are unchanged."""
|
|
755
|
+
|
|
756
|
+
EVENTS: ClassVar[Mapping[str, EventSpec]] = _specs("period_change", cls="StatsEvent")
|
|
757
|
+
|
|
758
|
+
def __init__(
|
|
759
|
+
self,
|
|
760
|
+
items: Sequence[StatItem],
|
|
761
|
+
*,
|
|
762
|
+
variant: Literal["plain", "icons", "group", "ring", "segments"] = "plain",
|
|
763
|
+
cols: int = 4,
|
|
764
|
+
total: float | None = None,
|
|
765
|
+
delta: float | None = None,
|
|
766
|
+
caption: str | None = None,
|
|
767
|
+
periods: Sequence[str] = (),
|
|
768
|
+
period: str | None = None,
|
|
769
|
+
on_period_change: Handler[StatsEvent] | None = None,
|
|
770
|
+
visible: bool | Unset = UNSET,
|
|
771
|
+
classes: str | Sequence[str] | Unset = UNSET,
|
|
772
|
+
styles: Mapping[str, str | int | float] | Unset = UNSET,
|
|
773
|
+
aria_label: str | Unset = UNSET,
|
|
774
|
+
) -> None:
|
|
775
|
+
_choice("StatsGrid.variant", variant, _GRID_VARIANTS)
|
|
776
|
+
if not 1 <= cols <= 12:
|
|
777
|
+
raise ValidationError("cols: 1..12")
|
|
778
|
+
super().__init__(
|
|
779
|
+
gap="md", visible=visible, classes=classes, styles=styles, aria_label=aria_label
|
|
780
|
+
)
|
|
781
|
+
self._variant = variant
|
|
782
|
+
self._cols = cols
|
|
783
|
+
self._items = _items("items", items)
|
|
784
|
+
self._total = _finite("total", total, allow_none=True)
|
|
785
|
+
self._delta = _finite("delta", delta, allow_none=True)
|
|
786
|
+
self._caption = caption
|
|
787
|
+
self._periods = _strings("periods", periods, 60)
|
|
788
|
+
self._period = (
|
|
789
|
+
period if period is not None else (self._periods[0] if self._periods else None)
|
|
790
|
+
)
|
|
791
|
+
if self._period is not None and self._period not in self._periods:
|
|
792
|
+
raise ValidationError(f"period: one of {self._periods}")
|
|
793
|
+
self.period_control: SegmentedControl | None = None
|
|
794
|
+
self.body: Component | None = None
|
|
795
|
+
self.total_text: Text | None = None
|
|
796
|
+
self.delta_text: Text | None = None
|
|
797
|
+
self.caption_text: Text | None = None
|
|
798
|
+
self.bar: Progress | None = None
|
|
799
|
+
self._cells: dict[str, dict[str, Component]] = {}
|
|
800
|
+
with self:
|
|
801
|
+
if self._periods:
|
|
802
|
+
self.period_control = SegmentedControl(
|
|
803
|
+
value=self._period,
|
|
804
|
+
data=list(self._periods),
|
|
805
|
+
size="xs",
|
|
806
|
+
aria_label="Period",
|
|
807
|
+
on_change=self._period_changed,
|
|
808
|
+
)
|
|
809
|
+
self._build_body()
|
|
810
|
+
if on_period_change is not None:
|
|
811
|
+
self.on("period_change", on_period_change)
|
|
812
|
+
self._refresh()
|
|
813
|
+
|
|
814
|
+
@property
|
|
815
|
+
def variant(self) -> str:
|
|
816
|
+
return self._variant
|
|
817
|
+
|
|
818
|
+
@property
|
|
819
|
+
def period(self) -> str | None:
|
|
820
|
+
return self._period
|
|
821
|
+
|
|
822
|
+
@property
|
|
823
|
+
def items(self) -> tuple[StatItem, ...]:
|
|
824
|
+
return tuple(self._items)
|
|
825
|
+
|
|
826
|
+
def select_period(self, period: str) -> None:
|
|
827
|
+
if self.period_control is None or period not in self._periods:
|
|
828
|
+
raise ValidationError(f"period: one of {self._periods}")
|
|
829
|
+
self._period = period
|
|
830
|
+
self.period_control.value = period
|
|
831
|
+
|
|
832
|
+
# ------------------------------------------------------------- building
|
|
833
|
+
def _build_body(self) -> None:
|
|
834
|
+
v = self._variant
|
|
835
|
+
if v == "group":
|
|
836
|
+
self.body = Paper(p="md", radius="md", bg="blue.7")
|
|
837
|
+
with self.body, Group(gap="xl", justify="space-around", wrap="wrap"):
|
|
838
|
+
self._build_cells()
|
|
839
|
+
elif v == "segments":
|
|
840
|
+
self.body = Paper(with_border=True, p="md", radius="md")
|
|
841
|
+
with self.body, Stack(gap="sm"):
|
|
842
|
+
with Group(gap="xs", align="flex-end"):
|
|
843
|
+
self.total_text = Text("", size="xl", fw=700)
|
|
844
|
+
self.delta_text = Text("", size="sm", fw=700)
|
|
845
|
+
self.caption_text = Text("", size="sm", dimmed=True)
|
|
846
|
+
self.bar = Progress(sections=[], size=34, aria_label="Shares")
|
|
847
|
+
with SimpleGrid(cols=min(self._cols, 4), spacing="md"):
|
|
848
|
+
self._build_cells()
|
|
849
|
+
else:
|
|
850
|
+
self.body = SimpleGrid(cols=self._cols, spacing="md")
|
|
851
|
+
with self.body:
|
|
852
|
+
self._build_cells()
|
|
853
|
+
|
|
854
|
+
def _build_cells(self) -> None:
|
|
855
|
+
self._cells = {}
|
|
856
|
+
for item in self._items:
|
|
857
|
+
self._cells[item.key] = getattr(self, "_cell_" + self._variant)(item)
|
|
858
|
+
|
|
859
|
+
def _cell_plain(self, item: StatItem) -> dict[str, Component]:
|
|
860
|
+
with Paper(with_border=True, p="md", radius="md", aria_label=item.label):
|
|
861
|
+
with Group(justify="space-between"):
|
|
862
|
+
Text(item.label, size="xs", fw=700, dimmed=True)
|
|
863
|
+
if item.icon:
|
|
864
|
+
_icon(item.icon, size=18)
|
|
865
|
+
with Group(gap="xs", align="flex-end"):
|
|
866
|
+
value = Text("", size="xl", fw=700)
|
|
867
|
+
delta = Text("", size="sm", fw=700)
|
|
868
|
+
arrow = _icon("minus", size=16)
|
|
869
|
+
caption = Text("", size="xs", dimmed=True)
|
|
870
|
+
return {"value": value, "delta": delta, "arrow": arrow, "caption": caption}
|
|
871
|
+
|
|
872
|
+
def _cell_icons(self, item: StatItem) -> dict[str, Component]:
|
|
873
|
+
with Paper(with_border=True, p="md", radius="md", aria_label=item.label):
|
|
874
|
+
with Group(justify="space-between"):
|
|
875
|
+
with Stack(gap=0):
|
|
876
|
+
Text(item.label, size="xs", fw=700, dimmed=True)
|
|
877
|
+
value = Text("", size="xl", fw=700)
|
|
878
|
+
with ThemeIcon(color="gray", variant="light", size=38, radius="md") as theme:
|
|
879
|
+
arrow = _icon("minus", size=24)
|
|
880
|
+
with Group(gap=4):
|
|
881
|
+
delta = Text("", size="sm", fw=700)
|
|
882
|
+
caption = Text("", size="xs", dimmed=True)
|
|
883
|
+
return {"value": value, "delta": delta, "arrow": arrow, "caption": caption, "theme": theme}
|
|
884
|
+
|
|
885
|
+
def _cell_group(self, item: StatItem) -> dict[str, Component]:
|
|
886
|
+
with Stack(gap=0, align="center", aria_label=item.label):
|
|
887
|
+
value = Text("", size="xl", fw=700, c="white")
|
|
888
|
+
Text(item.label, size="sm", c="white", fw=700)
|
|
889
|
+
caption = Text("", size="xs", c="gray.2")
|
|
890
|
+
return {"value": value, "caption": caption}
|
|
891
|
+
|
|
892
|
+
def _cell_ring(self, item: StatItem) -> dict[str, Component]:
|
|
893
|
+
with Paper(with_border=True, p="md", radius="md", aria_label=item.label):
|
|
894
|
+
with Group(gap="md", wrap="nowrap"):
|
|
895
|
+
ring = RingProgress(sections=[{"value": 0, "color": item.color or "blue"}], size=80)
|
|
896
|
+
with Stack(gap=0):
|
|
897
|
+
Text(item.label, size="xs", fw=700, dimmed=True)
|
|
898
|
+
value = Text("", size="lg", fw=700)
|
|
899
|
+
caption = Text("", size="xs", dimmed=True)
|
|
900
|
+
return {"value": value, "caption": caption, "ring": ring}
|
|
901
|
+
|
|
902
|
+
def _cell_segments(self, item: StatItem) -> dict[str, Component]:
|
|
903
|
+
with Box(aria_label=item.label):
|
|
904
|
+
Text(item.label, size="xs", fw=700, dimmed=True)
|
|
905
|
+
with Group(justify="space-between", align="flex-end", gap=0):
|
|
906
|
+
value = Text("", fw=700)
|
|
907
|
+
share = Text("", size="sm", fw=700, c=item.color or UNSET)
|
|
908
|
+
return {"value": value, "share": share}
|
|
909
|
+
|
|
910
|
+
# ------------------------------------------------------------ rendering
|
|
911
|
+
def _refresh(self) -> None:
|
|
912
|
+
for item in self._items:
|
|
913
|
+
self._apply(item, self._cells[item.key])
|
|
914
|
+
if self._variant == "segments":
|
|
915
|
+
if self.total_text is not None:
|
|
916
|
+
self.total_text.update(text=_num(self._total or 0))
|
|
917
|
+
if self.delta_text is not None:
|
|
918
|
+
self.delta_text.update(text=_delta_text(self._delta), c=_trend(self._delta)[1])
|
|
919
|
+
if self.caption_text is not None:
|
|
920
|
+
self.caption_text.update(text=self._caption or "")
|
|
921
|
+
if self.bar is not None:
|
|
922
|
+
self.bar.update(
|
|
923
|
+
sections=[
|
|
924
|
+
{
|
|
925
|
+
"value": i.share or 0,
|
|
926
|
+
"color": i.color or "gray",
|
|
927
|
+
"label": f"{i.share:g}%" if (i.share or 0) > 10 else "",
|
|
928
|
+
}
|
|
929
|
+
for i in self._items
|
|
930
|
+
]
|
|
931
|
+
)
|
|
932
|
+
|
|
933
|
+
def _apply(self, item: StatItem, cell: dict[str, Component]) -> None:
|
|
934
|
+
def put(key: str, **kw: Any) -> None:
|
|
935
|
+
c = cell.get(key)
|
|
936
|
+
if c is not None:
|
|
937
|
+
c.update(**kw)
|
|
938
|
+
|
|
939
|
+
put("value", text=item.show())
|
|
940
|
+
icon, colour = _trend(item.delta)
|
|
941
|
+
put("delta", text=_delta_text(item.delta), c=colour)
|
|
942
|
+
put("arrow", name=icon, color=colour)
|
|
943
|
+
put("theme", color=colour)
|
|
944
|
+
if self._variant == "ring":
|
|
945
|
+
pct = _percent(item.value, item.goal)
|
|
946
|
+
put(
|
|
947
|
+
"ring",
|
|
948
|
+
sections=[{"value": round(pct, 2), "color": item.color or "blue"}],
|
|
949
|
+
label=f"{pct:.0f}%",
|
|
950
|
+
)
|
|
951
|
+
put(
|
|
952
|
+
"caption",
|
|
953
|
+
text=item.caption or ("" if item.goal is None else f"of {item.show(item.goal)}"),
|
|
954
|
+
)
|
|
955
|
+
elif self._variant == "segments":
|
|
956
|
+
put("share", text="" if item.share is None else f"{item.share:g}%")
|
|
957
|
+
else:
|
|
958
|
+
put("caption", text=item.caption or "")
|
|
959
|
+
|
|
960
|
+
def set_items(
|
|
961
|
+
self,
|
|
962
|
+
items: Sequence[StatItem],
|
|
963
|
+
*,
|
|
964
|
+
total: float | Unset | None = UNSET,
|
|
965
|
+
delta: float | Unset | None = UNSET,
|
|
966
|
+
caption: str | Unset | None = UNSET,
|
|
967
|
+
) -> None:
|
|
968
|
+
"""Replace the metrics: same keys update in place, otherwise the cards are rebuilt."""
|
|
969
|
+
new = _items("items", items)
|
|
970
|
+
if total is not UNSET:
|
|
971
|
+
self._total = _finite("total", total, allow_none=True)
|
|
972
|
+
if delta is not UNSET:
|
|
973
|
+
self._delta = _finite("delta", delta, allow_none=True)
|
|
974
|
+
if caption is not UNSET:
|
|
975
|
+
self._caption = caption
|
|
976
|
+
same = [i.key for i in new] == [i.key for i in self._items]
|
|
977
|
+
self._items = new
|
|
978
|
+
if not same:
|
|
979
|
+
assert self.body is not None
|
|
980
|
+
self.body.remove()
|
|
981
|
+
with self:
|
|
982
|
+
self._build_body()
|
|
983
|
+
self._refresh()
|
|
984
|
+
|
|
985
|
+
def update_item(self, key: str, **fields: Any) -> None:
|
|
986
|
+
"""Change fields of one metric (``value``, ``delta``, ``goal`` ...)."""
|
|
987
|
+
for i, item in enumerate(self._items):
|
|
988
|
+
if item.key == key:
|
|
989
|
+
data = {f: getattr(item, f) for f in item.__dataclass_fields__}
|
|
990
|
+
data.update(fields)
|
|
991
|
+
new = list(self._items)
|
|
992
|
+
new[i] = StatItem(**data)
|
|
993
|
+
self.set_items(new)
|
|
994
|
+
return
|
|
995
|
+
raise ValidationError(f"no metric {key!r}")
|
|
996
|
+
|
|
997
|
+
def _period_changed(self, ev: Any) -> None:
|
|
998
|
+
if ev.value in self._periods:
|
|
999
|
+
self._period = ev.value
|
|
1000
|
+
self._emit(
|
|
1001
|
+
StatsEvent(name="period_change", sender=self, window=self._window, period=ev.value)
|
|
1002
|
+
)
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
# ==================================================================== InfoCard
|
|
1006
|
+
_INFO_VARIANTS = ("badge", "features", "stats", "task")
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
class InfoCard(_Emits, Card):
|
|
1010
|
+
"""Content card; one parameterised layout.
|
|
1011
|
+
|
|
1012
|
+
* ``badge``: image, title + badge, description, badge list, action button
|
|
1013
|
+
and a favourite toggle (``favourite=`` not None).
|
|
1014
|
+
* ``features``: like ``badge`` with ``badges`` as feature chips and a CTA.
|
|
1015
|
+
* ``stats``: image, title, description, ``stats`` rows and a ring
|
|
1016
|
+
(``progress`` percent).
|
|
1017
|
+
* ``task``: title, assignee avatar, progress bar and due badge.
|
|
1018
|
+
|
|
1019
|
+
``action`` events carry ``CardEvent.action`` ``"primary"`` (the button) or
|
|
1020
|
+
``"favourite"`` (``value`` is the new bool; the card keeps the state)."""
|
|
1021
|
+
|
|
1022
|
+
EVENTS: ClassVar[Mapping[str, EventSpec]] = _specs("action", cls="CardEvent")
|
|
1023
|
+
|
|
1024
|
+
def __init__(
|
|
1025
|
+
self,
|
|
1026
|
+
*,
|
|
1027
|
+
variant: Literal["badge", "features", "stats", "task"] = "badge",
|
|
1028
|
+
title: str,
|
|
1029
|
+
description: str | None = None,
|
|
1030
|
+
image: str | None = None,
|
|
1031
|
+
badge: str | None = None,
|
|
1032
|
+
badges: Sequence[str] = (),
|
|
1033
|
+
stats: Sequence[tuple[str, str]] = (),
|
|
1034
|
+
progress: float | None = None,
|
|
1035
|
+
assignee: str | None = None,
|
|
1036
|
+
due: str | None = None,
|
|
1037
|
+
favourite: bool | None = None,
|
|
1038
|
+
action_label: str | None = None,
|
|
1039
|
+
on_action: Handler[CardEvent] | None = None,
|
|
1040
|
+
visible: bool | Unset = UNSET,
|
|
1041
|
+
classes: str | Sequence[str] | Unset = UNSET,
|
|
1042
|
+
styles: Mapping[str, str | int | float] | Unset = UNSET,
|
|
1043
|
+
aria_label: str | Unset = UNSET,
|
|
1044
|
+
) -> None:
|
|
1045
|
+
_choice("InfoCard.variant", variant, _INFO_VARIANTS)
|
|
1046
|
+
super().__init__(
|
|
1047
|
+
with_border=True,
|
|
1048
|
+
padding="md",
|
|
1049
|
+
radius="md",
|
|
1050
|
+
visible=visible,
|
|
1051
|
+
classes=classes,
|
|
1052
|
+
styles=styles,
|
|
1053
|
+
aria_label=aria_label,
|
|
1054
|
+
)
|
|
1055
|
+
self._variant = variant
|
|
1056
|
+
self._title = _text("title", title, limit=200)
|
|
1057
|
+
self._favourite = bool(favourite)
|
|
1058
|
+
self._progress = _finite("progress", progress, allow_none=True)
|
|
1059
|
+
self.title_text: Text | None = None
|
|
1060
|
+
self.description_text: Text | None = None
|
|
1061
|
+
self.badge_label: Badge | None = None
|
|
1062
|
+
self.progress_bar: Progress | None = None
|
|
1063
|
+
self.ring: RingProgress | None = None
|
|
1064
|
+
self.due_badge: Badge | None = None
|
|
1065
|
+
self.favourite_button: ActionIcon | None = None
|
|
1066
|
+
self.action_button: Button | None = None
|
|
1067
|
+
self.stat_texts: list[Text] = []
|
|
1068
|
+
stat_rows = _pairs("stats", stats)
|
|
1069
|
+
chips = _strings("badges", badges)
|
|
1070
|
+
with self:
|
|
1071
|
+
if variant != "task":
|
|
1072
|
+
with CardSection(with_border=not image):
|
|
1073
|
+
if image:
|
|
1074
|
+
Image(src=image, alt=title, h=160, fit="cover")
|
|
1075
|
+
else:
|
|
1076
|
+
Box(h=8)
|
|
1077
|
+
with Group(justify="space-between"):
|
|
1078
|
+
self.title_text = Text(self._title, fw=500)
|
|
1079
|
+
if badge and variant != "task":
|
|
1080
|
+
self.badge_label = Badge(badge, variant="light")
|
|
1081
|
+
if variant == "task" and assignee:
|
|
1082
|
+
Avatar(name=assignee, size="sm", color="initials", aria_label=assignee)
|
|
1083
|
+
if description:
|
|
1084
|
+
self.description_text = Text(description, size="sm", dimmed=True)
|
|
1085
|
+
if variant in ("badge", "features") and chips:
|
|
1086
|
+
with Group(gap=7):
|
|
1087
|
+
for b in chips:
|
|
1088
|
+
Badge(b, variant="light")
|
|
1089
|
+
if variant == "stats":
|
|
1090
|
+
with Group(justify="space-between", wrap="nowrap"):
|
|
1091
|
+
with Stack(gap=4):
|
|
1092
|
+
for label, val in stat_rows:
|
|
1093
|
+
with Group(gap="xs"):
|
|
1094
|
+
Text(label, size="xs", dimmed=True)
|
|
1095
|
+
self.stat_texts.append(Text(val, size="sm", fw=700))
|
|
1096
|
+
self.ring = RingProgress(
|
|
1097
|
+
sections=[{"value": self._progress or 0, "color": "blue"}],
|
|
1098
|
+
size=70,
|
|
1099
|
+
thickness=6,
|
|
1100
|
+
label=f"{self._progress or 0:.0f}%",
|
|
1101
|
+
)
|
|
1102
|
+
if variant == "task":
|
|
1103
|
+
self.progress_bar = Progress(value=self._progress or 0, aria_label="Progress")
|
|
1104
|
+
if due:
|
|
1105
|
+
self.due_badge = Badge(f"Due {due}", variant="light", size="sm")
|
|
1106
|
+
with Group(gap="xs", wrap="nowrap"):
|
|
1107
|
+
if action_label:
|
|
1108
|
+
self.action_button = Button(
|
|
1109
|
+
action_label,
|
|
1110
|
+
radius="md",
|
|
1111
|
+
full_width=True,
|
|
1112
|
+
on_click=lambda: self._act("primary"),
|
|
1113
|
+
)
|
|
1114
|
+
if favourite is not None and variant in ("badge", "features"):
|
|
1115
|
+
self.favourite_button = ActionIcon(
|
|
1116
|
+
icon="heart-filled" if self._favourite else "heart",
|
|
1117
|
+
variant="default",
|
|
1118
|
+
color="red" if self._favourite else UNSET,
|
|
1119
|
+
aria_label="Favourite",
|
|
1120
|
+
on_click=self._toggle_favourite,
|
|
1121
|
+
)
|
|
1122
|
+
if on_action is not None:
|
|
1123
|
+
self.on("action", on_action)
|
|
1124
|
+
|
|
1125
|
+
@property
|
|
1126
|
+
def variant(self) -> str:
|
|
1127
|
+
return self._variant
|
|
1128
|
+
|
|
1129
|
+
@property
|
|
1130
|
+
def favourite(self) -> bool:
|
|
1131
|
+
return self._favourite
|
|
1132
|
+
|
|
1133
|
+
@property
|
|
1134
|
+
def progress(self) -> float | None:
|
|
1135
|
+
return self._progress
|
|
1136
|
+
|
|
1137
|
+
def set_progress(self, percent: float) -> None:
|
|
1138
|
+
"""Task/stats variants: update the progress bar or ring (0..100)."""
|
|
1139
|
+
p = _number("progress", percent)
|
|
1140
|
+
if not 0 <= p <= 100:
|
|
1141
|
+
raise ValidationError("progress: 0..100")
|
|
1142
|
+
self._progress = p
|
|
1143
|
+
if self.progress_bar is not None:
|
|
1144
|
+
self.progress_bar.update(value=p)
|
|
1145
|
+
if self.ring is not None:
|
|
1146
|
+
self.ring.update(sections=[{"value": p, "color": "blue"}], label=f"{p:.0f}%")
|
|
1147
|
+
|
|
1148
|
+
def set_favourite(self, value: bool) -> None:
|
|
1149
|
+
self._favourite = bool(value)
|
|
1150
|
+
if self.favourite_button is not None:
|
|
1151
|
+
self.favourite_button.update(
|
|
1152
|
+
icon="heart-filled" if value else "heart", color="red" if value else UNSET
|
|
1153
|
+
)
|
|
1154
|
+
|
|
1155
|
+
def _act(self, action: str, value: Any = None) -> None:
|
|
1156
|
+
self._emit(
|
|
1157
|
+
CardEvent(name="action", sender=self, window=self._window, action=action, value=value)
|
|
1158
|
+
)
|
|
1159
|
+
|
|
1160
|
+
def _toggle_favourite(self) -> None:
|
|
1161
|
+
self.set_favourite(not self._favourite)
|
|
1162
|
+
self._act("favourite", self._favourite)
|
|
1163
|
+
|
|
1164
|
+
|
|
1165
|
+
# ==================================================================== UserCard
|
|
1166
|
+
_USER_VARIANTS = ("image", "action", "icons")
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
class UserCard(_Emits, Card):
|
|
1170
|
+
"""Profile card of one person.
|
|
1171
|
+
|
|
1172
|
+
* ``image``: banner image (or a plain band), avatar, name/role, ``stats``
|
|
1173
|
+
and a follow toggle (``action`` ``"follow"``, ``value`` the new bool).
|
|
1174
|
+
* ``action``: avatar, name, role and an action button (``"primary"``);
|
|
1175
|
+
with ``note=True`` it expands a note field whose submit emits ``action``
|
|
1176
|
+
``"note"`` (``value`` the text; empty text is rejected in the field).
|
|
1177
|
+
* ``icons``: avatar, name, role and phone/e-mail rows."""
|
|
1178
|
+
|
|
1179
|
+
EVENTS: ClassVar[Mapping[str, EventSpec]] = _specs("action", cls="CardEvent")
|
|
1180
|
+
|
|
1181
|
+
def __init__(
|
|
1182
|
+
self,
|
|
1183
|
+
*,
|
|
1184
|
+
variant: Literal["image", "action", "icons"] = "image",
|
|
1185
|
+
name: str,
|
|
1186
|
+
role: str | None = None,
|
|
1187
|
+
email: str | None = None,
|
|
1188
|
+
phone: str | None = None,
|
|
1189
|
+
avatar: str | None = None,
|
|
1190
|
+
image: str | None = None,
|
|
1191
|
+
stats: Sequence[tuple[str, str]] = (),
|
|
1192
|
+
following: bool = False,
|
|
1193
|
+
action_label: str | None = None,
|
|
1194
|
+
note: bool = False,
|
|
1195
|
+
on_action: Handler[CardEvent] | None = None,
|
|
1196
|
+
visible: bool | Unset = UNSET,
|
|
1197
|
+
classes: str | Sequence[str] | Unset = UNSET,
|
|
1198
|
+
styles: Mapping[str, str | int | float] | Unset = UNSET,
|
|
1199
|
+
aria_label: str | Unset = UNSET,
|
|
1200
|
+
) -> None:
|
|
1201
|
+
_choice("UserCard.variant", variant, _USER_VARIANTS)
|
|
1202
|
+
super().__init__(
|
|
1203
|
+
with_border=True,
|
|
1204
|
+
padding="lg",
|
|
1205
|
+
radius="md",
|
|
1206
|
+
visible=visible,
|
|
1207
|
+
classes=classes,
|
|
1208
|
+
styles=styles,
|
|
1209
|
+
aria_label=aria_label,
|
|
1210
|
+
)
|
|
1211
|
+
self._variant = variant
|
|
1212
|
+
self._name = _text("name", name, limit=200)
|
|
1213
|
+
self._following = bool(following)
|
|
1214
|
+
self.follow_button: Button | None = None
|
|
1215
|
+
self.action_button: Button | None = None
|
|
1216
|
+
self.note_collapse: Collapse | None = None
|
|
1217
|
+
self.note_input: Textarea | None = None
|
|
1218
|
+
self.send_button: Button | None = None
|
|
1219
|
+
self.name_text: Text | None = None
|
|
1220
|
+
self.stat_texts: list[Text] = []
|
|
1221
|
+
rows = _pairs("stats", stats)
|
|
1222
|
+
with self:
|
|
1223
|
+
if variant == "image":
|
|
1224
|
+
with CardSection():
|
|
1225
|
+
if image:
|
|
1226
|
+
Image(src=image, alt=name, h=100, fit="cover")
|
|
1227
|
+
else:
|
|
1228
|
+
Box(h=60, bg="blue.2")
|
|
1229
|
+
with Stack(gap="xs", align="center"):
|
|
1230
|
+
Avatar(src=avatar, name=name, size=80, radius=80, color="initials")
|
|
1231
|
+
self.name_text = Text(name, ta="center", fw=500, size="lg")
|
|
1232
|
+
if role:
|
|
1233
|
+
Text(role, ta="center", size="sm", dimmed=True)
|
|
1234
|
+
with Group(justify="center", gap=30):
|
|
1235
|
+
for label, val in rows:
|
|
1236
|
+
with Stack(gap=0, align="center"):
|
|
1237
|
+
self.stat_texts.append(Text(val, fw=500, size="lg"))
|
|
1238
|
+
Text(label, size="sm", dimmed=True)
|
|
1239
|
+
self.follow_button = Button(
|
|
1240
|
+
"Unfollow" if self._following else "Follow",
|
|
1241
|
+
variant="default" if self._following else "filled",
|
|
1242
|
+
radius="md",
|
|
1243
|
+
full_width=True,
|
|
1244
|
+
on_click=self._toggle_follow,
|
|
1245
|
+
)
|
|
1246
|
+
else:
|
|
1247
|
+
with Group(wrap="nowrap", justify="space-between"):
|
|
1248
|
+
with Group(wrap="nowrap", gap="md"):
|
|
1249
|
+
Avatar(src=avatar, name=name, size=60, radius=60, color="initials")
|
|
1250
|
+
with Stack(gap=2):
|
|
1251
|
+
self.name_text = Text(name, fw=500, size="lg")
|
|
1252
|
+
if role:
|
|
1253
|
+
Text(role, size="xs", dimmed=True)
|
|
1254
|
+
if variant == "icons":
|
|
1255
|
+
if phone:
|
|
1256
|
+
with Group(gap=6, wrap="nowrap"):
|
|
1257
|
+
_icon("phone", size=14)
|
|
1258
|
+
Text(phone, size="xs")
|
|
1259
|
+
if email:
|
|
1260
|
+
with Group(gap=6, wrap="nowrap"):
|
|
1261
|
+
_icon("mail", size=14)
|
|
1262
|
+
Text(email, size="xs")
|
|
1263
|
+
if variant == "action" and action_label:
|
|
1264
|
+
self.action_button = Button(
|
|
1265
|
+
action_label,
|
|
1266
|
+
variant="default",
|
|
1267
|
+
size="xs",
|
|
1268
|
+
on_click=self._action_clicked,
|
|
1269
|
+
)
|
|
1270
|
+
if variant == "action" and note:
|
|
1271
|
+
self.note_collapse = Collapse(expanded=False)
|
|
1272
|
+
with self.note_collapse, Stack(gap="xs"):
|
|
1273
|
+
self.note_input = Textarea(
|
|
1274
|
+
label="Note", autosize=True, min_rows=2, debounce=0
|
|
1275
|
+
)
|
|
1276
|
+
self.send_button = Button("Send", size="xs", on_click=self._send_note)
|
|
1277
|
+
self._has_note = note
|
|
1278
|
+
if on_action is not None:
|
|
1279
|
+
self.on("action", on_action)
|
|
1280
|
+
|
|
1281
|
+
@property
|
|
1282
|
+
def variant(self) -> str:
|
|
1283
|
+
return self._variant
|
|
1284
|
+
|
|
1285
|
+
@property
|
|
1286
|
+
def following(self) -> bool:
|
|
1287
|
+
return self._following
|
|
1288
|
+
|
|
1289
|
+
def set_following(self, value: bool) -> None:
|
|
1290
|
+
self._following = bool(value)
|
|
1291
|
+
if self.follow_button is not None:
|
|
1292
|
+
self.follow_button.update(
|
|
1293
|
+
label="Unfollow" if value else "Follow", variant="default" if value else "filled"
|
|
1294
|
+
)
|
|
1295
|
+
|
|
1296
|
+
def set_stat(self, index: int, value: str) -> None:
|
|
1297
|
+
"""Update one stat value (for example the follower count) from Python."""
|
|
1298
|
+
self.stat_texts[index].update(text=_text("stat", value, limit=100))
|
|
1299
|
+
|
|
1300
|
+
def _act(self, action: str, value: Any = None) -> None:
|
|
1301
|
+
self._emit(
|
|
1302
|
+
CardEvent(name="action", sender=self, window=self._window, action=action, value=value)
|
|
1303
|
+
)
|
|
1304
|
+
|
|
1305
|
+
def _toggle_follow(self) -> None:
|
|
1306
|
+
self.set_following(not self._following)
|
|
1307
|
+
self._act("follow", self._following)
|
|
1308
|
+
|
|
1309
|
+
def _action_clicked(self) -> None:
|
|
1310
|
+
if self.note_collapse is not None:
|
|
1311
|
+
self.note_collapse.update(expanded=not self.note_collapse.expanded)
|
|
1312
|
+
self._act("primary")
|
|
1313
|
+
|
|
1314
|
+
def _send_note(self) -> None:
|
|
1315
|
+
assert self.note_input is not None and self.note_collapse is not None
|
|
1316
|
+
text = _str(self.note_input.value).strip()
|
|
1317
|
+
if not text:
|
|
1318
|
+
self.note_input.update(error="Write something first")
|
|
1319
|
+
return
|
|
1320
|
+
self.note_input.update(error="", value="")
|
|
1321
|
+
self.note_collapse.update(expanded=False)
|
|
1322
|
+
self._act("note", text)
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
# ============================================================ data table helpers
|
|
1326
|
+
def _cmp_key(v: Any) -> tuple[int, Any]:
|
|
1327
|
+
if v is None:
|
|
1328
|
+
return (2, 0)
|
|
1329
|
+
if isinstance(v, bool):
|
|
1330
|
+
return (0, int(v))
|
|
1331
|
+
if isinstance(v, (int, float)):
|
|
1332
|
+
return (0, v)
|
|
1333
|
+
return (1, str(v).lower())
|
|
1334
|
+
|
|
1335
|
+
|
|
1336
|
+
def _local_provider(
|
|
1337
|
+
source: Callable[[], Sequence[Mapping[str, Any]]],
|
|
1338
|
+
*,
|
|
1339
|
+
search_fields: Sequence[str] = (),
|
|
1340
|
+
exact: Sequence[str] = (),
|
|
1341
|
+
keys: Sequence[str] = (),
|
|
1342
|
+
limit_rows: Callable[[], int | None] | None = None,
|
|
1343
|
+
seen: Callable[[Sequence[Mapping[str, Any]]], None] | None = None,
|
|
1344
|
+
) -> Provider:
|
|
1345
|
+
"""A bounded-page provider over an in-process list: search (substring over
|
|
1346
|
+
``search_fields``, default all text), per-column filters (``exact`` keys
|
|
1347
|
+
match equal, others contain), sort and the requested ``offset``/``limit``
|
|
1348
|
+
window. ``keys`` projects every row to the columns the table shows (a
|
|
1349
|
+
DataTable page may not carry rows with more keys than its page size, and
|
|
1350
|
+
unused fields should not travel anyway). ``limit_rows`` caps the list;
|
|
1351
|
+
``seen`` receives each served page."""
|
|
1352
|
+
|
|
1353
|
+
def provider(q: TableQuery) -> TablePage:
|
|
1354
|
+
rows = list(source())
|
|
1355
|
+
cap = limit_rows() if limit_rows else None
|
|
1356
|
+
if cap is not None:
|
|
1357
|
+
rows = rows[:cap]
|
|
1358
|
+
if q.search:
|
|
1359
|
+
needle = q.search.lower()
|
|
1360
|
+
rows = [
|
|
1361
|
+
r
|
|
1362
|
+
for r in rows
|
|
1363
|
+
if any(
|
|
1364
|
+
needle in str(r.get(f, "")).lower()
|
|
1365
|
+
for f in (search_fields or [k for k, v in r.items() if isinstance(v, str)])
|
|
1366
|
+
)
|
|
1367
|
+
]
|
|
1368
|
+
for key, want in q.filters.items():
|
|
1369
|
+
if isinstance(want, str) and want:
|
|
1370
|
+
if key in exact:
|
|
1371
|
+
rows = [r for r in rows if str(r.get(key, "")).lower() == want.lower()]
|
|
1372
|
+
else:
|
|
1373
|
+
rows = [r for r in rows if want.lower() in str(r.get(key, "")).lower()]
|
|
1374
|
+
if q.sort:
|
|
1375
|
+
rows.sort(key=lambda r: _cmp_key(r.get(q.sort or "")), reverse=q.direction == "desc")
|
|
1376
|
+
page = rows[q.offset : q.offset + q.limit]
|
|
1377
|
+
if keys:
|
|
1378
|
+
page = [{k: r[k] for k in keys if k in r} for r in page]
|
|
1379
|
+
if seen is not None:
|
|
1380
|
+
seen(page)
|
|
1381
|
+
return TablePage(rows=page, total=len(rows))
|
|
1382
|
+
|
|
1383
|
+
return provider
|
|
1384
|
+
|
|
1385
|
+
|
|
1386
|
+
def _rows(path: str, rows: Sequence[Mapping[str, Any]], key: str) -> list[dict[str, Any]]:
|
|
1387
|
+
if isinstance(rows, (str, Mapping)) or not isinstance(rows, Sequence): # pyright: ignore[reportUnnecessaryIsInstance]
|
|
1388
|
+
raise ValidationError(f"{path}: expected a list of mappings")
|
|
1389
|
+
out = [dict(r) for r in rows]
|
|
1390
|
+
if len(out) > _MAX_ROWS:
|
|
1391
|
+
raise ValidationError(f"{path}: at most {_MAX_ROWS} rows (use a provider for more)")
|
|
1392
|
+
seen: set[Any] = set()
|
|
1393
|
+
for i, r in enumerate(out):
|
|
1394
|
+
if key not in r or r[key] in seen:
|
|
1395
|
+
raise ValidationError(f"{path}[{i}]: missing or duplicate {key!r}")
|
|
1396
|
+
seen.add(r[key])
|
|
1397
|
+
return out
|
|
1398
|
+
|
|
1399
|
+
|
|
1400
|
+
# =================================================================== UsersTable
|
|
1401
|
+
_USERS_VARIANTS = ("table", "roles", "stack")
|
|
1402
|
+
|
|
1403
|
+
|
|
1404
|
+
def _user_columns(variant: str, roles: Sequence[str]) -> list[Column]:
|
|
1405
|
+
if variant == "table":
|
|
1406
|
+
return [
|
|
1407
|
+
Column("name", "Name", sortable=True, filter="text"),
|
|
1408
|
+
Column("email", "Email", sortable=True),
|
|
1409
|
+
Column("role", "Role", kind="badge", sortable=True, filter="select", options=roles),
|
|
1410
|
+
Column("status", "Status", kind="badge"),
|
|
1411
|
+
]
|
|
1412
|
+
if variant == "roles":
|
|
1413
|
+
return [
|
|
1414
|
+
Column("name", "Name", sortable=True),
|
|
1415
|
+
Column("email", "Email"),
|
|
1416
|
+
Column("role", "Role", editable=True, filter="select", options=roles),
|
|
1417
|
+
]
|
|
1418
|
+
return [
|
|
1419
|
+
Column("name", "Name", sortable=True),
|
|
1420
|
+
Column("email", "Email"),
|
|
1421
|
+
Column("job", "Job"),
|
|
1422
|
+
Column("rate", "Rate / hr", kind="number", sortable=True),
|
|
1423
|
+
]
|
|
1424
|
+
|
|
1425
|
+
|
|
1426
|
+
def _user_actions(variant: str) -> list[RowAction]:
|
|
1427
|
+
if variant == "table":
|
|
1428
|
+
return [
|
|
1429
|
+
RowAction("edit", "Edit"),
|
|
1430
|
+
RowAction("delete", "Delete", color="red", confirm="Delete {name}?"),
|
|
1431
|
+
]
|
|
1432
|
+
if variant == "stack":
|
|
1433
|
+
return [
|
|
1434
|
+
RowAction("edit", "Edit"),
|
|
1435
|
+
RowAction("message", "Message"),
|
|
1436
|
+
RowAction("note", "Note"),
|
|
1437
|
+
RowAction(
|
|
1438
|
+
"terminate", "Terminate", color="red", confirm="Terminate the contract of {name}?"
|
|
1439
|
+
),
|
|
1440
|
+
]
|
|
1441
|
+
return []
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
class UsersTable(_Emits, Stack):
|
|
1445
|
+
"""People table on a bounded provider.
|
|
1446
|
+
|
|
1447
|
+
``variant``: ``table`` (sort/search/filter, edit + add modal, delete),
|
|
1448
|
+
``roles`` (the role cell is edited inline and validated against ``roles``)
|
|
1449
|
+
or ``stack`` (edit, message, note and terminate row actions). Data is
|
|
1450
|
+
``users`` (in-process list served through a bounded provider; the block
|
|
1451
|
+
applies saves, role changes and removals to it) or ``provider`` (a
|
|
1452
|
+
``TableQuery -> TablePage`` callable; the application applies changes and
|
|
1453
|
+
calls ``refresh()``). Rows need an ``id``. Events (:class:`UserEvent`):
|
|
1454
|
+
``save`` (add/edit), ``role_change`` and ``action`` (message/note texts,
|
|
1455
|
+
delete, terminate)."""
|
|
1456
|
+
|
|
1457
|
+
EVENTS: ClassVar[Mapping[str, EventSpec]] = _specs(
|
|
1458
|
+
"save", "role_change", "action", cls="UserEvent"
|
|
1459
|
+
)
|
|
1460
|
+
|
|
1461
|
+
def __init__(
|
|
1462
|
+
self,
|
|
1463
|
+
*,
|
|
1464
|
+
users: Sequence[Mapping[str, Any]] | None = None,
|
|
1465
|
+
provider: Provider | None = None,
|
|
1466
|
+
variant: Literal["table", "roles", "stack"] = "table",
|
|
1467
|
+
roles: Sequence[str] = ("admin", "user", "guest"),
|
|
1468
|
+
page_size: int = 10,
|
|
1469
|
+
allow_add: bool = True,
|
|
1470
|
+
on_save: Handler[UserEvent] | None = None,
|
|
1471
|
+
on_role_change: Handler[UserEvent] | None = None,
|
|
1472
|
+
on_action: Handler[UserEvent] | None = None,
|
|
1473
|
+
visible: bool | Unset = UNSET,
|
|
1474
|
+
classes: str | Sequence[str] | Unset = UNSET,
|
|
1475
|
+
styles: Mapping[str, str | int | float] | Unset = UNSET,
|
|
1476
|
+
aria_label: str | Unset = UNSET,
|
|
1477
|
+
) -> None:
|
|
1478
|
+
_choice("UsersTable.variant", variant, _USERS_VARIANTS)
|
|
1479
|
+
if (users is None) == (provider is None):
|
|
1480
|
+
raise ValidationError("UsersTable: pass exactly one of users= and provider=")
|
|
1481
|
+
if not 1 <= page_size <= 200:
|
|
1482
|
+
raise ValidationError("page_size: 1..200")
|
|
1483
|
+
super().__init__(
|
|
1484
|
+
gap="sm", visible=visible, classes=classes, styles=styles, aria_label=aria_label
|
|
1485
|
+
)
|
|
1486
|
+
self._variant = variant
|
|
1487
|
+
self._roles = _strings("roles", roles, 60)
|
|
1488
|
+
self._local = None if users is None else _rows("users", users, "id")
|
|
1489
|
+
self._page: dict[Any, dict[str, Any]] = {}
|
|
1490
|
+
self._editing: Any = None
|
|
1491
|
+
self._noting: tuple[Any, str] | None = None
|
|
1492
|
+
if self._local is not None:
|
|
1493
|
+
data = self._local
|
|
1494
|
+
|
|
1495
|
+
def remember(page: Sequence[Mapping[str, Any]]) -> None:
|
|
1496
|
+
self._page = {r["id"]: dict(r) for r in page}
|
|
1497
|
+
|
|
1498
|
+
prov: Provider = _local_provider(
|
|
1499
|
+
lambda: data,
|
|
1500
|
+
search_fields=("name", "email", "role", "job"),
|
|
1501
|
+
exact=("role", "status"),
|
|
1502
|
+
keys=("id", *(c.key for c in _user_columns(variant, self._roles))),
|
|
1503
|
+
seen=remember,
|
|
1504
|
+
)
|
|
1505
|
+
else:
|
|
1506
|
+
assert provider is not None
|
|
1507
|
+
inner = provider
|
|
1508
|
+
|
|
1509
|
+
shown = ("id", *(c.key for c in _user_columns(variant, self._roles)))
|
|
1510
|
+
|
|
1511
|
+
def project(res: Any) -> Any:
|
|
1512
|
+
rows = res.rows if isinstance(res, TablePage) else res
|
|
1513
|
+
self._page = {r["id"]: dict(r) for r in rows if "id" in r}
|
|
1514
|
+
slim = [{k: r[k] for k in shown if k in r} for r in rows]
|
|
1515
|
+
return TablePage(rows=slim, total=res.total) if isinstance(res, TablePage) else slim
|
|
1516
|
+
|
|
1517
|
+
def wrapped(q: TableQuery) -> Any:
|
|
1518
|
+
res = inner(q)
|
|
1519
|
+
if not inspect.isawaitable(res):
|
|
1520
|
+
return project(res)
|
|
1521
|
+
|
|
1522
|
+
async def later() -> Any:
|
|
1523
|
+
return project(await res)
|
|
1524
|
+
|
|
1525
|
+
return later()
|
|
1526
|
+
|
|
1527
|
+
prov = wrapped
|
|
1528
|
+
self.table: DataTable
|
|
1529
|
+
self.add_button: Button | None = None
|
|
1530
|
+
self.edit_modal: Modal | None = None
|
|
1531
|
+
self.note_modal: Modal | None = None
|
|
1532
|
+
self.fields: dict[str, TextInput | Select] = {}
|
|
1533
|
+
self.note_input: Textarea | None = None
|
|
1534
|
+
self.save_button: Button | None = None
|
|
1535
|
+
self.note_button: Button | None = None
|
|
1536
|
+
with self:
|
|
1537
|
+
if allow_add and variant != "roles":
|
|
1538
|
+
with Group(justify="flex-end"):
|
|
1539
|
+
self.add_button = Button(
|
|
1540
|
+
"Add user", size="xs", on_click=lambda: self.open_edit(None)
|
|
1541
|
+
)
|
|
1542
|
+
actions = _user_actions(variant)
|
|
1543
|
+
self.table = DataTable(
|
|
1544
|
+
columns=cast("Any", _user_columns(variant, self._roles)),
|
|
1545
|
+
row_actions=cast("Any", actions) if actions else UNSET,
|
|
1546
|
+
page_size=page_size,
|
|
1547
|
+
searchable=True,
|
|
1548
|
+
striped=True,
|
|
1549
|
+
provider=prov,
|
|
1550
|
+
empty_text="No users",
|
|
1551
|
+
)
|
|
1552
|
+
if variant != "roles":
|
|
1553
|
+
self._build_edit_modal()
|
|
1554
|
+
if variant == "stack":
|
|
1555
|
+
self._build_note_modal()
|
|
1556
|
+
self.table.on("row_action", self._row_action)
|
|
1557
|
+
self.table.on("cell_edit", self._cell_edit)
|
|
1558
|
+
for ev, fn in (("save", on_save), ("role_change", on_role_change), ("action", on_action)):
|
|
1559
|
+
if fn is not None:
|
|
1560
|
+
self.on(ev, fn)
|
|
1561
|
+
|
|
1562
|
+
@property
|
|
1563
|
+
def variant(self) -> str:
|
|
1564
|
+
return self._variant
|
|
1565
|
+
|
|
1566
|
+
@property
|
|
1567
|
+
def users(self) -> list[dict[str, Any]]:
|
|
1568
|
+
"""Copy of the local list (empty when a provider serves the data)."""
|
|
1569
|
+
return [dict(r) for r in self._local or []]
|
|
1570
|
+
|
|
1571
|
+
def set_users(self, users: Sequence[Mapping[str, Any]]) -> None:
|
|
1572
|
+
if self._local is None:
|
|
1573
|
+
raise ValidationError("set_users: the table is served by a provider; use refresh()")
|
|
1574
|
+
self._local[:] = _rows("users", users, "id")
|
|
1575
|
+
self.refresh()
|
|
1576
|
+
|
|
1577
|
+
def refresh(self) -> None:
|
|
1578
|
+
"""Re-query the provider (data changed)."""
|
|
1579
|
+
self.table.refresh()
|
|
1580
|
+
|
|
1581
|
+
# ------------------------------------------------------------- building
|
|
1582
|
+
def _build_edit_modal(self) -> None:
|
|
1583
|
+
names = (
|
|
1584
|
+
[("name", "Name"), ("email", "Email"), ("role", "Role")]
|
|
1585
|
+
if self._variant == "table"
|
|
1586
|
+
else [("email", "Email"), ("job", "Job title")]
|
|
1587
|
+
)
|
|
1588
|
+
self.edit_modal = Modal(opened=False, title="User", centered=True)
|
|
1589
|
+
with self.edit_modal, Stack():
|
|
1590
|
+
for key, label in names:
|
|
1591
|
+
if key == "role":
|
|
1592
|
+
self.fields[key] = Select(label=label, data=list(self._roles))
|
|
1593
|
+
else:
|
|
1594
|
+
self.fields[key] = TextInput(label=label, debounce=0)
|
|
1595
|
+
self.save_button = Button("Save", on_click=self._submit_edit)
|
|
1596
|
+
|
|
1597
|
+
def _build_note_modal(self) -> None:
|
|
1598
|
+
self.note_modal = Modal(opened=False, title="Note", centered=True)
|
|
1599
|
+
with self.note_modal, Stack():
|
|
1600
|
+
self.note_input = Textarea(autosize=True, min_rows=3, debounce=0, aria_label="Text")
|
|
1601
|
+
self.note_button = Button("Save", on_click=self._submit_note)
|
|
1602
|
+
|
|
1603
|
+
# ------------------------------------------------------------- behaviour
|
|
1604
|
+
def _row(self, key: Any) -> dict[str, Any] | None:
|
|
1605
|
+
if self._local is not None:
|
|
1606
|
+
return next((r for r in self._local if r["id"] == key), None)
|
|
1607
|
+
return self._page.get(key)
|
|
1608
|
+
|
|
1609
|
+
def open_edit(self, key: Any | None) -> None:
|
|
1610
|
+
"""Open the add (``None``) or edit modal; fields start from the row."""
|
|
1611
|
+
if self.edit_modal is None:
|
|
1612
|
+
raise ValidationError("this variant edits inline")
|
|
1613
|
+
row = None if key is None else self._row(key)
|
|
1614
|
+
if key is not None and row is None:
|
|
1615
|
+
raise ValidationError(f"no user {key!r} on this page")
|
|
1616
|
+
self._editing = key
|
|
1617
|
+
for name, fld in self.fields.items():
|
|
1618
|
+
val = "" if row is None else str(row.get(name, "") or "")
|
|
1619
|
+
if isinstance(fld, Select):
|
|
1620
|
+
fld.update(error="", value=val or None)
|
|
1621
|
+
else:
|
|
1622
|
+
fld.update(error="", value=val)
|
|
1623
|
+
title = "Add user" if row is None else f"Edit {row.get('name', 'user')}"
|
|
1624
|
+
self.edit_modal.update(title=title, opened=True)
|
|
1625
|
+
|
|
1626
|
+
def _submit_edit(self) -> None:
|
|
1627
|
+
values: dict[str, Any] = {}
|
|
1628
|
+
ok = True
|
|
1629
|
+
for name, fld in self.fields.items():
|
|
1630
|
+
raw = fld.value
|
|
1631
|
+
raw = raw.strip() if isinstance(raw, str) else raw
|
|
1632
|
+
err = ""
|
|
1633
|
+
if name == "email":
|
|
1634
|
+
if not raw or not _EMAIL.match(str(raw)):
|
|
1635
|
+
err = "Enter a valid e-mail address"
|
|
1636
|
+
elif name == "role":
|
|
1637
|
+
if raw not in self._roles:
|
|
1638
|
+
err = f"Choose one of {', '.join(self._roles)}"
|
|
1639
|
+
elif not raw:
|
|
1640
|
+
err = "Required"
|
|
1641
|
+
fld.update(error=err)
|
|
1642
|
+
ok = ok and not err
|
|
1643
|
+
values[name] = raw
|
|
1644
|
+
if not ok:
|
|
1645
|
+
return
|
|
1646
|
+
assert self.edit_modal is not None
|
|
1647
|
+
key = self._editing
|
|
1648
|
+
action = "add" if key is None else "edit"
|
|
1649
|
+
if self._local is not None:
|
|
1650
|
+
if key is None:
|
|
1651
|
+
key = max((cast("int", r["id"]) for r in self._local), default=0) + 1
|
|
1652
|
+
self._local.append({"id": key, "status": "active", **values})
|
|
1653
|
+
else:
|
|
1654
|
+
row = self._row(key)
|
|
1655
|
+
if row is not None:
|
|
1656
|
+
row.update(values)
|
|
1657
|
+
self.refresh()
|
|
1658
|
+
self.edit_modal.update(opened=False)
|
|
1659
|
+
self._emit(
|
|
1660
|
+
UserEvent(
|
|
1661
|
+
name="save", sender=self, window=self._window, action=action, key=key, values=values
|
|
1662
|
+
)
|
|
1663
|
+
)
|
|
1664
|
+
|
|
1665
|
+
def _row_action(self, ev: RowActionEvent) -> None:
|
|
1666
|
+
if ev.action == "edit":
|
|
1667
|
+
self.open_edit(ev.key)
|
|
1668
|
+
elif ev.action in ("message", "note"):
|
|
1669
|
+
assert self.note_modal is not None and self.note_input is not None
|
|
1670
|
+
row = self._row(ev.key)
|
|
1671
|
+
who = (row or {}).get("name", "user")
|
|
1672
|
+
self._noting = (ev.key, ev.action)
|
|
1673
|
+
self.note_input.update(value="", error="")
|
|
1674
|
+
self.note_modal.update(
|
|
1675
|
+
title=f"{'Message to' if ev.action == 'message' else 'Note about'} {who}",
|
|
1676
|
+
opened=True,
|
|
1677
|
+
)
|
|
1678
|
+
else:
|
|
1679
|
+
if ev.action in ("delete", "terminate") and self._local is not None:
|
|
1680
|
+
self._local[:] = [r for r in self._local if r["id"] != ev.key]
|
|
1681
|
+
self.refresh()
|
|
1682
|
+
self._emit(
|
|
1683
|
+
UserEvent(
|
|
1684
|
+
name="action", sender=self, window=self._window, action=ev.action, key=ev.key
|
|
1685
|
+
)
|
|
1686
|
+
)
|
|
1687
|
+
|
|
1688
|
+
def _submit_note(self) -> None:
|
|
1689
|
+
assert self.note_input is not None and self.note_modal is not None and self._noting
|
|
1690
|
+
text = _str(self.note_input.value).strip()
|
|
1691
|
+
if not text:
|
|
1692
|
+
self.note_input.update(error="Text is required")
|
|
1693
|
+
return
|
|
1694
|
+
key, kind = self._noting
|
|
1695
|
+
self.note_modal.update(opened=False)
|
|
1696
|
+
self._emit(
|
|
1697
|
+
UserEvent(
|
|
1698
|
+
name="action",
|
|
1699
|
+
sender=self,
|
|
1700
|
+
window=self._window,
|
|
1701
|
+
action=kind,
|
|
1702
|
+
key=key,
|
|
1703
|
+
values={"text": text},
|
|
1704
|
+
)
|
|
1705
|
+
)
|
|
1706
|
+
|
|
1707
|
+
def _cell_edit(self, ev: TableEvent) -> None:
|
|
1708
|
+
if ev.column != "role":
|
|
1709
|
+
return
|
|
1710
|
+
key = ev.keys[0] if ev.keys else None
|
|
1711
|
+
if ev.value not in self._roles: # not an allowed role: revert the cell
|
|
1712
|
+
self.refresh()
|
|
1713
|
+
return
|
|
1714
|
+
if self._local is not None:
|
|
1715
|
+
row = self._row(key)
|
|
1716
|
+
if row is not None:
|
|
1717
|
+
row["role"] = ev.value
|
|
1718
|
+
self.refresh()
|
|
1719
|
+
self._emit(
|
|
1720
|
+
UserEvent(
|
|
1721
|
+
name="role_change",
|
|
1722
|
+
sender=self,
|
|
1723
|
+
window=self._window,
|
|
1724
|
+
action="role",
|
|
1725
|
+
key=key,
|
|
1726
|
+
values={"role": ev.value},
|
|
1727
|
+
)
|
|
1728
|
+
)
|
|
1729
|
+
|
|
1730
|
+
|
|
1731
|
+
# =================================================================== TableBlock
|
|
1732
|
+
_TABLE_VARIANTS = ("reviews", "scroll", "selection", "sort")
|
|
1733
|
+
|
|
1734
|
+
|
|
1735
|
+
class TableBlock(_Emits, Stack):
|
|
1736
|
+
"""Table presets on bounded pages.
|
|
1737
|
+
|
|
1738
|
+
``reviews``: component rows (title, year, review count and a split
|
|
1739
|
+
positive/negative bar) paged with ``Pagination``; rows need ``title``,
|
|
1740
|
+
``year``, ``positive``, ``negative``. The other variants wrap a
|
|
1741
|
+
``DataTable`` (``.table``): ``scroll`` (fixed-height virtual body with a
|
|
1742
|
+
sticky header and a row-count input), ``selection`` (select-all and
|
|
1743
|
+
per-row checkboxes; ``selected_keys``) and ``sort`` (sortable columns,
|
|
1744
|
+
search, paging). Data is ``rows`` (served in pages by the block) or
|
|
1745
|
+
``provider``; the inner table's events (``selection_change``, ``sort``,
|
|
1746
|
+
``row_click``, ``row_action``) are re-emitted with the same typed events."""
|
|
1747
|
+
|
|
1748
|
+
EVENTS: ClassVar[Mapping[str, EventSpec]] = {
|
|
1749
|
+
"selection_change": EventSpec("selection_change", "TableEvent"),
|
|
1750
|
+
"sort": EventSpec("sort", "TableEvent"),
|
|
1751
|
+
"row_click": EventSpec("row_click", "TableEvent"),
|
|
1752
|
+
"row_action": EventSpec("row_action", "RowActionEvent"),
|
|
1753
|
+
"page_change": EventSpec("page_change", "CardEvent"),
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
def __init__(
|
|
1757
|
+
self,
|
|
1758
|
+
columns: Sequence[Column | Mapping[str, Any]] | None = None,
|
|
1759
|
+
*,
|
|
1760
|
+
variant: Literal["reviews", "scroll", "selection", "sort"] = "sort",
|
|
1761
|
+
rows: Sequence[Mapping[str, Any]] | None = None,
|
|
1762
|
+
provider: Provider | None = None,
|
|
1763
|
+
row_key: str = "id",
|
|
1764
|
+
page_size: int = 10,
|
|
1765
|
+
height: int = 300,
|
|
1766
|
+
row_count: int | None = None,
|
|
1767
|
+
max_rows: int = 1000,
|
|
1768
|
+
row_actions: Sequence[RowAction] = (),
|
|
1769
|
+
on_selection_change: Handler[TableEvent] | None = None,
|
|
1770
|
+
on_sort: Handler[TableEvent] | None = None,
|
|
1771
|
+
on_row_click: Handler[TableEvent] | None = None,
|
|
1772
|
+
on_row_action: Handler[RowActionEvent] | None = None,
|
|
1773
|
+
on_page_change: Handler[CardEvent] | None = None,
|
|
1774
|
+
visible: bool | Unset = UNSET,
|
|
1775
|
+
classes: str | Sequence[str] | Unset = UNSET,
|
|
1776
|
+
styles: Mapping[str, str | int | float] | Unset = UNSET,
|
|
1777
|
+
aria_label: str | Unset = UNSET,
|
|
1778
|
+
) -> None:
|
|
1779
|
+
_choice("TableBlock.variant", variant, _TABLE_VARIANTS)
|
|
1780
|
+
if (rows is None) == (provider is None):
|
|
1781
|
+
raise ValidationError("TableBlock: pass exactly one of rows= and provider=")
|
|
1782
|
+
if variant == "reviews" and rows is None:
|
|
1783
|
+
raise ValidationError("TableBlock(variant='reviews') takes rows=")
|
|
1784
|
+
if variant != "reviews" and not columns:
|
|
1785
|
+
raise ValidationError(f"TableBlock(variant={variant!r}) needs columns")
|
|
1786
|
+
if not 1 <= page_size <= 200:
|
|
1787
|
+
raise ValidationError("page_size: 1..200")
|
|
1788
|
+
super().__init__(
|
|
1789
|
+
gap="sm", visible=visible, classes=classes, styles=styles, aria_label=aria_label
|
|
1790
|
+
)
|
|
1791
|
+
self._variant = variant
|
|
1792
|
+
self._row_key = row_key
|
|
1793
|
+
self._rows = None if rows is None else _rows("rows", rows, row_key)
|
|
1794
|
+
self._page_size = page_size
|
|
1795
|
+
self._max_rows = max_rows
|
|
1796
|
+
self._row_count: int | None = None
|
|
1797
|
+
if variant == "scroll":
|
|
1798
|
+
start = row_count or len(self._rows or []) or max_rows
|
|
1799
|
+
self._row_count = max(1, min(start, max_rows))
|
|
1800
|
+
self.table: DataTable | None = None
|
|
1801
|
+
self.pagination: Pagination | None = None
|
|
1802
|
+
self.count_input: NumberInput | None = None
|
|
1803
|
+
self.selected_text: Text | None = None
|
|
1804
|
+
self.review_rows: list[Component] = []
|
|
1805
|
+
self._review_box: Stack | None = None
|
|
1806
|
+
self._review_page = 1
|
|
1807
|
+
with self:
|
|
1808
|
+
if variant == "reviews":
|
|
1809
|
+
self._build_reviews()
|
|
1810
|
+
else:
|
|
1811
|
+
self._build_table(columns or [], provider, height, row_actions)
|
|
1812
|
+
if variant != "reviews":
|
|
1813
|
+
assert self.table is not None
|
|
1814
|
+
for ev, fn in (
|
|
1815
|
+
("selection_change", on_selection_change),
|
|
1816
|
+
("sort", on_sort),
|
|
1817
|
+
("row_click", on_row_click),
|
|
1818
|
+
("row_action", on_row_action),
|
|
1819
|
+
):
|
|
1820
|
+
self.table.on(ev, self._forward(ev))
|
|
1821
|
+
if fn is not None:
|
|
1822
|
+
self.on(ev, fn)
|
|
1823
|
+
elif on_page_change is not None:
|
|
1824
|
+
self.on("page_change", on_page_change)
|
|
1825
|
+
|
|
1826
|
+
@property
|
|
1827
|
+
def variant(self) -> str:
|
|
1828
|
+
return self._variant
|
|
1829
|
+
|
|
1830
|
+
@property
|
|
1831
|
+
def selected_keys(self) -> list[Any]:
|
|
1832
|
+
return [] if self.table is None else self.table.selected_keys
|
|
1833
|
+
|
|
1834
|
+
@property
|
|
1835
|
+
def row_count(self) -> int | None:
|
|
1836
|
+
return self._row_count
|
|
1837
|
+
|
|
1838
|
+
# ------------------------------------------------------------- DataTable
|
|
1839
|
+
def _build_table(
|
|
1840
|
+
self,
|
|
1841
|
+
columns: Sequence[Column | Mapping[str, Any]],
|
|
1842
|
+
provider: Provider | None,
|
|
1843
|
+
height: int,
|
|
1844
|
+
row_actions: Sequence[RowAction],
|
|
1845
|
+
) -> None:
|
|
1846
|
+
v = self._variant
|
|
1847
|
+
if self._rows is not None:
|
|
1848
|
+
data = self._rows
|
|
1849
|
+
prov: Provider = _local_provider(
|
|
1850
|
+
lambda: data,
|
|
1851
|
+
keys=(
|
|
1852
|
+
self._row_key,
|
|
1853
|
+
*(c.key if isinstance(c, Column) else str(c.get("key")) for c in columns),
|
|
1854
|
+
),
|
|
1855
|
+
limit_rows=(lambda: self._row_count) if v == "scroll" else None,
|
|
1856
|
+
)
|
|
1857
|
+
else:
|
|
1858
|
+
assert provider is not None
|
|
1859
|
+
prov = provider
|
|
1860
|
+
if v == "scroll":
|
|
1861
|
+
self.count_input = NumberInput(
|
|
1862
|
+
label="Rows",
|
|
1863
|
+
value=self._row_count,
|
|
1864
|
+
min=1,
|
|
1865
|
+
max=self._max_rows,
|
|
1866
|
+
allow_decimal=False,
|
|
1867
|
+
debounce=0,
|
|
1868
|
+
size="xs",
|
|
1869
|
+
on_change=self._count_changed,
|
|
1870
|
+
)
|
|
1871
|
+
if v == "selection":
|
|
1872
|
+
self.selected_text = Text("0 selected", size="sm", dimmed=True)
|
|
1873
|
+
self.table = DataTable(
|
|
1874
|
+
columns=cast("Any", list(columns)),
|
|
1875
|
+
row_key=self._row_key,
|
|
1876
|
+
page_size=self._page_size,
|
|
1877
|
+
provider=prov,
|
|
1878
|
+
selection="multiple" if v == "selection" else UNSET,
|
|
1879
|
+
searchable=v in ("sort", "scroll"),
|
|
1880
|
+
striped=True,
|
|
1881
|
+
virtual=True if v == "scroll" else UNSET,
|
|
1882
|
+
height=height if v == "scroll" else UNSET,
|
|
1883
|
+
row_actions=cast("Any", list(row_actions)) if row_actions else UNSET,
|
|
1884
|
+
)
|
|
1885
|
+
|
|
1886
|
+
def _forward(self, name: str) -> Callable[[Any], None]:
|
|
1887
|
+
def run(ev: Any) -> None:
|
|
1888
|
+
if name == "selection_change" and self.selected_text is not None:
|
|
1889
|
+
self.selected_text.update(text=f"{len(ev.keys)} selected")
|
|
1890
|
+
extra = {
|
|
1891
|
+
k: getattr(ev, k)
|
|
1892
|
+
for k in ("keys", "column", "value", "action", "key")
|
|
1893
|
+
if hasattr(ev, k)
|
|
1894
|
+
}
|
|
1895
|
+
self._emit(
|
|
1896
|
+
type(ev)(
|
|
1897
|
+
name=name,
|
|
1898
|
+
sender=self,
|
|
1899
|
+
payload=dict(ev.payload),
|
|
1900
|
+
window=self._window,
|
|
1901
|
+
**extra,
|
|
1902
|
+
)
|
|
1903
|
+
)
|
|
1904
|
+
|
|
1905
|
+
return run
|
|
1906
|
+
|
|
1907
|
+
def _count_changed(self, ev: Any) -> None:
|
|
1908
|
+
if ev.value is not None:
|
|
1909
|
+
self.set_row_count(int(ev.value))
|
|
1910
|
+
|
|
1911
|
+
def set_row_count(self, n: int) -> None:
|
|
1912
|
+
"""Scroll variant: how many rows the table holds (bounded to ``max_rows``)."""
|
|
1913
|
+
if self._variant != "scroll" or self.table is None:
|
|
1914
|
+
raise ValidationError("set_row_count: only the scroll variant")
|
|
1915
|
+
n = max(1, min(int(n), self._max_rows))
|
|
1916
|
+
if n != self._row_count:
|
|
1917
|
+
self._row_count = n
|
|
1918
|
+
self.table.refresh()
|
|
1919
|
+
|
|
1920
|
+
def set_rows(self, rows: Sequence[Mapping[str, Any]]) -> None:
|
|
1921
|
+
"""Replace the local rows (``rows=`` mode) and refresh the page."""
|
|
1922
|
+
if self._rows is None:
|
|
1923
|
+
raise ValidationError("set_rows: the block is served by a provider; use refresh()")
|
|
1924
|
+
self._rows[:] = _rows("rows", rows, self._row_key)
|
|
1925
|
+
self.refresh()
|
|
1926
|
+
|
|
1927
|
+
def refresh(self) -> None:
|
|
1928
|
+
if self.table is not None:
|
|
1929
|
+
self.table.refresh()
|
|
1930
|
+
else:
|
|
1931
|
+
self._draw_reviews()
|
|
1932
|
+
|
|
1933
|
+
def select(self, keys: Sequence[Any]) -> None:
|
|
1934
|
+
"""Selection variant: select rows from Python."""
|
|
1935
|
+
if self.table is None:
|
|
1936
|
+
raise ValidationError("select: no table")
|
|
1937
|
+
self.table.update(selected=list(keys))
|
|
1938
|
+
if self.selected_text is not None:
|
|
1939
|
+
self.selected_text.update(text=f"{len(keys)} selected")
|
|
1940
|
+
|
|
1941
|
+
# --------------------------------------------------------------- reviews
|
|
1942
|
+
def _build_reviews(self) -> None:
|
|
1943
|
+
with Group(justify="space-between", wrap="nowrap"):
|
|
1944
|
+
for h in ("Book title", "Year", "Reviews", "Reviews distribution"):
|
|
1945
|
+
Text(h, size="xs", fw=700, dimmed=True)
|
|
1946
|
+
self._review_box = Stack(gap="xs", aria_label="Reviews")
|
|
1947
|
+
self.pagination = Pagination(value=1, total=1, size="sm", on_change=self._page_changed)
|
|
1948
|
+
self._draw_reviews()
|
|
1949
|
+
|
|
1950
|
+
def _draw_reviews(self) -> None:
|
|
1951
|
+
assert self._rows is not None and self._review_box is not None
|
|
1952
|
+
pages = max(1, math.ceil(len(self._rows) / self._page_size))
|
|
1953
|
+
self._review_page = max(1, min(self._review_page, pages))
|
|
1954
|
+
start = (self._review_page - 1) * self._page_size
|
|
1955
|
+
page = self._rows[start : start + self._page_size]
|
|
1956
|
+
self._review_box.clear()
|
|
1957
|
+
self.review_rows = []
|
|
1958
|
+
with self._review_box:
|
|
1959
|
+
for r in page:
|
|
1960
|
+
pos, neg = float(r.get("positive", 0)), float(r.get("negative", 0))
|
|
1961
|
+
tot = pos + neg
|
|
1962
|
+
with Group(
|
|
1963
|
+
justify="space-between", wrap="nowrap", aria_label=str(r.get("title", ""))
|
|
1964
|
+
) as row:
|
|
1965
|
+
Anchor(str(r.get("title", "")), size="sm")
|
|
1966
|
+
Text(str(r.get("year", "")), size="sm")
|
|
1967
|
+
Text(f"{tot:,.0f}", size="sm")
|
|
1968
|
+
Progress(
|
|
1969
|
+
sections=[
|
|
1970
|
+
{"value": round(pos / tot * 100, 2) if tot else 0, "color": "teal"},
|
|
1971
|
+
{"value": round(neg / tot * 100, 2) if tot else 0, "color": "red"},
|
|
1972
|
+
],
|
|
1973
|
+
aria_label=f"{pos:.0f} positive, {neg:.0f} negative",
|
|
1974
|
+
)
|
|
1975
|
+
self.review_rows.append(row)
|
|
1976
|
+
if self.pagination is not None:
|
|
1977
|
+
self.pagination.update(total=pages, value=self._review_page)
|
|
1978
|
+
|
|
1979
|
+
def _page_changed(self, ev: Any) -> None:
|
|
1980
|
+
if isinstance(ev.value, int) and ev.value != self._review_page:
|
|
1981
|
+
self._review_page = ev.value
|
|
1982
|
+
self._draw_reviews()
|
|
1983
|
+
self._emit(
|
|
1984
|
+
CardEvent(
|
|
1985
|
+
name="page_change",
|
|
1986
|
+
sender=self,
|
|
1987
|
+
window=self._window,
|
|
1988
|
+
action="page",
|
|
1989
|
+
value=ev.value,
|
|
1990
|
+
)
|
|
1991
|
+
)
|
|
1992
|
+
|
|
1993
|
+
|
|
1994
|
+
# =============================================================== DashboardGrid
|
|
1995
|
+
_DASH_VARIANTS = ("asymmetrical", "lead", "subgrid", "actions")
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
@dataclass(frozen=True)
|
|
1999
|
+
class GridAction:
|
|
2000
|
+
"""One tile of ``DashboardGrid(variant="actions")``; clicking it emits
|
|
2001
|
+
``action`` with ``key=id``."""
|
|
2002
|
+
|
|
2003
|
+
id: str
|
|
2004
|
+
label: str
|
|
2005
|
+
icon: str | None = None
|
|
2006
|
+
badge: str | None = None
|
|
2007
|
+
color: str | None = None
|
|
2008
|
+
|
|
2009
|
+
def __post_init__(self) -> None:
|
|
2010
|
+
if not re.fullmatch(r"[a-z][a-z0-9_\-.]{0,63}", self.id):
|
|
2011
|
+
raise ValidationError("GridAction.id: lower-case name (a-z, 0-9, _ - .)")
|
|
2012
|
+
_text("GridAction.label", self.label, limit=100)
|
|
2013
|
+
|
|
2014
|
+
|
|
2015
|
+
class DashboardGrid(_Emits, Box):
|
|
2016
|
+
"""Responsive grid of dashboard tiles; the tiles are arbitrary components.
|
|
2017
|
+
|
|
2018
|
+
* ``asymmetrical``: 12-column grid, spans cycle through ``spans``
|
|
2019
|
+
(default 4, 8, 8, 4).
|
|
2020
|
+
* ``lead``: the first tile beside the other tiles in a 2-column grid.
|
|
2021
|
+
* ``subgrid``: a tall lead tile beside the other tiles stacked in rows.
|
|
2022
|
+
* ``actions``: ``actions=`` icon tiles (label, badge); a click emits
|
|
2023
|
+
``action`` (:class:`CardEvent`, ``key`` is the tile id).
|
|
2024
|
+
|
|
2025
|
+
``add_tile`` appends later; ``tiles`` lists the content components."""
|
|
2026
|
+
|
|
2027
|
+
EVENTS: ClassVar[Mapping[str, EventSpec]] = _specs("action", cls="CardEvent")
|
|
2028
|
+
|
|
2029
|
+
def __init__(
|
|
2030
|
+
self,
|
|
2031
|
+
tiles: Sequence[Component] = (),
|
|
2032
|
+
*,
|
|
2033
|
+
variant: Literal["asymmetrical", "lead", "subgrid", "actions"] = "asymmetrical",
|
|
2034
|
+
actions: Sequence[GridAction] = (),
|
|
2035
|
+
spans: Sequence[int] = (4, 8, 8, 4),
|
|
2036
|
+
lead_span: int = 4,
|
|
2037
|
+
cols: int = 3,
|
|
2038
|
+
gap: str = "md",
|
|
2039
|
+
on_action: Handler[CardEvent] | None = None,
|
|
2040
|
+
visible: bool | Unset = UNSET,
|
|
2041
|
+
classes: str | Sequence[str] | Unset = UNSET,
|
|
2042
|
+
styles: Mapping[str, str | int | float] | Unset = UNSET,
|
|
2043
|
+
aria_label: str | Unset = UNSET,
|
|
2044
|
+
) -> None:
|
|
2045
|
+
_choice("DashboardGrid.variant", variant, _DASH_VARIANTS)
|
|
2046
|
+
if not spans or not all(isinstance(s, int) and 1 <= s <= 12 for s in spans): # pyright: ignore[reportUnnecessaryIsInstance]
|
|
2047
|
+
raise ValidationError("spans: integers 1..12")
|
|
2048
|
+
if not 1 <= lead_span <= 11:
|
|
2049
|
+
raise ValidationError("lead_span: 1..11")
|
|
2050
|
+
if variant == "actions" and tiles:
|
|
2051
|
+
raise ValidationError("DashboardGrid(variant='actions') takes actions=, not tiles=")
|
|
2052
|
+
if variant != "actions" and actions:
|
|
2053
|
+
raise ValidationError("actions= belongs to variant='actions'")
|
|
2054
|
+
super().__init__(visible=visible, classes=classes, styles=styles, aria_label=aria_label)
|
|
2055
|
+
self._variant = variant
|
|
2056
|
+
self._spans = list(spans)
|
|
2057
|
+
self._tiles: list[Component] = []
|
|
2058
|
+
self._actions: list[GridAction] = []
|
|
2059
|
+
self.layout: Container
|
|
2060
|
+
self.rest: Component | None = None
|
|
2061
|
+
self.lead: GridColumn | None = None
|
|
2062
|
+
with self:
|
|
2063
|
+
if variant == "actions":
|
|
2064
|
+
self.layout = SimpleGrid(cols=cols, spacing=gap)
|
|
2065
|
+
else:
|
|
2066
|
+
self.layout = Grid(gap=gap)
|
|
2067
|
+
if variant in ("lead", "subgrid"):
|
|
2068
|
+
with self.layout:
|
|
2069
|
+
self.lead = GridColumn(span=lead_span)
|
|
2070
|
+
rest_col = GridColumn(span=12 - lead_span)
|
|
2071
|
+
with rest_col:
|
|
2072
|
+
self.rest = (
|
|
2073
|
+
SimpleGrid(cols=2, spacing=gap) if variant == "lead" else Stack(gap=gap)
|
|
2074
|
+
)
|
|
2075
|
+
for t in _list("tiles", tiles):
|
|
2076
|
+
self.add_tile(t)
|
|
2077
|
+
for a in _list("actions", actions):
|
|
2078
|
+
self.add_action(a)
|
|
2079
|
+
if on_action is not None:
|
|
2080
|
+
self.on("action", on_action)
|
|
2081
|
+
|
|
2082
|
+
@property
|
|
2083
|
+
def variant(self) -> str:
|
|
2084
|
+
return self._variant
|
|
2085
|
+
|
|
2086
|
+
@property
|
|
2087
|
+
def tiles(self) -> tuple[Component, ...]:
|
|
2088
|
+
return tuple(self._tiles)
|
|
2089
|
+
|
|
2090
|
+
@property
|
|
2091
|
+
def actions(self) -> tuple[GridAction, ...]:
|
|
2092
|
+
return tuple(self._actions)
|
|
2093
|
+
|
|
2094
|
+
def add_tile(self, tile: Component, *, span: int | None = None) -> Component:
|
|
2095
|
+
"""Move ``tile`` into the grid (it may already sit in the surrounding
|
|
2096
|
+
``with`` block); ``span`` (1..12) overrides the cycle for ``asymmetrical``."""
|
|
2097
|
+
if self._variant == "actions":
|
|
2098
|
+
raise ValidationError("add_action() adds tiles to an actions grid")
|
|
2099
|
+
if span is not None and not 1 <= span <= 12:
|
|
2100
|
+
raise ValidationError("span: 1..12")
|
|
2101
|
+
n = len(self._tiles)
|
|
2102
|
+
if self._variant == "asymmetrical":
|
|
2103
|
+
with self.layout:
|
|
2104
|
+
col = GridColumn(span=span or self._spans[n % len(self._spans)])
|
|
2105
|
+
tile.move_to(col)
|
|
2106
|
+
elif n == 0:
|
|
2107
|
+
assert self.lead is not None
|
|
2108
|
+
tile.move_to(self.lead)
|
|
2109
|
+
else:
|
|
2110
|
+
assert self.rest is not None
|
|
2111
|
+
tile.move_to(self.rest)
|
|
2112
|
+
self._tiles.append(tile)
|
|
2113
|
+
return tile
|
|
2114
|
+
|
|
2115
|
+
def add_action(self, action: GridAction) -> None:
|
|
2116
|
+
if self._variant != "actions":
|
|
2117
|
+
raise ValidationError("add_action: only variant='actions'")
|
|
2118
|
+
if not isinstance(action, GridAction): # pyright: ignore[reportUnnecessaryIsInstance]
|
|
2119
|
+
raise ValidationError("expected a GridAction")
|
|
2120
|
+
if any(a.id == action.id for a in self._actions):
|
|
2121
|
+
raise ValidationError(f"duplicate action {action.id!r}")
|
|
2122
|
+
with self.layout:
|
|
2123
|
+
with UnstyledButton(aria_label=action.label, on_click=self._opener(action.id)):
|
|
2124
|
+
with Card(with_border=True, padding="md", radius="md"):
|
|
2125
|
+
with Stack(gap=4, align="center"):
|
|
2126
|
+
if action.icon:
|
|
2127
|
+
with ThemeIcon(
|
|
2128
|
+
size=40, radius="md", variant="light", color=action.color or UNSET
|
|
2129
|
+
):
|
|
2130
|
+
_icon(action.icon, size=24)
|
|
2131
|
+
Text(action.label, size="sm", fw=500)
|
|
2132
|
+
if action.badge:
|
|
2133
|
+
Badge(action.badge, size="xs")
|
|
2134
|
+
self._actions.append(action)
|
|
2135
|
+
|
|
2136
|
+
def _opener(self, key: str) -> Callable[[], None]:
|
|
2137
|
+
return lambda: self._act(key)
|
|
2138
|
+
|
|
2139
|
+
@staticmethod
|
|
2140
|
+
def placeholders(n: int, height: int = 120) -> list[Component]:
|
|
2141
|
+
"""``n`` skeleton tiles (loading state of a dashboard)."""
|
|
2142
|
+
return [Skeleton(height=height, radius="md") for _ in range(n)]
|
|
2143
|
+
|
|
2144
|
+
def _act(self, key: str) -> None:
|
|
2145
|
+
self._emit(
|
|
2146
|
+
CardEvent(name="action", sender=self, window=self._window, action="open", key=key)
|
|
2147
|
+
)
|
|
2148
|
+
|
|
2149
|
+
|
|
2150
|
+
# ==================================================================== EmptyState
|
|
2151
|
+
_EMPTY_KINDS: dict[str, tuple[str, str]] = {
|
|
2152
|
+
"empty": ("inbox", "gray"),
|
|
2153
|
+
"error": ("alert-circle", "red"),
|
|
2154
|
+
"loading": ("loader", "blue"),
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
|
|
2158
|
+
class EmptyState(_Emits, _EmptyState):
|
|
2159
|
+
"""Empty, error or loading placeholder with an optional action.
|
|
2160
|
+
|
|
2161
|
+
``kind``: ``empty`` (default), ``error`` (red alert icon) or ``loading``
|
|
2162
|
+
(adds a loader and disables the action). ``set_kind`` switches in place;
|
|
2163
|
+
the action button emits ``action`` (:class:`CardEvent`)."""
|
|
2164
|
+
|
|
2165
|
+
EVENTS: ClassVar[Mapping[str, EventSpec]] = _specs("action", cls="CardEvent")
|
|
2166
|
+
|
|
2167
|
+
def __init__(
|
|
2168
|
+
self,
|
|
2169
|
+
title: str,
|
|
2170
|
+
*,
|
|
2171
|
+
description: str | None = None,
|
|
2172
|
+
kind: Literal["empty", "error", "loading"] = "empty",
|
|
2173
|
+
icon: str | None = None,
|
|
2174
|
+
action_label: str | None = None,
|
|
2175
|
+
on_action: Handler[CardEvent] | None = None,
|
|
2176
|
+
visible: bool | Unset = UNSET,
|
|
2177
|
+
classes: str | Sequence[str] | Unset = UNSET,
|
|
2178
|
+
styles: Mapping[str, str | int | float] | Unset = UNSET,
|
|
2179
|
+
aria_label: str | Unset = UNSET,
|
|
2180
|
+
) -> None:
|
|
2181
|
+
_choice("EmptyState.kind", kind, tuple(_EMPTY_KINDS))
|
|
2182
|
+
glyph, colour = _EMPTY_KINDS[kind]
|
|
2183
|
+
button = (
|
|
2184
|
+
Button(action_label, variant="light", disabled=kind == "loading", on_click=self._act)
|
|
2185
|
+
if action_label
|
|
2186
|
+
else None
|
|
2187
|
+
)
|
|
2188
|
+
super().__init__(
|
|
2189
|
+
title=_text("title", title, limit=200),
|
|
2190
|
+
description=description if description is not None else UNSET,
|
|
2191
|
+
icon=icon or glyph,
|
|
2192
|
+
color=colour,
|
|
2193
|
+
actions=button,
|
|
2194
|
+
visible=visible,
|
|
2195
|
+
classes=classes,
|
|
2196
|
+
styles=styles,
|
|
2197
|
+
aria_label=aria_label,
|
|
2198
|
+
)
|
|
2199
|
+
self._kind: str = kind
|
|
2200
|
+
self._custom_icon = icon
|
|
2201
|
+
self.action_button = button
|
|
2202
|
+
self.loader: Loader | None = None
|
|
2203
|
+
if kind == "loading":
|
|
2204
|
+
with self:
|
|
2205
|
+
self.loader = Loader(size="sm")
|
|
2206
|
+
if on_action is not None:
|
|
2207
|
+
self.on("action", on_action)
|
|
2208
|
+
|
|
2209
|
+
@property
|
|
2210
|
+
def kind(self) -> str:
|
|
2211
|
+
return self._kind
|
|
2212
|
+
|
|
2213
|
+
def set_kind(
|
|
2214
|
+
self,
|
|
2215
|
+
kind: Literal["empty", "error", "loading"],
|
|
2216
|
+
*,
|
|
2217
|
+
title: str | None = None,
|
|
2218
|
+
description: str | None = None,
|
|
2219
|
+
) -> None:
|
|
2220
|
+
_choice("EmptyState.kind", kind, tuple(_EMPTY_KINDS))
|
|
2221
|
+
glyph, colour = _EMPTY_KINDS[kind]
|
|
2222
|
+
props: dict[str, Any] = {"icon": self._custom_icon or glyph, "color": colour}
|
|
2223
|
+
if title is not None:
|
|
2224
|
+
props["title"] = _text("title", title, limit=200)
|
|
2225
|
+
if description is not None:
|
|
2226
|
+
props["description"] = description
|
|
2227
|
+
self.update(**props)
|
|
2228
|
+
if self.loader is not None and kind != "loading":
|
|
2229
|
+
self.loader.remove()
|
|
2230
|
+
self.loader = None
|
|
2231
|
+
elif self.loader is None and kind == "loading":
|
|
2232
|
+
with self:
|
|
2233
|
+
self.loader = Loader(size="sm")
|
|
2234
|
+
if self.action_button is not None:
|
|
2235
|
+
self.action_button.update(disabled=kind == "loading")
|
|
2236
|
+
self._kind = kind
|
|
2237
|
+
|
|
2238
|
+
def _act(self) -> None:
|
|
2239
|
+
self._emit(CardEvent(name="action", sender=self, window=self._window, action="primary"))
|