taipy

How to update python dataframe data in chart and it should update in interval?


The python data frame getting refreshed and the data need to update in chart. It contains the time and sales data to show on chart. As time and sales data changes it should shows in chart. Tried the invoke_long_callback function but not able to update data in chart.


Solution

  • The best way to do it would be to use long callbacks. Long callbacks are used to execute a long job without blocking the application and update the elements of the GUI easily. Here are two links: one for the documentation and one for examples.

    In your case, it won't execute a job. It will just update the chart in intervals.

    The code below will create a page with a button and a chart. The data of the chart is refreshed every 2 seconds. This refresh starts when the button is pressed. If you want it to begin at the start of the application, change the name 'on_button' to 'on_init'.

    from taipy.gui import Gui, invoke_long_callback
    import time
    
    from math import cos, exp
    
    def compute_data(decay:int)->list:
        return [cos(i/6) * exp(-i*decay/600) for i in range(100)]
    
    def heavy_function():
        """Just a function to sleep, it normally executes a job"""
        while True:
            time.sleep(1)
    
    def heavy_function_status(state, status):
        """The function that will be called each 2000 milliseconds
        and update the data"""
        state.decay += 5
        state.data = compute_data(state.decay)
     
    def on_button(state):
        invoke_long_callback(state, heavy_function, [],
                             heavy_function_status, [],
                             2000)
        
    
    decay = 1
    data = compute_data(decay)
    
    Gui("<|Start refresh of data|button|on_action=on_button|> <|{data}|chart|>").run()
    

    Another example:

    from taipy.gui import Gui, invoke_long_callback
    import pandas as pd
    import numpy as np
    import time
    
    def fetch_data():
        data = pd.DataFrame({"x": range(10), "random_y": np.random.randn(10), "other_var":[4,5,3,4,5,6,7,8,9,10]})
        return data
    
    
    def heavy_function():
        """Just a function to sleep, it normally executes a job"""
        while True:
            time.sleep(1)
    
    def heavy_function_status(state, status):
        """The function that will be called each 2000 milliseconds
        and update the data"""
        state.data = fetch_data() # Here I have called the function
        state.subset = {"x":state.data['x'], "y_random":state.data['random_y']}
     
    def on_button(state):
        invoke_long_callback(state, heavy_function, [],
                             heavy_function_status, [],
                             2000)
    
    data = fetch_data()
    subset = {"x":data['x'], "y_random":data['random_y']}
    
    Gui("<|Start refresh of data|button|on_action=on_button|><|{subset}|chart|x=x|y[1]=y_random|>").run()
    

    This feature of updating a chart will be integrated into the next release for streamlining data. So, it will be much easier to do what you want in the upcoming weeks when it is released.