File size: 1,260 Bytes
333f068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sqlite3
import pandas as pd
import plotly.express as px
from database import DB_NAME

def generate_sentiment_pie_chart():
    """Fetches data and returns a Plotly Pie Chart figure."""
    conn = sqlite3.connect(DB_NAME)
    # Fetch only the sentiment column
    df = pd.read_sql_query("SELECT sentiment FROM reviews", conn)
    conn.close()
    
    if df.empty:
        # Return a simple figure with a message if no reviews exist
        fig = px.pie(title="No reviews collected yet.")
        return fig
    
    # Process data for the chart
    counts = df['sentiment'].value_counts().reset_index()
    counts.columns = ['Sentiment', 'Total']
    
    # Create the chart
    fig = px.pie(
        counts, 
        values='Total', 
        names='Sentiment', 
        title='Customer Sentiment Distribution',
        # Professional color mapping
        color='Sentiment',
        color_discrete_map={
            'Positive': '#2ecc71', # Emerald Green
            'Negative': '#e74c3c', # Alizarin Red
            'Neutral': '#f1c40f'   # Sunflower Yellow
        }
    )
    
    # Update layout for a cleaner look
    fig.update_layout(showlegend=True, margin=dict(t=50, b=20, l=20, r=20))
    
    return fig