ESMATUGBA commited on
Commit
ec823d3
·
verified ·
1 Parent(s): 5e70003

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +55 -62
src/streamlit_app.py CHANGED
@@ -1,89 +1,82 @@
1
  import streamlit as st
2
  import pandas as pd
 
3
  import plotly.graph_objects as go
4
- import matplotlib.pyplot as plt
5
 
6
  # Sayfa Ayarları
7
  st.set_page_config(page_title="World Data Analysis", layout="wide")
8
 
9
- # --- SIDEBAR (SOL TARAF) - SENİN METNİN ---
10
- st.sidebar.header("📊 Analysis Notes / Analiz Notları")
11
-
12
- # İngilizce Notlar
13
- st.sidebar.markdown("""
14
- **EN:** Analysis shows that developed countries have high GDP and internet usage. It has also been observed that CO2 emissions are high in these countries. In low-income countries, energy and internet usage are found to be lower. In some cases, an inverse relationship between forest rate and CO2 emissions has been seen.
15
-
16
- **Examples:**
17
- * **High GDP & CO2:** USA, China, Germany.
18
- * **Low Income & Energy:** Afghanistan, Yemen, Zambia.
19
- """)
20
-
21
- st.sidebar.markdown("---") # Ayırıcı çizgi
22
-
23
- # Türkçe Notlar
24
- st.sidebar.markdown("""
25
- **TR:** Analiz sonucunda gelişmiş ülkelerde GDP ve internet kullanımının yüksek olduğu görülmüştür. Aynı zamanda bu ülkelerde CO2 emisyonunun da yüksek olduğu tespit edilmiştir. Düşük gelirli ülkelerde ise enerji ve internet kullanımının daha düşük olduğu gözlemlenmiştir. Orman oranı ile CO2 emisyonu arasında bazı durumlarda ters ilişki olduğu görülmüştür.
26
-
27
- **Örnekler:**
28
- * **Yüksek GDP & CO2:** ABD (USA), Çin (China), Almanya.
29
- * **Düşük Gelir & Enerji:** Afganistan, Yemen, Zambiya.
30
- """)
31
 
32
  # --- DATA LOAD ---
33
- try:
 
34
  df = pd.read_csv("us_pollution_cleaned.csv")
35
- except:
36
- st.error("Dosya bulunamadı! / File not found!")
37
- st.stop()
38
-
39
- # Sütun isimlerini veriye göre eşleştirme
40
- df = df.rename(columns={
41
- 'country': 'COUNTRY',
42
- 'GDP: Gross domestic product (million current US$)': 'GDP',
43
- 'CO2 emission estimates (million tons/tons per capita)': 'CO2'
44
- })
45
-
46
- # GDP_LEVEL Sütununu Güncelleme
47
- if 'GDP' in df.columns:
48
  df['GDP_LEVEL'] = pd.cut(df['GDP'], bins=[0, 50000, 1000000, 30000000], labels=['Low', 'Medium', 'High'])
 
 
 
49
 
50
- # --- ANA SAYFA BAŞLIK ---
51
- st.title("🌍 World Data Analysis Dashboard / Dünya Veri Analizi Paneli")
52
- st.write("Veri Analizi ve Görselleştirme / Data Analysis and Visualization")
53
  st.divider()
54
 
55
- # --- 1. HARİTA ---
56
  st.subheader("🗺️ World GDP Map / Dünya GSYİH Haritası")
57
- fig_map = go.Figure(data=dict(
58
- type="choropleth",
59
- colorscale='Viridis',
60
- locations=df["COUNTRY"],
61
  locationmode="country names",
62
- z=df["GDP"],
63
- colorbar={'title': "GDP"}
64
- ))
65
- fig_map.update_layout(height=400, margin={"r":0,"t":0,"l":0,"b":0})
66
  st.plotly_chart(fig_map, use_container_width=True)
67
 
68
  st.divider()
69
 
70
- # --- 2. ALT GRAFİKLER (YAN YANA) ---
71
  col1, col2 = st.columns(2)
72
 
73
  with col1:
74
- st.subheader("📊 GDP vs CO2")
75
- fig1, ax1 = plt.subplots(figsize=(5, 4))
76
- ax1.scatter(df['GDP'], df['CO2'], color='royalblue', alpha=0.6, s=20)
77
- ax1.set_xlabel("GDP")
78
- ax1.set_ylabel("CO2")
79
- st.pyplot(fig1)
 
80
 
81
  with col2:
82
- st.subheader("Pie Chart / Gelir Seviyesi Dağılımı")
83
- fig2, ax2 = plt.subplots(figsize=(5, 4))
84
- df_pie = df['GDP_LEVEL'].value_counts()
85
- ax2.pie(df_pie, labels=df_pie.index, autopct='%1.1f%%', colors=['#ff9999','#66b3ff','#99ff99'])
86
- st.pyplot(fig2)
 
87
 
88
  st.divider()
89
- st.write("✅ Proje tamamlandı / Project completed")
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import plotly.express as px
4
  import plotly.graph_objects as go
 
5
 
6
  # Sayfa Ayarları
7
  st.set_page_config(page_title="World Data Analysis", layout="wide")
8
 
9
+ # --- SIDEBAR (SOL PANEL) ---
10
+ with st.sidebar:
11
+ st.header("📊 Analysis Notes / Analiz Notları")
12
+ st.markdown("""
13
+ **EN:** Analysis shows that developed countries have high GDP and internet usage. It has also been observed that CO2 emissions are high in these countries. In low-income countries, energy and internet usage are found to be lower. In some cases, an inverse relationship between forest rate and CO2 emissions has been seen.
14
+
15
+ **Examples:**
16
+ * **High GDP & CO2:** USA, China, Germany.
17
+ * **Low Income & Energy:** Afghanistan, Yemen, Zambia.
18
+ """)
19
+ st.divider()
20
+ st.markdown("""
21
+ **TR:** Analiz sonucunda gelişmiş ülkelerde GDP ve internet kullanımının yüksek olduğu görülmüştür. Aynı zamanda bu ülkelerde CO2 emisyonunun da yüksek olduğu tespit edilmiştir. Düşük gelirli ülkelerde ise enerji ve internet kullanımının daha düşük olduğu gözlemlenmiştir. Orman oranı ile CO2 emisyonu arasında bazı durumlarda ters ilişki olduğu görülmüştür.
22
+
23
+ **Örnekler:**
24
+ * **Yüksek GDP & CO2:** ABD (USA), Çin (China), Almanya.
25
+ * **Düşük Gelir & Enerji:** Afganistan, Yemen, Zambiya.
26
+ """)
 
 
 
 
27
 
28
  # --- DATA LOAD ---
29
+ @st.cache_data # Titremeyi engellemek için veriyi hafızaya alır
30
+ def load_data():
31
  df = pd.read_csv("us_pollution_cleaned.csv")
32
+ # Sütun isimlerini kısaltma
33
+ df = df.rename(columns={
34
+ 'country': 'COUNTRY',
35
+ 'GDP: Gross domestic product (million current US$)': 'GDP',
36
+ 'CO2 emission estimates (million tons/tons per capita)': 'CO2'
37
+ })
38
+ # GDP_LEVEL oluşturma
 
 
 
 
 
 
39
  df['GDP_LEVEL'] = pd.cut(df['GDP'], bins=[0, 50000, 1000000, 30000000], labels=['Low', 'Medium', 'High'])
40
+ return df
41
+
42
+ df = load_data()
43
 
44
+ # --- ANA PANEL ---
45
+ st.title("🌍 World Data Analysis Dashboard")
46
+ st.caption("Veri Analizi ve Görselleştirme / Data Analysis and Visualization")
47
  st.divider()
48
 
49
+ # 1. HARİTA (Plotly Express - Daha hızlı ve etkileşimli)
50
  st.subheader("🗺️ World GDP Map / Dünya GSYİH Haritası")
51
+ fig_map = px.choropleth(df,
52
+ locations="COUNTRY",
 
 
53
  locationmode="country names",
54
+ color="GDP",
55
+ color_continuous_scale='Viridis',
56
+ height=450)
 
57
  st.plotly_chart(fig_map, use_container_width=True)
58
 
59
  st.divider()
60
 
61
+ # 2. ALT GRAFİKLER (Yan Yana ve İnteraktif)
62
  col1, col2 = st.columns(2)
63
 
64
  with col1:
65
+ st.subheader("📊 GDP vs CO2 (Interactive)")
66
+ # Artık noktaların üzerine gelebilirsin!
67
+ fig_scatter = px.scatter(df, x="GDP", y="CO2",
68
+ color="GDP_LEVEL",
69
+ hover_name="COUNTRY",
70
+ template="plotly_white")
71
+ st.plotly_chart(fig_scatter, use_container_width=True)
72
 
73
  with col2:
74
+ st.subheader("🍕 GDP Level Distribution")
75
+ # Pasta grafiği de artık tıklanabilir
76
+ fig_pie = px.pie(df, names='GDP_LEVEL', values='GDP',
77
+ color_discrete_sequence=px.colors.sequential.RdBu,
78
+ hole=0.3)
79
+ st.plotly_chart(fig_pie, use_container_width=True)
80
 
81
  st.divider()
82
+ st.write("✅ Proje tamamlandı. Tüm grafikler etkileşimlidir.")