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