I have some CSV which I want to see as a dashboard, so I can easily explore the data. - Create a dropzone where a user can drop his csv. Once drop, render a table + Interactive charts showing transaction trends, project type distributions, and geographic breakdowns transaction_id,date,project_type,project_id,seller,buyer,quantity,price_per_ton,total_value,standard,vintage_year,location TR-12493,2024-01-15,Reforestation,PRJ-4587,Carbon Forest Partners,GreenInvest Group,5000,12.50,62500.00,Gold Standard,2023,Brazil TR-12494,2024-01-17,Renewable Energy,PRJ-2839,SunPower Solutions,Eco Capital Fund,3200,9.75,31200.00,VCS,2023,India TR-12495,2024-01-21,Methane Capture,PRJ-6721,AgriTech Emissions,Climate Forward,2800,14.25,39900.00,CAR,2023,United States
To upload files, please first save the app
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import altair as alt
from datetime import datetime
import io
st.set_page_config(page_title="Carbon Credit Dashboard", layout="wide")
st.title("๐ Carbon Credit Transaction Dashboard")
st.markdown("Upload your CSV file to explore carbon credit transaction data")
# File uploader
uploaded_file = st.file_uploader(
"Drop your CSV file here",
type=['csv'],
help="Upload a CSV file containing carbon credit transaction data"
)
# Sample data for demonstration
sample_data = """transaction_id,date,project_type,project_id,seller,buyer,quantity,price_per_ton,total_value,standard,vintage_year,location
TR-12493,2024-01-15,Reforestation,PRJ-4587,Carbon Forest Partners,GreenInvest Group,5000,12.50,62500.00,Gold Standard,2023,Brazil
TR-12494,2024-01-17,Renewable Energy,PRJ-2839,SunPower Solutions,Eco Capital Fund,3200,9.75,31200.00,VCS,2023,India
TR-12495,2024-01-21,Methane Capture,PRJ-6721,AgriTech Emissions,Climate Forward,2800,14.25,39900.00,CAR,2023,United States
TR-12496,2024-01-23,Reforestation,PRJ-4588,Forest Guard Corp,Blue Sky Ventures,4500,13.00,58500.00,Gold Standard,2023,Indonesia
TR-12497,2024-01-25,Solar Energy,PRJ-2840,Solar Dynamics,EcoFund International,6000,8.90,53400.00,VCS,2023,Morocco
TR-12498,2024-01-28,Wind Energy,PRJ-2841,WindTech Solutions,Green Capital Partners,7200,10.20,73440.00,VCS,2023,Denmark
TR-12499,2024-02-01,Reforestation,PRJ-4589,Amazon Forestry,Carbon Offset Inc,3800,12.75,48450.00,Gold Standard,2023,Peru
TR-12500,2024-02-03,Methane Capture,PRJ-6722,Waste Management Co,Climate Solutions,2100,15.50,32550.00,CAR,2023,Canada
TR-12501,2024-02-05,Renewable Energy,PRJ-2842,HydroClean Energy,Sustainable Investments,5500,11.30,62150.00,VCS,2023,Norway
TR-12502,2024-02-08,Reforestation,PRJ-4590,Tropical Forest Alliance,Green Future Fund,4200,13.25,55650.00,Gold Standard,2023,Costa Rica"""
def load_data(file_content):
"""Load and process the CSV data"""
try:
df = pd.read_csv(io.StringIO(file_content))
# Convert date column to datetime
if 'date' in df.columns:
df['date'] = pd.to_datetime(df['date'])
# Ensure numeric columns are properly typed
numeric_columns = ['quantity', 'price_per_ton', 'total_value', 'vintage_year']
for col in numeric_columns:
if col in df.columns:
df[col] = pd.to_numeric(df[col], errors='coerce')
return df
except Exception as e:
st.error(f"Error loading data: {str(e)}")
return None
def create_charts(df):
"""Create interactive charts for the dashboard"""
col1, col2 = st.columns(2)
with col1:
st.subheader("๐ Transaction Trends Over Time")
# Daily transaction volume
daily_stats = df.groupby('date').agg({
'total_value': 'sum',
'quantity': 'sum',
'transaction_id': 'count'
}).reset_index()
fig_trend = px.line(
daily_stats,
x='date',
y='total_value',
title='Total Transaction Value Over Time',
labels={'total_value': 'Total Value ($)', 'date': 'Date'}
)
fig_trend.update_layout(height=400)
st.plotly_chart(fig_trend, use_container_width=True)
with col2:
st.subheader("๐ญ Project Type Distribution")
# Project type distribution
project_stats = df.groupby('project_type').agg({
'total_value': 'sum',
'quantity': 'sum',
'transaction_id': 'count'
}).reset_index()
fig_pie = px.pie(
project_stats,
values='total_value',
names='project_type',
title='Total Value by Project Type'
)
fig_pie.update_layout(height=400)
st.plotly_chart(fig_pie, use_container_width=True)
col3, col4 = st.columns(2)
with col3:
st.subheader("๐ Geographic Distribution")
# Geographic breakdown
location_stats = df.groupby('location').agg({
'total_value': 'sum',
'quantity': 'sum',
'transaction_id': 'count'
}).reset_index().sort_values('total_value', ascending=False)
fig_geo = px.bar(
location_stats.head(10),
x='location',
y='total_value',
title='Transaction Value by Location (Top 10)',
labels={'total_value': 'Total Value ($)', 'location': 'Location'}
)
fig_geo.update_xaxes(tickangle=45)
fig_geo.update_layout(height=400)
st.plotly_chart(fig_geo, use_container_width=True)
with col4:
st.subheader("๐ฐ Price per Ton Analysis")
# Price distribution by project type
fig_box = px.box(
df,
x='project_type',
y='price_per_ton',
title='Price per Ton Distribution by Project Type'
)
fig_box.update_xaxes(tickangle=45)
fig_box.update_layout(height=400)
st.plotly_chart(fig_box, use_container_width=True)
def show_summary_metrics(df):
"""Display key summary metrics"""
st.subheader("๐ Key Metrics")
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
total_transactions = len(df)
st.metric("Total Transactions", f"{total_transactions:,}")
with col2:
total_value = df['total_value'].sum()
st.metric("Total Value", f"${total_value:,.2f}")
with col3:
total_quantity = df['quantity'].sum()
st.metric("Total Quantity", f"{total_quantity:,} tons")
with col4:
avg_price = df['price_per_ton'].mean()
st.metric("Avg Price/Ton", f"${avg_price:.2f}")
with col5:
unique_projects = df['project_type'].nunique()
st.metric("Project Types", unique_projects)
# Main app logic
if uploaded_file is not None:
# Read the uploaded file
file_content = uploaded_file.read().decode('utf-8')
df = load_data(file_content)
if df is not None:
st.success(f"Successfully loaded {len(df)} transactions!")
# Show summary metrics
show_summary_metrics(df)
# Create tabs for different views
tab1, tab2, tab3 = st.tabs(["๐ Dashboard", "๐ Data Table", "๐ Data Info"])
with tab1:
create_charts(df)
with tab2:
st.subheader("Transaction Data")
st.dataframe(df, use_container_width=True)
# Download filtered data
csv = df.to_csv(index=False)
st.download_button(
label="Download data as CSV",
data=csv,
file_name='filtered_transactions.csv',
mime='text/csv',
)
with tab3:
st.subheader("Dataset Information")
col1, col2 = st.columns(2)
with col1:
st.write("**Dataset Shape:**", df.shape)
st.write("**Date Range:**", f"{df['date'].min().strftime('%Y-%m-%d')} to {df['date'].max().strftime('%Y-%m-%d')}")
st.write("**Column Info:**")
buffer = io.StringIO()
df.info(buf=buffer)
st.text(buffer.getvalue())
with col2:
st.write("**Summary Statistics:**")
st.dataframe(df.describe())
else:
st.info("๐ Please upload a CSV file to get started")
# Show sample data preview
with st.expander("๐ View Sample Data Format"):
st.markdown("Here's an example of the expected CSV format:")
sample_df = load_data(sample_data)
if sample_df is not None:
st.dataframe(sample_df)
st.markdown("**Expected columns:**")
st.write("- `transaction_id`: Unique identifier for each transaction")
st.write("- `date`: Transaction date (YYYY-MM-DD format)")
st.write("- `project_type`: Type of carbon credit project")
st.write("- `project_id`: Project identifier")
st.write("- `seller`: Selling organization")
st.write("- `buyer`: Buying organization")
st.write("- `quantity`: Quantity of carbon credits (tons)")
st.write("- `price_per_ton`: Price per ton of CO2")
st.write("- `total_value`: Total transaction value")
st.write("- `standard`: Carbon credit standard")
st.write("- `vintage_year`: Year of carbon credit vintage")
st.write("- `location`: Geographic location of project")
# Test with sample data button
if st.button("๐งช Try with Sample Data"):
df = load_data(sample_data)
if df is not None:
st.success("Loaded sample data!")
# Show summary metrics
show_summary_metrics(df)
# Create tabs for different views
tab1, tab2, tab3 = st.tabs(["๐ Dashboard", "๐ Data Table", "๐ Data Info"])
with tab1:
create_charts(df)
with tab2:
st.subheader("Sample Transaction Data")
st.dataframe(df, use_container_width=True)
with tab3:
st.subheader("Sample Dataset Information")
col1, col2 = st.columns(2)
with col1:
st.write("**Dataset Shape:**", df.shape)
st.write("**Date Range:**", f"{df['date'].min().strftime('%Y-%m-%d')} to {df['date'].max().strftime('%Y-%m-%d')}")
with col2:
st.write("**Summary Statistics:**")
st.dataframe(df.describe())
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?