climateclock / app.py
gaialive's picture
Update app.py
32a1a86 verified
Raw
History Blame Contribute Delete
5.12 kB
import streamlit as st
import pandas as pd
import altair as alt
import json
import os
from datetime import datetime
# -------------------------------
# App Config and Title
# -------------------------------
st.set_page_config(page_title="Đài Quan sát Đồng hồ Khí hậu", layout="wide")
st.title("🌍 Đài Quan sát Đồng hồ Khí hậu Thời gian Thực")
# -------------------------------
# Show Last Updated Timestamp
# -------------------------------
try:
file_stats = os.stat("climate_data.json")
last_updated = datetime.fromtimestamp(file_stats.st_mtime).strftime("%Y-%m-%d %H:%M:%S")
st.caption(f"🕒 Data last updated: {last_updated}")
except:
st.caption("🕒 Last updated time not available")
# -------------------------------
# Refresh Button
# -------------------------------
if st.button("🔁 Refresh Data"):
with st.spinner("Refreshing data..."):
st.experimental_rerun()
# -------------------------------
# Load Climate Data from JSON
# -------------------------------
try:
with open("climate_data.json", "r") as f:
clock = json.load(f)
except Exception as e:
st.error("❌ Failed to load climate data.")
st.stop()
# -------------------------------
# 1️⃣ CO₂ Budget Depletion Projection
# -------------------------------
st.header("1️⃣ Dự báo Cạn kiệt Ngân quỹ CO₂ ")
co2_data = clock.get("co2", {})
co2_remaining = float(co2_data.get("remaining", 0))
co2_rate = float(co2_data.get("rate", 0))
years = list(range(datetime.now().year, datetime.now().year + 10))
remaining_budget = [max(co2_remaining - i * co2_rate, 0) for i in range(10)]
co2_df = pd.DataFrame({"Year": years, "Remaining CO₂ Budget (Gt)": remaining_budget})
co2_chart = alt.Chart(co2_df).mark_line(point=True).encode(
x='Year:O',
y='Remaining CO₂ Budget (Gt):Q',
tooltip=['Year', 'Remaining CO₂ Budget (Gt)']
).properties(width=700)
st.altair_chart(co2_chart, use_container_width=True)
# -------------------------------
# 2️⃣ Global Energy Mix Donut Chart
# -------------------------------
st.header("2️⃣ Tổng hợp Năng lượng Toàn cầu – Năng lượng tái tạo vs Các loại khác")
renew_data = clock.get("renewables", {})
renew_percent = float(renew_data.get("percentage", 0))
energy_df = pd.DataFrame({
"Type": ["Renewables", "Other"],
"Percentage": [renew_percent, 100 - renew_percent]
})
energy_chart = alt.Chart(energy_df).mark_arc(innerRadius=50).encode(
theta="Percentage:Q",
color="Type:N",
tooltip=["Type", "Percentage"]
).properties(width=400, height=400)
st.altair_chart(energy_chart, use_container_width=True)
# -------------------------------
# 3️⃣ Lifeline Metrics Bar Chart
# -------------------------------
st.header("3️⃣ Chỉ số Lifeline – Những đóng góp tích cực")
lifelines = clock.get("lifelines", [])
lifeline_df = pd.DataFrame([
{"Label": item.get("label", ""), "Value": float(item.get("value", 0))}
for item in lifelines
])
lifeline_chart = alt.Chart(lifeline_df).mark_bar().encode(
x=alt.X("Label:N", sort="-y"),
y="Value:Q",
tooltip=["Label", "Value"]
).properties(width=700)
st.altair_chart(lifeline_chart, use_container_width=True)
# -------------------------------
# 4️⃣ Time Left to 1.5°C Threshold
# -------------------------------
st.header("4️⃣ Thời gian còn lại đến ngưỡng 1.5°C ")
deadline = clock.get("deadline", {})
time_parts = deadline.get("time_left", "0:0:0:0:0").split(":")
try:
years, months, days = int(time_parts[0]), int(time_parts[1]), int(time_parts[2])
st.success(f"⏳ Estimated time before 1.5°C threshold: {years} years, {months} months, {days} days")
except:
st.warning("⚠️ Could not parse climate deadline.")
# -------------------------------
# 5️⃣ CO₂ Budget Simulator with Slider
# -------------------------------
st.header("5️⃣ Mô phỏng Ngân quỹ CO₂ – Sẽ như thế nào nếu chúng ta giảm phát thải?")
st.markdown("📉 Sử dụng thanh trượt bên dưới để mô phỏng lượng khí thải hàng năm được giảm và hình dung tác động của nó.")
new_rate = st.slider(
"New Annual CO₂ Emission Rate (Gt/year)",
min_value=10.0,
max_value=45.0,
value=co2_rate,
step=0.5
)
sim_years = []
sim_budget = []
current_budget = co2_remaining
year = datetime.now().year
while current_budget > 0 and len(sim_years) < 20:
sim_years.append(year)
sim_budget.append(current_budget)
current_budget -= new_rate
year += 1
sim_df = pd.DataFrame({
"Year": sim_years,
"Projected CO₂ Budget (Gt)": sim_budget
})
sim_chart = alt.Chart(sim_df).mark_line(point=True).encode(
x="Year:O",
y="Projected CO₂ Budget (Gt):Q",
tooltip=["Year", "Projected CO₂ Budget (Gt)"]
).properties(width=700)
st.altair_chart(sim_chart, use_container_width=True)
# -------------------------------
# Footer
# -------------------------------
st.markdown("---")
st.caption("Built and Powered by Mạng lưới GXS | Data from Climate Clock API (cached sample)")