Avaiga / taipy

Turns Data and AI algorithms into production-ready web applications in no time.
https://www.taipy.io
Apache License 2.0
10.11k stars 713 forks source link

Evaluating Core objects expressions in Taipy(Python) without warnings #406

Open FlorianJacta opened 1 year ago

FlorianJacta commented 1 year ago

What would that feature address The aim is to integrate Core expressions within the augmented Markdown of Taipy GUI. In the beginning stages of the application, scenarios are typically not created.

In this situation, utilizing Core expressions in the augmented Markdown—similar to scenario attributes or functions (e.g., .is_primary, .name, etc.)—results in a Warning during the web application's initialization.

Description of the ideal solution The objective is to develop a method that prevents these warnings while still allowing the use of Core expressions in the Markdown, such as scenario.is_primary, scenario.name, scenario.creation_date, ...

jrobinAV commented 1 year ago

I believe the good practice is to have a proper initialization of your application variables. However, initializing your variables with an instance of such a "mock" class could do the trick. Something like that :

from __future__ import annotations

from datetime import datetime
from typing import List

from ..data.data_node import DataNode
from ..pipeline.pipeline import Pipeline
from ..task.task import Task

class EmptyScenario:
    """ An empty scenario singleton.

        It is designed to mock the scenario class. It can be used to initialize a variable representing a Scenario
        instead of initializing it to None. This is particularly useful when the variable is used in a Taipy
        GUI callback function to avoid runtime errors.
    """

    def __init__(self, replacement_label: str = "No_scenario"):
        self.replacement_label = replacement_label

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, exc_traceback):
        pass

    def __getattr__(self, attribute_name):
        if attribute_name == "data_nodes":
            return dict[str, DataNode]()
        elif attribute_name == "pipelines":
            return dict[str, List[Pipeline]]()
        elif attribute_name == "tasks":
            return dict[str, List[Task]]()
        elif attribute_name == "config_id":
            return self.replacement_label
        elif attribute_name == "id":
            return self.replacement_label
        elif attribute_name == "name":
            return self.replacement_label
        elif attribute_name == "label":
            return self.replacement_label
        elif attribute_name == "creation_date":
            return datetime.fromtimestamp(0)
        elif attribute_name == "cycle":
            return None
        elif attribute_name == "is_primary":
            return False
        elif attribute_name == "subscribers":
            return list()
        elif attribute_name == "tags":
            return set()
        elif attribute_name == "owner_id":
            return None
        elif attribute_name == "has_tag":
            return False
        else:
            return None

    def subscribe(self, *args, **kwargs):
        pass

    def unsubscribe(self, *args, **kwargs):
        pass

    def submit(self, *args, **kwargs):
        return []

    def export(self, *args, **kwargs):
        pass

    def set_primary(self, *args, **kwargs):
        pass

    def add_tag(self, *args, **kwargs):
        pass

    def remove_tag(self, *args, **kwargs):
        pass

    def is_deletable(self, *args, **kwargs) -> bool:
        return False

    def get_label(self, *args, **kwargs) -> str:
        return self.replacement_label

    def get_simple_label(self, *args, **kwargs) -> str:
        return self.replacement_label

We could implement this for scenarios, pipelines, tasks, cycles, jobs, datanodes if needed.

jrobinAV commented 1 year ago

@FlorianJacta Do not hesitate to test it and tell me if it fits your needs.

jrobinAV commented 4 months ago

@FlorianJacta Do not hesitate to test it and tell me if it fits your needs.

jrobinAV commented 4 months ago

Note from FLE: Make the Empty objects sub-classes of real Core entities.