fletext 0.1.0__tar.gz
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.
- fletext-0.1.0/.gitignore +207 -0
- fletext-0.1.0/LICENSE +21 -0
- fletext-0.1.0/PKG-INFO +15 -0
- fletext-0.1.0/README.md +1 -0
- fletext-0.1.0/pyproject.toml +27 -0
- fletext-0.1.0/src/fletext/__init__.py +5 -0
- fletext-0.1.0/src/fletext/dvalue.py +94 -0
- fletext-0.1.0/src/fletext/pubsub.py +17 -0
- fletext-0.1.0/src/fletext/ui.py +254 -0
fletext-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
fletext-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stone Zhong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
fletext-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fletext
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Utility to build flet based UI application
|
|
5
|
+
Project-URL: Homepage, https://github.com/pypa/sampleproject
|
|
6
|
+
Project-URL: Issues, https://github.com/pypa/sampleproject/issues
|
|
7
|
+
Author-email: Stone Zhong <stone.zhong@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.13
|
|
12
|
+
Requires-Dist: flet
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
Wraps flet UI component and make it easier to build flext based UI app.
|
fletext-0.1.0/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Wraps flet UI component and make it easier to build flext based UI app.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling >= 1.26"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "fletext"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
authors = [
|
|
9
|
+
{name="Stone Zhong", email="stone.zhong@gmail.com"}
|
|
10
|
+
]
|
|
11
|
+
description = "Utility to build flet based UI application"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.13"
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
license = "MIT"
|
|
19
|
+
license-files = ["LICENCSE"]
|
|
20
|
+
|
|
21
|
+
dependencies = [
|
|
22
|
+
"flet"
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/pypa/sampleproject"
|
|
27
|
+
Issues = "https://github.com/pypa/sampleproject/issues"
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from typing import Callable, Any, Dict, Optional
|
|
3
|
+
from abc import ABC
|
|
4
|
+
from .pubsub import PubSub
|
|
5
|
+
from uuid import uuid4
|
|
6
|
+
|
|
7
|
+
class VarEnvironment:
|
|
8
|
+
variable_map: Dict[str, BindVariable]
|
|
9
|
+
parent_env: Optional[VarEnvironment]
|
|
10
|
+
pubsub: Optional[PubSub] = None
|
|
11
|
+
name:str
|
|
12
|
+
|
|
13
|
+
def __init__(
|
|
14
|
+
self,
|
|
15
|
+
parent_env = None,
|
|
16
|
+
variable_map:Dict[str, BindVariable]={},
|
|
17
|
+
pubsub:Optional[PubSub]=None,
|
|
18
|
+
name:str="default"
|
|
19
|
+
):
|
|
20
|
+
self.parent_env = parent_env
|
|
21
|
+
self.variable_map = variable_map.copy()
|
|
22
|
+
self.pubsub = pubsub
|
|
23
|
+
self.name = name
|
|
24
|
+
|
|
25
|
+
def get_value(self, name:str):
|
|
26
|
+
if name in self.variable_map:
|
|
27
|
+
var = self.variable_map[name]
|
|
28
|
+
return var.get_value() if isinstance(var, BindVariable) else var
|
|
29
|
+
return self.parent_env.get_value(name) if self.parent_env is not None else None
|
|
30
|
+
|
|
31
|
+
def __getattr__(self, name:str)->BindVariable:
|
|
32
|
+
return self.get_value(name)
|
|
33
|
+
|
|
34
|
+
def new_settable(self, value:Any=None, name:Optional[str]=None):
|
|
35
|
+
if name is None:
|
|
36
|
+
name = str(uuid4())
|
|
37
|
+
ret = SettableVariable(self, name, value)
|
|
38
|
+
self.variable_map[name] = ret
|
|
39
|
+
return ret
|
|
40
|
+
|
|
41
|
+
def new_calculable(self, calculator:Callable[[], Any], **dependOn):
|
|
42
|
+
return CalculableVariable(self, calculator, **dependOn)
|
|
43
|
+
|
|
44
|
+
# 将一个SettableVariable变量发布,这样其他人可以通过发布消息来设置这个变量的值
|
|
45
|
+
def publish_settable(self, name:str):
|
|
46
|
+
if name not in self.variable_map:
|
|
47
|
+
raise ValueError(f"There is no variable for \"{name}\"")
|
|
48
|
+
var = self.variable_map[name]
|
|
49
|
+
if not isinstance(var, SettableVariable):
|
|
50
|
+
raise ValueError(f"Variable \"{name}\" is not settable")
|
|
51
|
+
self.pubsub.subscribe(f"env.{self.name}.{name}", lambda new_value: var.set_value(new_value))
|
|
52
|
+
|
|
53
|
+
class BindVariable(ABC):
|
|
54
|
+
env: VarEnvironment # 这个变量对应的环境
|
|
55
|
+
name: str # 这个变量在环境中的名字
|
|
56
|
+
|
|
57
|
+
def __init__(self, env:VarEnvironment, name:str):
|
|
58
|
+
self.env = env
|
|
59
|
+
self.name = name
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# 一个可以设置的变量,它不依赖于其他变量
|
|
63
|
+
class SettableVariable(BindVariable):
|
|
64
|
+
_value: Any
|
|
65
|
+
|
|
66
|
+
def __init__(self, env:VarEnvironment, name:str, value:Any=None):
|
|
67
|
+
super().__init__(env, name=name)
|
|
68
|
+
self._value = value
|
|
69
|
+
|
|
70
|
+
def get_value(self):
|
|
71
|
+
return self._value
|
|
72
|
+
|
|
73
|
+
def set_value(self, value:Any=None):
|
|
74
|
+
self._value = value
|
|
75
|
+
|
|
76
|
+
def publish(self) -> SettableVariable:
|
|
77
|
+
if self.env.pubsub is None:
|
|
78
|
+
raise ValueError("The environment is not bind to a message bus")
|
|
79
|
+
print(f"subscribe: env.{self.env.name}.{self.name}")
|
|
80
|
+
self.env.pubsub.subscribe(f"env.{self.env.name}.{self.name}", lambda new_value: self.set_value(new_value))
|
|
81
|
+
return self
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# 一个由计算得到的变量
|
|
85
|
+
class CalculableVariable(BindVariable):
|
|
86
|
+
calculator: Callable[[], Any]
|
|
87
|
+
|
|
88
|
+
def __init__(self, env:VarEnvironment, calculator:Callable[[], Any], name:Optional[str]=None):
|
|
89
|
+
super().__init__(env, name=name)
|
|
90
|
+
self.calculator = calculator
|
|
91
|
+
|
|
92
|
+
def get_value(self):
|
|
93
|
+
return self.calculator(self.env)
|
|
94
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from typing import List, Callable, Dict, Any
|
|
2
|
+
from collections import defaultdict
|
|
3
|
+
|
|
4
|
+
#########################################################################################
|
|
5
|
+
# 这个模块负责消息的订阅和消息的发送
|
|
6
|
+
#########################################################################################
|
|
7
|
+
class PubSub:
|
|
8
|
+
callback_registry:Dict[str, List[Callable[[], None]]] = defaultdict(list)
|
|
9
|
+
|
|
10
|
+
# 订阅消息,一旦一个消息被发布,所有的订阅者登记注册的方法就会被处罚
|
|
11
|
+
def subscribe(self, subject_id:str, callback: Callable[[], None]):
|
|
12
|
+
self.callback_registry[subject_id].append(callback)
|
|
13
|
+
|
|
14
|
+
# 发布消息
|
|
15
|
+
def publish(self, subject_id, message:Any):
|
|
16
|
+
for callback in self.callback_registry.get(subject_id, []):
|
|
17
|
+
callback(message)
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
import flet as ft
|
|
4
|
+
import sys
|
|
5
|
+
from copy import copy
|
|
6
|
+
from typing import Any, List, Optional, get_type_hints, Callable
|
|
7
|
+
from .dvalue import VarEnvironment
|
|
8
|
+
from enum import Enum
|
|
9
|
+
|
|
10
|
+
#########################################################################################
|
|
11
|
+
# 概念
|
|
12
|
+
# - 组件描述符
|
|
13
|
+
# 是一种JSON数据,描述组件。$type字段表示组件的类型。$refid表示组件的代号
|
|
14
|
+
# 将来,你可以通过组件代号来寻找组件。
|
|
15
|
+
#
|
|
16
|
+
# 在描述符中如果有属性的值是对象,且有"$type"字段,则视为一个组件的描述符。
|
|
17
|
+
#
|
|
18
|
+
# 要从UI Descriptor加载UI组件,客户端要调用函数 load_component_from_descriptor
|
|
19
|
+
#
|
|
20
|
+
# 组件的ui property
|
|
21
|
+
# - 只有在访问时才计算
|
|
22
|
+
# - 计算的时候通过build_ui()函数获得。一旦获得,就不会再次计算了。
|
|
23
|
+
#
|
|
24
|
+
# 组件和变量的绑定, 绑定类型: 输出型。通过函数 bind_variable(variable_name, bind_type)
|
|
25
|
+
# 这时,这个组件的值会被传送到其绑定的变量。当组件的值变化了,这个变量的值也会变化
|
|
26
|
+
# 组件和变量的绑定, 绑定类型: 输入型
|
|
27
|
+
# 这时,这个组件的值会从被绑定的变量中读取,如果被绑定的变量的值变化了,组件的值也会变化
|
|
28
|
+
#########################################################################################
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
##########################################################
|
|
32
|
+
# This module helps to build flet UI based on YAML
|
|
33
|
+
# descriptor.
|
|
34
|
+
##########################################################
|
|
35
|
+
|
|
36
|
+
TYPE_FIELD = "$type"
|
|
37
|
+
REFID_FIELD = "$refid"
|
|
38
|
+
|
|
39
|
+
class BindType(Enum):
|
|
40
|
+
INPUT = 1
|
|
41
|
+
EXPORT = 2
|
|
42
|
+
|
|
43
|
+
DPC_QUEUE = []
|
|
44
|
+
|
|
45
|
+
def queue_method(method: Callable[[], None]):
|
|
46
|
+
DPC_QUEUE.append(method)
|
|
47
|
+
|
|
48
|
+
def drain_dpc_queue():
|
|
49
|
+
while len(DPC_QUEUE) > 0:
|
|
50
|
+
method = DPC_QUEUE.pop()
|
|
51
|
+
method()
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class ValueRef:
|
|
55
|
+
value: Any = None
|
|
56
|
+
|
|
57
|
+
# 解析yaml中一个字段的值
|
|
58
|
+
def resolve(value, env:VarEnvironment, refs:dict, context:dict={}):
|
|
59
|
+
if isinstance(value, list):
|
|
60
|
+
return [resolve(item, env, refs, context=context) for item in value]
|
|
61
|
+
|
|
62
|
+
if isinstance(value, dict) and value.get(TYPE_FIELD) is not None:
|
|
63
|
+
# 如果值是对象且有$type字段,则被视为一个子组件的描述符
|
|
64
|
+
return load_component_from_descriptor(env, value, refs=refs, context=context)
|
|
65
|
+
|
|
66
|
+
if not isinstance(value, str):
|
|
67
|
+
return value
|
|
68
|
+
|
|
69
|
+
# check if we are using context variable
|
|
70
|
+
if value.startswith("❔"):
|
|
71
|
+
return context.get(value[1:])
|
|
72
|
+
|
|
73
|
+
# 这是一个变量
|
|
74
|
+
if value.startswith("❕"):
|
|
75
|
+
path = value[1:]
|
|
76
|
+
current_obj = sys.modules[__name__]
|
|
77
|
+
for seg_name in path.split("."):
|
|
78
|
+
current_obj = getattr(current_obj, seg_name)
|
|
79
|
+
return current_obj
|
|
80
|
+
|
|
81
|
+
# 其他情况,假设就是字符串
|
|
82
|
+
return value
|
|
83
|
+
|
|
84
|
+
# 从descriptor加载一个组件
|
|
85
|
+
def load_component_from_descriptor(
|
|
86
|
+
env: VarEnvironment,
|
|
87
|
+
descriptor:dict,
|
|
88
|
+
refs:Optional[dict] = None,
|
|
89
|
+
context={},
|
|
90
|
+
) -> Component:
|
|
91
|
+
if TYPE_FIELD not in descriptor:
|
|
92
|
+
raise ValueError(f"{TYPE_FIELD} not found in desscriptor")
|
|
93
|
+
|
|
94
|
+
component_type = descriptor[TYPE_FIELD]
|
|
95
|
+
if component_type not in COMPONENT_MAP:
|
|
96
|
+
raise Exception(f"{component_type} is not valid UI Component type")
|
|
97
|
+
|
|
98
|
+
component = COMPONENT_MAP[component_type]()
|
|
99
|
+
component._env = env
|
|
100
|
+
if refs is None:
|
|
101
|
+
component._refs = {}
|
|
102
|
+
new_refs = component._refs
|
|
103
|
+
else:
|
|
104
|
+
new_refs = refs
|
|
105
|
+
|
|
106
|
+
component.initialize_component_from_descriptor(descriptor, new_refs, context=context)
|
|
107
|
+
return component
|
|
108
|
+
|
|
109
|
+
#################################################################
|
|
110
|
+
# 所有组件类的共同祖先
|
|
111
|
+
# ui: 这是指向flet ui组件。这是一个property,并且是lazy计算的。
|
|
112
|
+
# 它只计算一次,并且只有在第一次访问的时候才出发计算
|
|
113
|
+
#################################################################
|
|
114
|
+
@dataclass
|
|
115
|
+
class Component:
|
|
116
|
+
_refs: Optional[dict] = None
|
|
117
|
+
_ui: Optional[ft.Control] = None
|
|
118
|
+
_env: Optional[VarEnvironment] = None
|
|
119
|
+
|
|
120
|
+
def get_child(self, refid:str)->Optional[Component]:
|
|
121
|
+
if self._refs is None:
|
|
122
|
+
return None
|
|
123
|
+
return self._refs.get(refid)
|
|
124
|
+
|
|
125
|
+
@property
|
|
126
|
+
def ui(self):
|
|
127
|
+
if self._ui is not None:
|
|
128
|
+
return self._ui
|
|
129
|
+
self._ui = self.build_ui()
|
|
130
|
+
return self._ui
|
|
131
|
+
|
|
132
|
+
# def bind_variable(self, variable_name:str, bind_type:BindType):
|
|
133
|
+
# if bind_type == BindType.INPUT:
|
|
134
|
+
# # 输入型绑定
|
|
135
|
+
# # 组件的值从绑定的变量获得
|
|
136
|
+
|
|
137
|
+
# def on_value_changed():
|
|
138
|
+
# pass
|
|
139
|
+
|
|
140
|
+
# self._env.pubsub.subscribe(
|
|
141
|
+
# f"env.{self._env.name}.{variable_name}", lambda new_value: self.set_value(new_value))
|
|
142
|
+
|
|
143
|
+
# pass
|
|
144
|
+
# elif bind_type == BindType.EXPORT:
|
|
145
|
+
# # 输出型绑定
|
|
146
|
+
# pass
|
|
147
|
+
# else:
|
|
148
|
+
# assert False, "非法绑定类型"
|
|
149
|
+
|
|
150
|
+
def get_attr_value_for_building_ui(self, property_value:Any):
|
|
151
|
+
if isinstance(property_value, Component):
|
|
152
|
+
return property_value.ui
|
|
153
|
+
if isinstance(property_value, list):
|
|
154
|
+
return [self.get_attr_value_for_building_ui(item) for item in property_value]
|
|
155
|
+
if isinstance(property_value, dict):
|
|
156
|
+
return {
|
|
157
|
+
attr_name: self.get_attr_value_for_building_ui(attr_value) \
|
|
158
|
+
for attr_name, attr_value in property_value.items() \
|
|
159
|
+
if not attr_name.startswith("_") and attr_value is not None
|
|
160
|
+
# attr_value应该是ValueRef类型的。
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return property_value.value if isinstance(property_value, ValueRef) else property_value
|
|
164
|
+
|
|
165
|
+
def build_ui(self) -> ft.Control:
|
|
166
|
+
kwargs = self.get_attr_value_for_building_ui(vars(self))
|
|
167
|
+
ui_class = getattr(ft, type(self).__name__)
|
|
168
|
+
return ui_class(**kwargs)
|
|
169
|
+
|
|
170
|
+
# 构造Component及其子类的时候,需要从JSON Descriptor中加载Component
|
|
171
|
+
def initialize_component_from_descriptor(self, descriptor:dict, refs:dict, context={}):
|
|
172
|
+
# 处理refid
|
|
173
|
+
refid = descriptor.get(REFID_FIELD)
|
|
174
|
+
if refid is not None:
|
|
175
|
+
refs[refid] = self
|
|
176
|
+
|
|
177
|
+
for field_name, field_value in descriptor.items():
|
|
178
|
+
if field_name in (TYPE_FIELD, REFID_FIELD):
|
|
179
|
+
continue
|
|
180
|
+
setattr(self, field_name, resolve(field_value, self._env, refs, context=context))
|
|
181
|
+
|
|
182
|
+
@dataclass
|
|
183
|
+
class Text(Component):
|
|
184
|
+
# see https://flet.dev/docs/controls/text
|
|
185
|
+
value: Optional[ValueRef] = None
|
|
186
|
+
size: Optional[ValueRef] = None
|
|
187
|
+
weight: Optional[ValueRef] = None
|
|
188
|
+
font_family: Optional[ValueRef] = None
|
|
189
|
+
selectable: Optional[ValueRef] = None
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
@dataclass
|
|
193
|
+
class Column(Component):
|
|
194
|
+
controls: List[Component] = field(default_factory=list)
|
|
195
|
+
horizontal_alignment: Optional[ValueRef] = None
|
|
196
|
+
spacing: Optional[ValueRef] = None
|
|
197
|
+
width: Optional[ValueRef] = None
|
|
198
|
+
expand: Optional[ValueRef] = None
|
|
199
|
+
tight: Optional[ValueRef] = None
|
|
200
|
+
|
|
201
|
+
@dataclass
|
|
202
|
+
class Row(Component):
|
|
203
|
+
controls: List[Component] = field(default_factory=list)
|
|
204
|
+
alignment: Optional[ValueRef] = None
|
|
205
|
+
spacing: Optional[ValueRef] = None
|
|
206
|
+
expand: Optional[ValueRef] = None
|
|
207
|
+
|
|
208
|
+
@dataclass
|
|
209
|
+
class Container(Component):
|
|
210
|
+
content: Optional[Component] = None
|
|
211
|
+
bgcolor: Optional[ValueRef] = None
|
|
212
|
+
padding: Optional[ValueRef] = None
|
|
213
|
+
border: Optional[ValueRef] = None
|
|
214
|
+
expand: Optional[ValueRef] = None
|
|
215
|
+
height: Optional[ValueRef] = None
|
|
216
|
+
width: Optional[ValueRef] = None
|
|
217
|
+
alignment: Optional[ValueRef] = None
|
|
218
|
+
border_radius: Optional[ValueRef] = None
|
|
219
|
+
|
|
220
|
+
@dataclass
|
|
221
|
+
class TextField(Component):
|
|
222
|
+
value: Optional[ValueRef] = None
|
|
223
|
+
label: Optional[ValueRef] = None
|
|
224
|
+
width: Optional[ValueRef] = None
|
|
225
|
+
expand: Optional[ValueRef] = None
|
|
226
|
+
font_family: Optional[ValueRef] = None
|
|
227
|
+
|
|
228
|
+
@dataclass
|
|
229
|
+
class Button(Component):
|
|
230
|
+
disabled: Optional[ValueRef] = None
|
|
231
|
+
content: Optional[ValueRef] = None
|
|
232
|
+
|
|
233
|
+
@dataclass
|
|
234
|
+
class IconButton(Component):
|
|
235
|
+
disabled: Optional[ValueRef] = None
|
|
236
|
+
icon: Optional[ValueRef] = None
|
|
237
|
+
|
|
238
|
+
@dataclass
|
|
239
|
+
class ExpansionTile(Component):
|
|
240
|
+
title: Optional[Component] = None
|
|
241
|
+
controls: List[Component] = field(default_factory=list)
|
|
242
|
+
shape: Optional[ValueRef] = None
|
|
243
|
+
|
|
244
|
+
COMPONENT_MAP = {
|
|
245
|
+
"Text": Text,
|
|
246
|
+
"Column": Column,
|
|
247
|
+
"Row": Row,
|
|
248
|
+
"Container": Container,
|
|
249
|
+
"TextField": TextField,
|
|
250
|
+
"Button": Button,
|
|
251
|
+
"IconButton": IconButton,
|
|
252
|
+
"ExpansionTile": ExpansionTile
|
|
253
|
+
}
|
|
254
|
+
|