# -*- coding: utf-8 -*- """Fuel_CompAPP.ipynb Automatically generated by Colab. Original file is located at https://colab.research.google.com/drive/1OtjGIkl4y8uY9b3pvalX4LzTIUDaGpYT """ import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import plotly.express as px import gradio as gr try: df = pd.read_csv("https://raw.githubusercontent.com/baren-kumar/File-/refs/heads/main/data-fuel-consumption.csv") except FileNotFoundError: raise FileNotFoundError("https://raw.githubusercontent.com/baren-kumar/File-/refs/heads/main/data-fuel-consumption.csv' was not found.") def create_dashboard_components(num_cylinders, vehicle_class): """ Generates all dashboard components based on the selected number of cylinders and vehicle class. """ # Filter data for both selected criteria filtered_df = df[(df['CYLINDERS'] == num_cylinders) & (df['VEHICLECLASS'] == vehicle_class)] if filtered_df.empty: # Return empty components and a message if no data is found return None, None, None, f"No data available for {num_cylinders} cylinders and {vehicle_class}.", None # Group by MODEL and calculate total CO2 emissions emissions_by_model = filtered_df.groupby('MODEL')['CO2EMISSIONS'].sum().reset_index() # Create the bar chart using Plotly bar_chart = px.bar( emissions_by_model, x='MODEL', y='CO2EMISSIONS', title=f'Total CO2 Emissions by Model for {num_cylinders} Cylinders and {vehicle_class}' ) bar_chart.update_layout( xaxis_title="Vehicle Model", yaxis_title="Total CO2 Emissions", xaxis={'categoryorder': 'total descending'} ) # Create the pie chart using Plotly pie_chart = px.pie( emissions_by_model, values='CO2EMISSIONS', names='MODEL', title=f'Distribution of CO2 Emissions for {num_cylinders} Cylinders and {vehicle_class}' ) # Create a summary string total_emissions = filtered_df['CO2EMISSIONS'].sum() average_emissions = filtered_df['CO2EMISSIONS'].mean() summary = ( f"Summary for vehicles with {num_cylinders} cylinders and vehicle class '{vehicle_class}':\n" f"Total CO2 Emissions: {total_emissions:,.2f} g/km\n" f"Average CO2 Emissions: {average_emissions:,.2f} g/km\n" f"Number of Records: {len(filtered_df)}" ) return bar_chart, pie_chart, filtered_df, summary, None # Get a unique sorted list of cylinder counts for the dropdown unique_cylinders = sorted(df['CYLINDERS'].unique().tolist()) unique_vehicle_classes = sorted(df['VEHICLECLASS'].unique().tolist()) # Create the Gradio interface with gr.Blocks(title="CO2 Emissions Dashboard") as dashboard: gr.Markdown("# šŸ“Š CO2 Emissions Dashboard") gr.Markdown("Use the filters below to explore CO2 emissions data.") with gr.Row(): cylinder_selector = gr.Dropdown( choices=unique_cylinders, label="Select Number of Cylinders", value=unique_cylinders[0] ) vehicle_class_selector = gr.Dropdown( choices=unique_vehicle_classes, label="Select Vehicle Class", value=unique_vehicle_classes[0] ) with gr.Row(): with gr.Column(scale=2): chart_bar = gr.Plot(label="Bar Chart") with gr.Column(scale=1): chart_pie = gr.Plot(label="Pie Chart") with gr.Row(): summary_output = gr.Textbox(label="Summary", interactive=False) with gr.Row(): table_output = gr.Dataframe(label="Data Table") # Define event to update all components when either dropdown value changes # A cleaner approach is to use a single function that takes both inputs def update_all(cylinders, vehicle_class): return create_dashboard_components(cylinders, vehicle_class) cylinder_selector.change( fn=update_all, inputs=[cylinder_selector, vehicle_class_selector], outputs=[chart_bar, chart_pie, table_output, summary_output] ) vehicle_class_selector.change( fn=update_all, inputs=[cylinder_selector, vehicle_class_selector], outputs=[chart_bar, chart_pie, table_output, summary_output] ) # Load the initial components when the app starts dashboard.load( fn=lambda: create_dashboard_components(unique_cylinders[0], unique_vehicle_classes[0]), outputs=[chart_bar, chart_pie, table_output, summary_output] ) gr.Markdown("---") gr.Markdown("šŸ‘Øā€šŸ’» Developed by **Baren Kumar Baidya ** \nšŸ“§ [eng.barenbscme@gmail.com](mailto:eng.barenbscme@gmail.com)") # Launch the Gradio app dashboard.launch()