chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

Plotting multiple charts using "side" argument in Plotly

Open Hiten-03 opened this issue 2 months ago • 0 comments

I am working a project where when a user uploads and excel file then it should plot count plots for all categorical columns. I am using plotly and in that it is working with the inline command of display argument, but when instead of inline, side command is used then it shows blank. Whereas if I plot only single chart with side command it works

import pandas as pd
import itertools
import chainlit as cl
import plotly.express as px
from chainlit.input_widget import Select
import plotly.graph_objects as go


@cl.on_chat_start
async def on_chat_start():
    """
    Decorator is used to define a hook that is called when a new chat session is created.
    """
    files = None
    # print('user',cl.user,cl.user.UserDict,type(cl.user.UserDict),dir(cl.user.UserDict))
    # Wait for the user to upload a file
    # while files == None:
    files = await cl.AskFileMessage(
        content="Please upload a text file to begin!",
        accept=["*/*"],
        max_size_mb=10,
        max_files=3,
        timeout=180,
    ).send()

    # file = files[0]  # limit to single file
    # print('file type-> ',file,dir(file))
    all_pages = []
    for file in files:
        msg = cl.Message(content=f"Processing `{file.name}`...", disable_feedback=True)
        await msg.send()

        df = pd.read_excel(file.path)
        categorical_columns = list(df.select_dtypes(include=['object', 'category']).columns)

        all_figures = []
        for column in categorical_columns:
            counts = df[column].value_counts()
            fig = go.Figure(data=[go.Bar(x=counts.index, y=counts.values)])
            fig.update_layout(title=f'Count Plot for {column}')
            all_figures.append(fig)
        
        await cl.Message(
            content="chart",
            elements=[cl.Plotly(name=f"chart_{column}", figure=fig, display="side") for fig in all_figures],
        ).send()


        print(f"DATA FRAME ---> {df} \n {categorical_columns}")

Hiten-03 avatar Apr 22 '24 09:04 Hiten-03