123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- # Copyright 2021-2024 Avaiga Private Limited
- #
- # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
- # the License. You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
- # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
- # specific language governing permissions and limitations under the License.
- import typing as t
- from datetime import datetime
- from taipy.gui import Gui, State
- from taipy.gui.extension import Element, ElementLibrary, ElementProperty, PropertyType
- from ..version import _get_version
- from ._adapters import (
- _GuiCoreDatanodeAdapter,
- _GuiCoreScenarioAdapter,
- _GuiCoreScenarioDagAdapter,
- )
- from ._context import _GuiCoreContext
- class _GuiCore(ElementLibrary):
- __LIB_NAME = "taipy_gui_core"
- __CTX_VAR_NAME = f"__{__LIB_NAME}_Ctx"
- __SCENARIO_ADAPTER = "tgc_scenario"
- __DATANODE_ADAPTER = "tgc_datanode"
- __JOB_ADAPTER = "tgc_job"
- __INNER_VARS = (
- _GuiCoreContext._SCENARIO_SELECTOR_ERROR_VAR,
- _GuiCoreContext._SCENARIO_SELECTOR_ID_VAR,
- _GuiCoreContext._SCENARIO_VIZ_ERROR_VAR,
- _GuiCoreContext._JOB_SELECTOR_ERROR_VAR,
- _GuiCoreContext._DATANODE_VIZ_ERROR_VAR,
- _GuiCoreContext._DATANODE_VIZ_OWNER_ID_VAR,
- _GuiCoreContext._DATANODE_VIZ_HISTORY_ID_VAR,
- _GuiCoreContext._DATANODE_VIZ_DATA_ID_VAR,
- _GuiCoreContext._DATANODE_VIZ_DATA_CHART_ID_VAR,
- _GuiCoreContext._DATANODE_VIZ_PROPERTIES_ID_VAR,
- )
- __elts = {
- "scenario_selector": Element(
- "value",
- {
- "id": ElementProperty(PropertyType.string),
- "show_add_button": ElementProperty(PropertyType.boolean, True),
- "display_cycles": ElementProperty(PropertyType.boolean, True),
- "show_primary_flag": ElementProperty(PropertyType.boolean, True),
- "value": ElementProperty(PropertyType.lov_value),
- "on_change": ElementProperty(PropertyType.function),
- "height": ElementProperty(PropertyType.string, "50vh"),
- "class_name": ElementProperty(PropertyType.dynamic_string),
- "show_pins": ElementProperty(PropertyType.boolean, False),
- "on_creation": ElementProperty(PropertyType.function),
- "show_dialog": ElementProperty(PropertyType.boolean, True),
- _GuiCoreContext._SEL_SCENARIOS_PROP: ElementProperty(PropertyType.dynamic_list),
- "multiple": ElementProperty(PropertyType.boolean, False),
- },
- inner_properties={
- "inner_scenarios": ElementProperty(
- PropertyType.lov,
- f"{{{__CTX_VAR_NAME}.get_scenarios(<tp:prop:{_GuiCoreContext._SEL_SCENARIOS_PROP}>)}}",
- ),
- "on_scenario_crud": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.crud_scenario}}"),
- "configs": ElementProperty(PropertyType.react, f"{{{__CTX_VAR_NAME}.get_scenario_configs()}}"),
- "core_changed": ElementProperty(PropertyType.broadcast, _GuiCoreContext._CORE_CHANGED_NAME),
- "error": ElementProperty(PropertyType.react, f"{{{_GuiCoreContext._SCENARIO_SELECTOR_ERROR_VAR}}}"),
- "type": ElementProperty(PropertyType.inner, __SCENARIO_ADAPTER),
- "scenario_edit": ElementProperty(
- _GuiCoreScenarioAdapter,
- f"{{{__CTX_VAR_NAME}.get_scenario_by_id({_GuiCoreContext._SCENARIO_SELECTOR_ID_VAR})}}",
- ),
- "on_scenario_select": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.select_scenario}}"),
- },
- ),
- "scenario": Element(
- "scenario",
- {
- "id": ElementProperty(PropertyType.string),
- "scenario": ElementProperty(_GuiCoreScenarioAdapter),
- "active": ElementProperty(PropertyType.dynamic_boolean, True),
- "expandable": ElementProperty(PropertyType.boolean, True),
- "expanded": ElementProperty(PropertyType.boolean, True),
- "show_submit": ElementProperty(PropertyType.boolean, True),
- "show_delete": ElementProperty(PropertyType.boolean, True),
- "show_config": ElementProperty(PropertyType.boolean, False),
- "show_cycle": ElementProperty(PropertyType.boolean, False),
- "show_tags": ElementProperty(PropertyType.boolean, True),
- "show_properties": ElementProperty(PropertyType.boolean, True),
- "show_sequences": ElementProperty(PropertyType.boolean, True),
- "show_submit_sequences": ElementProperty(PropertyType.boolean, True),
- "class_name": ElementProperty(PropertyType.dynamic_string),
- "on_submission_change": ElementProperty(PropertyType.function),
- },
- inner_properties={
- "on_edit": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.edit_entity}}"),
- "on_submit": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.submit_entity}}"),
- "on_delete": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.crud_scenario}}"),
- "core_changed": ElementProperty(PropertyType.broadcast, _GuiCoreContext._CORE_CHANGED_NAME),
- "error": ElementProperty(PropertyType.react, f"{{{_GuiCoreContext._SCENARIO_VIZ_ERROR_VAR}}}"),
- },
- ),
- "scenario_dag": Element(
- "scenario",
- {
- "id": ElementProperty(PropertyType.string),
- "scenario": ElementProperty(_GuiCoreScenarioDagAdapter),
- "render": ElementProperty(PropertyType.dynamic_boolean, True),
- "show_toolbar": ElementProperty(PropertyType.boolean, True),
- "width": ElementProperty(PropertyType.string),
- "height": ElementProperty(PropertyType.string),
- "class_name": ElementProperty(PropertyType.dynamic_string),
- "on_action": ElementProperty(PropertyType.function),
- },
- inner_properties={
- "core_changed": ElementProperty(PropertyType.broadcast, _GuiCoreContext._CORE_CHANGED_NAME),
- "on_select": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.on_dag_select}}"),
- },
- ),
- "data_node_selector": Element(
- "value",
- {
- "id": ElementProperty(PropertyType.string),
- "display_cycles": ElementProperty(PropertyType.boolean, True),
- "show_primary_flag": ElementProperty(PropertyType.boolean, True),
- "value": ElementProperty(PropertyType.lov_value),
- "on_change": ElementProperty(PropertyType.function),
- "height": ElementProperty(PropertyType.string, "50vh"),
- "class_name": ElementProperty(PropertyType.dynamic_string),
- "show_pins": ElementProperty(PropertyType.boolean, True),
- _GuiCoreContext._DATANODE_SEL_SCENARIO_PROP: ElementProperty(PropertyType.dynamic_list),
- "multiple": ElementProperty(PropertyType.boolean, False),
- },
- inner_properties={
- "datanodes": ElementProperty(
- PropertyType.lov,
- f"{{{__CTX_VAR_NAME}.get_datanodes_tree(<tp:prop:{_GuiCoreContext._DATANODE_SEL_SCENARIO_PROP}>)}}",
- ),
- "core_changed": ElementProperty(PropertyType.broadcast, _GuiCoreContext._CORE_CHANGED_NAME),
- "type": ElementProperty(PropertyType.inner, __DATANODE_ADAPTER),
- },
- ),
- "data_node": Element(
- _GuiCoreContext._DATANODE_VIZ_DATA_NODE_PROP,
- {
- "id": ElementProperty(PropertyType.string),
- _GuiCoreContext._DATANODE_VIZ_DATA_NODE_PROP: ElementProperty(_GuiCoreDatanodeAdapter),
- "active": ElementProperty(PropertyType.dynamic_boolean, True),
- "expandable": ElementProperty(PropertyType.boolean, True),
- "expanded": ElementProperty(PropertyType.boolean, True),
- "show_config": ElementProperty(PropertyType.boolean, False),
- "show_owner": ElementProperty(PropertyType.boolean, True),
- "show_edit_date": ElementProperty(PropertyType.boolean, False),
- "show_expiration_date": ElementProperty(PropertyType.boolean, False),
- "show_properties": ElementProperty(PropertyType.boolean, True),
- "show_history": ElementProperty(PropertyType.boolean, True),
- "show_data": ElementProperty(PropertyType.boolean, True),
- "chart_configs": ElementProperty(PropertyType.dict),
- "class_name": ElementProperty(PropertyType.dynamic_string),
- "scenario": ElementProperty(PropertyType.lov_value, "optional"),
- "width": ElementProperty(PropertyType.string),
- },
- inner_properties={
- "on_edit": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.edit_data_node}}"),
- "core_changed": ElementProperty(PropertyType.broadcast, _GuiCoreContext._CORE_CHANGED_NAME),
- "error": ElementProperty(PropertyType.react, f"{{{_GuiCoreContext._DATANODE_VIZ_ERROR_VAR}}}"),
- "scenarios": ElementProperty(
- PropertyType.lov,
- f"{{{__CTX_VAR_NAME}.get_scenarios_for_owner({_GuiCoreContext._DATANODE_VIZ_OWNER_ID_VAR},"
- + "<tp:uniq:dn>)}",
- ),
- "type": ElementProperty(PropertyType.inner, __SCENARIO_ADAPTER),
- "dn_properties": ElementProperty(
- PropertyType.react,
- f"{{{__CTX_VAR_NAME}.get_data_node_properties("
- + f"{_GuiCoreContext._DATANODE_VIZ_PROPERTIES_ID_VAR},"
- + "<tp:uniq:dn>)}",
- ),
- "history": ElementProperty(
- PropertyType.react,
- f"{{{__CTX_VAR_NAME}.get_data_node_history("
- + f"{_GuiCoreContext._DATANODE_VIZ_HISTORY_ID_VAR},"
- + "<tp:uniq:dn>)}",
- ),
- "tabular_data": ElementProperty(
- PropertyType.data,
- f"{{{__CTX_VAR_NAME}.get_data_node_tabular_data("
- + f"{_GuiCoreContext._DATANODE_VIZ_DATA_ID_VAR},"
- + "<tp:uniq:dn>)}",
- ),
- "tabular_columns": ElementProperty(
- PropertyType.dynamic_string,
- f"{{{__CTX_VAR_NAME}.get_data_node_tabular_columns("
- + f"{_GuiCoreContext._DATANODE_VIZ_DATA_ID_VAR},"
- + "<tp:uniq:dn>)}",
- with_update=True,
- ),
- "chart_config": ElementProperty(
- PropertyType.dynamic_string,
- f"{{{__CTX_VAR_NAME}.get_data_node_chart_config("
- + f"{_GuiCoreContext._DATANODE_VIZ_DATA_CHART_ID_VAR},"
- + "<tp:uniq:dn>)}",
- with_update=True,
- ),
- "on_data_value": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.update_data}}"),
- "on_tabular_data_edit": ElementProperty(
- PropertyType.function, f"{{{__CTX_VAR_NAME}.tabular_data_edit}}"
- ),
- "on_lock": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.lock_datanode_for_edit}}"),
- "update_dn_vars": ElementProperty(
- PropertyType.string,
- f"data_id={_GuiCoreContext._DATANODE_VIZ_DATA_ID_VAR};"
- + f"history_id={_GuiCoreContext._DATANODE_VIZ_HISTORY_ID_VAR};"
- + f"owner_id={_GuiCoreContext._DATANODE_VIZ_OWNER_ID_VAR};"
- + f"chart_id={_GuiCoreContext._DATANODE_VIZ_DATA_CHART_ID_VAR};"
- + f"properties_id={_GuiCoreContext._DATANODE_VIZ_PROPERTIES_ID_VAR}",
- ),
- },
- ),
- "job_selector": Element(
- "value",
- {
- "id": ElementProperty(PropertyType.string),
- "class_name": ElementProperty(PropertyType.dynamic_string),
- "value": ElementProperty(PropertyType.lov_value),
- "show_id": ElementProperty(PropertyType.boolean, True),
- "show_submitted_label": ElementProperty(PropertyType.boolean, True),
- "show_submitted_id": ElementProperty(PropertyType.boolean, False),
- "show_submission_id": ElementProperty(PropertyType.boolean, False),
- "show_date": ElementProperty(PropertyType.boolean, True),
- "show_cancel": ElementProperty(PropertyType.boolean, True),
- "show_delete": ElementProperty(PropertyType.boolean, True),
- "on_change": ElementProperty(PropertyType.function),
- "height": ElementProperty(PropertyType.string, "50vh"),
- },
- inner_properties={
- "jobs": ElementProperty(PropertyType.lov, f"{{{__CTX_VAR_NAME}.get_jobs_list()}}"),
- "core_changed": ElementProperty(PropertyType.broadcast, _GuiCoreContext._CORE_CHANGED_NAME),
- "type": ElementProperty(PropertyType.inner, __JOB_ADAPTER),
- "on_job_action": ElementProperty(PropertyType.function, f"{{{__CTX_VAR_NAME}.act_on_jobs}}"),
- "error": ElementProperty(PropertyType.dynamic_string, f"{{{_GuiCoreContext._JOB_SELECTOR_ERROR_VAR}}}"),
- },
- ),
- }
- def get_name(self) -> str:
- return _GuiCore.__LIB_NAME
- def get_elements(self) -> t.Dict[str, Element]:
- return _GuiCore.__elts
- def get_scripts(self) -> t.List[str]:
- return ["lib/taipy-gui-core.js"]
- def on_init(self, gui: Gui) -> t.Optional[t.Tuple[str, t.Any]]:
- gui._get_default_locals_bind().update({v: "" for v in _GuiCore.__INNER_VARS})
- ctx = _GuiCoreContext(gui)
- gui._add_adapter_for_type(_GuiCore.__SCENARIO_ADAPTER, ctx.scenario_adapter)
- gui._add_adapter_for_type(_GuiCore.__DATANODE_ADAPTER, ctx.data_node_adapter)
- gui._add_adapter_for_type(_GuiCore.__JOB_ADAPTER, ctx.job_adapter)
- return _GuiCore.__CTX_VAR_NAME, ctx
- def on_user_init(self, state: State):
- for var in _GuiCore.__INNER_VARS:
- state._add_attribute(var, "")
- def get_version(self) -> str:
- if not hasattr(self, "version"):
- self.version = _get_version()
- if "dev" in self.version:
- self.version += str(datetime.now().timestamp())
- return self.version
|