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

BUG- Error when using the Taipy data generated from another __main__ #1174

Open FlorianJacta opened 3 months ago

FlorianJacta commented 3 months ago

Description An error is created when I am trying to use Taipy SDM (Scenario and Data Management) from another script. The issue only arises when my tasks use functions inside my main.py (main).

How to reproduce main.py

This is working fine:

from taipy import Config
import taipy as tp
import pandas as pd
import datetime as dt

data = pd.read_csv("https://raw.githubusercontent.com/Avaiga/taipy-getting-started-core/develop/src/daily-min-temperatures.csv")

# Normal function used by Taipy
def predict(historical_temperature: pd.DataFrame, date_to_forecast: dt.datetime) -> float:
    print(f"Running baseline...")
    historical_temperature['Date'] = pd.to_datetime(historical_temperature['Date'])
    historical_same_day = historical_temperature.loc[
        (historical_temperature['Date'].dt.day == date_to_forecast.day) &
        (historical_temperature['Date'].dt.month == date_to_forecast.month)
    ]
    return historical_same_day['Temp'].mean()

# Configuration of Data Nodes
historical_temperature_cfg = Config.configure_data_node("historical_temperature")
date_to_forecast_cfg = Config.configure_data_node("date_to_forecast")
predictions_cfg = Config.configure_data_node("predictions")

# Configuration of tasks
predictions_cfg = Config.configure_task("predict",
                                        predict,
                                        [historical_temperature_cfg, date_to_forecast_cfg],
                                        predictions_cfg)

# Configuration of scenario
scenario_cfg = Config.configure_scenario(id="my_scenario", task_configs=[predictions_cfg])

Config.export('config.toml')

if __name__ == '__main__':
    # Run of the Core
    tp.Core().run()

    # Creation of the scenario and execution
    scenario = tp.create_scenario(scenario_cfg)
    scenario.historical_temperature.write(data)
    scenario.date_to_forecast.write(dt.datetime.now())
    tp.submit(scenario)

test.py:

This throws an error.

import taipy as tp

if __name__ == '__main__':
    # Creation of the scenario and execution
    scenario = tp.get_scenarios()[0]
    print(scenario.date_to_forecast.read())

Error: image (11)

Expected behavior At a minimum, better logs should appear.

Runtime environment Please specify relevant indications.

jrobinAV commented 3 months ago

Indeed, the internal Taipy data (.taipy folder) is not designed to be shared across multiple applications (multiple main scripts). The data stores some user code locations relative to the main script. Switching from one application to another is not guaranteed to work properly. This would depend on where the user code is located and how it is imported and declared to Taipy objects.

I keep the ticket open to investigate if we can do something to make it work in more cases or at least to document the use cases that don't work.