PandasAI in Plotly Dash

Hello, has anyone tried or has an idea how to implement PandasAI outputs? I am asking specifically about dataframes and matplotlib charts (open on serverside as apps). The answers as string are compatible from what I tested.
Could use try or dtype detection to process dataframes but for charts - no idea.

It is somehow done inside jupyter notebooks without much fuss.

I hope the discussion will be fruitful

Greetings,
Kamil

I figured it out, had to do some wrapping and matplotlib settings changes.
So for matplotlib basically - the following code turns off default chart plot (on server side)

import matplotlib
matplotlib.use('Agg')

I have some user identyfication so my dir id is custom, generally pandasAI creates temp_chart.png, wchich can be put inside your custom folder - best if it’s your assets as then you can easily access it.
You also need to change config for SmartDataFrame / SmartDataLake

  dir_id = str(session['user']) +"_"+ str(session['db'])

  assets_dir = 'assets/'
  dir_path = f"images/pandasai_{dir_id}/"
  full_path = assets_dir + dir_path
  dir_create_if_not_exists(dir_path)
  config = {"save_charts":True,
            "save_charts_path":full_path,
            "llm": llm}

I found 3 output types from PandasAI: str (for text response), pandasai.smart_dataframe.SmartDataframe (for table) and None for imgs.

    df = SmartDataframe(df_filtered, config=config) 
    answer = df.chat(question)
    
    if type(answer) is str:
        return html.Div(answer, className='standard-output-div')
    if type(answer) is pandasai.smart_dataframe.SmartDataframe:
        from components.AIO_AgGridTable import AIO_AgGridDataTable
        return AIO_AgGridDataTable(answer, 'Data Assistant output', pagination=True, paginationPageSize=30)
    else:
        time.sleep(2)
        return html.Div(html.Img(src = dash.get_asset_url(f'{dir_path}temp_chart.png')), 
                        style = {'horizontal-align':'center'})

I wrap pandasai.smart_dataframe.SmartDataframe into nice funtional agGrid. The rest is basic.
Honestly this is amazing, the users enjoy this data exploration feature a lot.

Hope you will find it useful