Skip to content

Commit 81de95e

Browse files
feature: recompute variable directory (#2383)
* recompute variable directory * add test for var dir recompute
1 parent 115b2e8 commit 81de95e

File tree

3 files changed

+91
-11
lines changed

3 files changed

+91
-11
lines changed

taipy/gui/utils/_variable_directory.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def __init__(self, locals_context: _LocalsContext):
2525
self._var_dir: t.Dict[str, t.Dict] = {}
2626
self._var_head: t.Dict[str, t.List[t.Tuple[str, str]]] = {}
2727
self._imported_var_dir: t.Dict[str, t.List[t.Tuple[str, str, str]]] = {}
28+
self._pre_processed_module: t.Set[str] = set()
29+
self._processed_module: t.Set[str] = set()
2830

2931
def set_default(self, frame: FrameType) -> None:
3032
self._default_module = _get_module_name_from_frame(frame)
@@ -39,7 +41,10 @@ def add_frame(self, frame: t.Optional[FrameType]) -> None:
3941
self._imported_var_dir[t.cast(str, module_name)] = imported_var_list
4042

4143
def pre_process_module_import_all(self) -> None:
42-
for imported_dir in self._imported_var_dir.values():
44+
for base_module, imported_dir in self._imported_var_dir.items():
45+
# Skip pre process for modules that have been processed
46+
if base_module in self._pre_processed_module:
47+
continue
4348
additional_var_list: t.List[t.Tuple[str, str, str]] = []
4449
for name, asname, module in imported_dir:
4550
if name != "*" or asname != "*":
@@ -51,21 +56,28 @@ def pre_process_module_import_all(self) -> None:
5156
(v, v, module) for v in self._locals_context.get_locals().keys() if not v.startswith("_")
5257
)
5358
imported_dir.extend(additional_var_list)
59+
# Save the pre-processed module
60+
self._pre_processed_module.add(base_module)
5461

5562
def process_imported_var(self) -> None:
5663
self.pre_process_module_import_all()
57-
default_imported_dir = self._imported_var_dir[t.cast(str, self._default_module)]
58-
with self._locals_context.set_locals_context(self._default_module):
59-
for name, asname, module in default_imported_dir:
60-
if name == "*" and asname == "*":
61-
continue
62-
imported_module_name = _get_module_name_from_imported_var(
63-
name, self._locals_context.get_locals().get(asname, None), module
64-
)
65-
temp_var_name = self.add_var(asname, self._default_module)
66-
self.add_var(name, imported_module_name, temp_var_name)
64+
default_module = t.cast(str, self._default_module)
65+
if default_module not in self._processed_module:
66+
default_imported_dir = self._imported_var_dir[default_module]
67+
with self._locals_context.set_locals_context(self._default_module):
68+
for name, asname, module in default_imported_dir:
69+
if name == "*" and asname == "*":
70+
continue
71+
imported_module_name = _get_module_name_from_imported_var(
72+
name, self._locals_context.get_locals().get(asname, None), module
73+
)
74+
temp_var_name = self.add_var(asname, self._default_module)
75+
self.add_var(name, imported_module_name, temp_var_name)
76+
self._processed_module.add(default_module)
6777

6878
for k, v in self._imported_var_dir.items():
79+
if k in self._processed_module:
80+
continue
6981
with self._locals_context.set_locals_context(k):
7082
for name, asname, module in v:
7183
if name == "*" and asname == "*":
@@ -82,6 +94,7 @@ def process_imported_var(self) -> None:
8294
self.add_var(asname, k, var_name)
8395
else:
8496
self.add_var(name, imported_module_name, var_asname)
97+
self._processed_module.add(k)
8598

8699
def add_var(self, name: str, module: t.Optional[str], var_name: t.Optional[str] = None) -> str:
87100
if module is None:
@@ -119,9 +132,11 @@ def get_var(self, name: str, module: str) -> t.Optional[str]:
119132
_MODULE_ID = "_TPMDL_"
120133
_RE_TPMDL_DECODE = re.compile(r"(.*?)" + _MODULE_ID + r"(\d+)$")
121134

135+
122136
def _is_moduled_variable(var_name: str):
123137
return _MODULE_ID in var_name
124138

139+
125140
def _variable_encode(var_name: str, module_name: t.Optional[str]):
126141
if module_name is None:
127142
return var_name
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2021-2025 Avaiga Private Limited
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
# the License. You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
# specific language governing permissions and limitations under the License.
11+
12+
from taipy.gui import Markdown
13+
14+
b = 20
15+
16+
17+
def get_b(state):
18+
return state.b
19+
20+
21+
def set_b(state, val):
22+
state.b = val
23+
24+
25+
md_page2 = Markdown(
26+
"""
27+
<|{b}|>
28+
"""
29+
)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2021-2025 Avaiga Private Limited
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
# the License. You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
# specific language governing permissions and limitations under the License.
11+
12+
13+
from taipy.gui import Gui
14+
from taipy.gui.utils import _LocalsContext, _VariableDirectory
15+
16+
from .state_asset.page1 import md_page1
17+
from .state_asset.page2 import md_page2
18+
19+
20+
def test_variable_directory_dyanmic_process(gui: Gui):
21+
gui.run(run_server=False)
22+
with gui.get_flask_app().app_context():
23+
locals_context = _LocalsContext()
24+
variable_directory = _VariableDirectory(locals_context)
25+
page1_module = str(md_page1._get_module_name())
26+
page2_module = str(md_page2._get_module_name())
27+
locals_context.add(page1_module, md_page1._get_locals())
28+
variable_directory.set_default(md_page1._get_frame()) # type: ignore
29+
variable_directory.process_imported_var()
30+
assert page1_module in variable_directory._pre_processed_module
31+
assert page1_module in variable_directory._processed_module
32+
locals_context.add(page2_module, md_page2._get_locals())
33+
variable_directory.add_frame(md_page2._get_frame())
34+
variable_directory.process_imported_var()
35+
assert page2_module in variable_directory._pre_processed_module
36+
assert page2_module in variable_directory._processed_module

0 commit comments

Comments
 (0)