ESMATUGBA commited on
Commit
7b3604a
·
verified ·
1 Parent(s): ebcbda5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -67
app.py CHANGED
@@ -1,13 +1,11 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
4
- import seaborn as sns
5
 
6
- # Sayfa Ayarları
7
  st.set_page_config(layout="wide", page_title="Gold Analysis Dashboard")
8
- plt.style.use('dark_background')
9
 
10
- # 1. VERİ YÜKLEME
11
  @st.cache_data(show_spinner=False)
12
  def load_data():
13
  df = pd.read_csv("gold_feature_engineered.csv")
@@ -20,84 +18,77 @@ def load_data():
20
  try:
21
  df = load_data()
22
  except Exception as e:
23
- st.error(f"⚠️ Hata: {e}")
24
  st.stop()
25
 
26
- # 2. SOL PANEL (SIDEBAR)
27
  st.sidebar.markdown("# 📅 Filtreler / Filters")
28
  years_list = sorted([y for y in df['YEAR'].unique() if y >= 2008])
29
-
30
- year = st.sidebar.selectbox(
31
- "Yıl Seçin / Select Year",
32
- options=years_list,
33
- index=0
34
- )
35
 
36
  filtered_df = df[df['YEAR'] == year].copy()
37
 
38
- # 3. ANA BAŞLIK
39
  st.markdown("<h1 style='text-align: center;'>Gold Price Analysis / Altın Fiyat Analizi</h1>", unsafe_allow_html=True)
40
  st.divider()
41
 
42
- # 4. YÖNETİCİ BİLGİ NOTU
43
  st.markdown("## 💼 Yönetici Bilgi Notu / Executive Summary")
44
  info_col1, info_col2 = st.columns(2)
45
 
46
- if year == 2008:
47
- with info_col1:
48
  st.warning("### 🇹🇷 2008'de altın güvenli limandır, alım faydalıdır.")
49
- with info_col2:
50
- st.warning("### 🇺🇸 Gold is a safe haven in 2008; buying is beneficial.")
51
- else:
52
- with info_col1:
53
  st.success(f"### 🇹🇷 {year} yılı verileri istikrarlı görünüyor.")
54
- with info_col2:
 
 
 
 
55
  st.success(f"### 🇺🇸 Data for {year} appears stable.")
56
 
57
  st.divider()
58
 
59
- # 5. GRAFİKLER VE TABLO (Titremeyi önleyen Fragment yapısı)
60
- @st.fragment
61
- def render_analysis(data, selected_year):
62
- st.markdown(f"## 📈 {selected_year} Analizi / Analysis")
63
-
64
- # Ana Grafik
65
- fig1, ax1 = plt.subplots(figsize=(8, 2.8))
66
- ax1.plot(data['Date'], data['GLD'], color='gold', linewidth=2)
67
- ax1.fill_between(data['Date'], data['GLD'], color='gold', alpha=0.1)
68
- y_min, y_max = data['GLD'].min(), data['GLD'].max()
69
- ax1.set_ylim(y_min * 0.98, y_max * 1.02)
70
- st.pyplot(fig1)
71
- plt.close(fig1)
72
-
73
- st.markdown(f"#### 📝 Not: Bu grafik {selected_year} yılındaki günlük fiyat hareketlerini gösterir.")
74
- st.divider()
75
-
76
- # Yan Yana Grafikler
77
- c1, c2 = st.columns(2)
78
- with c1:
79
- st.markdown("### 🎯 Dağılım / Distribution")
80
- fig2, ax2 = plt.subplots(figsize=(5, 3.5))
81
- sns.kdeplot(x=data['GLD'], fill=True, color="orange", ax=ax2)
82
- st.pyplot(fig2)
83
- plt.close(fig2)
84
- with c2:
85
- st.markdown("### 🌡️ Korelasyon / Correlation")
86
- fig3, ax3 = plt.subplots(figsize=(5, 3.5))
87
- num_df = data.select_dtypes(include=['number'])
88
- sns.heatmap(num_df.corr(), annot=True, cmap='YlOrBr', ax=ax3, annot_kws={"size": 8})
89
- st.pyplot(fig3)
90
- plt.close(fig3)
91
-
92
- st.divider()
93
-
94
- # Veri Tablosu (Loglardaki width hatası için width='stretch' eklendi)
95
- st.markdown("### 📋 Veri Tablosu / Data Table")
96
- st.dataframe(data.head(10), width="stretch") # Logdaki hatayı bu düzeltecek
97
-
98
- csv = data.to_csv(index=False).encode('utf-8')
99
- st.download_button("📥 Veriyi İndir / Download CSV", data=csv, file_name=f'gold_{selected_year}.csv')
100
-
101
- # Fonksiyonu çağır
102
- render_analysis(filtered_df, year)
103
-
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import plotly.express as px
 
4
 
5
+ # 1. SAYFA AYARLARI
6
  st.set_page_config(layout="wide", page_title="Gold Analysis Dashboard")
 
7
 
8
+ # 2. VERİ YÜKLEME
9
  @st.cache_data(show_spinner=False)
10
  def load_data():
11
  df = pd.read_csv("gold_feature_engineered.csv")
 
18
  try:
19
  df = load_data()
20
  except Exception as e:
21
+ st.error(f"Veri yükleme hatası: {e}")
22
  st.stop()
23
 
24
+ # 3. SIDEBAR (SOL PANEL)
25
  st.sidebar.markdown("# 📅 Filtreler / Filters")
26
  years_list = sorted([y for y in df['YEAR'].unique() if y >= 2008])
27
+ year = st.sidebar.selectbox("Yıl Seçin / Select Year", options=years_list, index=0)
 
 
 
 
 
28
 
29
  filtered_df = df[df['YEAR'] == year].copy()
30
 
31
+ # 4. ANA BAŞLIK
32
  st.markdown("<h1 style='text-align: center;'>Gold Price Analysis / Altın Fiyat Analizi</h1>", unsafe_allow_html=True)
33
  st.divider()
34
 
35
+ # 5. YÖNETİCİ BİLGİ NOTU
36
  st.markdown("## 💼 Yönetici Bilgi Notu / Executive Summary")
37
  info_col1, info_col2 = st.columns(2)
38
 
39
+ with info_col1:
40
+ if year == 2008:
41
  st.warning("### 🇹🇷 2008'de altın güvenli limandır, alım faydalıdır.")
42
+ else:
 
 
 
43
  st.success(f"### 🇹🇷 {year} yılı verileri istikrarlı görünüyor.")
44
+
45
+ with info_col2:
46
+ if year == 2008:
47
+ st.warning("### 🇺🇸 Gold is a safe haven in 2008; buying is beneficial.")
48
+ else:
49
  st.success(f"### 🇺🇸 Data for {year} appears stable.")
50
 
51
  st.divider()
52
 
53
+ # 6. GRAFİKLER (PLOTLY İLE TİTREMESİZ)
54
+ st.markdown(f"## 📈 {year} Analizi / Analysis")
55
+
56
+ # Ana Grafik (Plotly)
57
+ fig1 = px.line(filtered_df, x='Date', y='GLD',
58
+ title=f"{year} Altın Fiyat Hareketi",
59
+ color_discrete_sequence=['gold'],
60
+ template="plotly_dark")
61
+ fig1.update_layout(height=400, margin=dict(l=20, r=20, t=40, b=20))
62
+ st.plotly_chart(fig1, use_container_width=True)
63
+
64
+ st.markdown(f"#### 📝 Not: Bu grafik {year} yılındaki günlük fiyat hareketlerini gösterir.")
65
+ st.divider()
66
+
67
+ # Yan Yana Grafikler
68
+ c1, c2 = st.columns(2)
69
+
70
+ with c1:
71
+ st.markdown("### 🎯 Dağılım / Distribution")
72
+ fig2 = px.histogram(filtered_df, x="GLD", nbins=30,
73
+ marginal="rug", color_discrete_sequence=['orange'],
74
+ template="plotly_dark")
75
+ fig2.update_layout(height=350)
76
+ st.plotly_chart(fig2, use_container_width=True)
77
+
78
+ with c2:
79
+ st.markdown("### 🌡️ Korelasyon / Correlation")
80
+ num_df = filtered_df.select_dtypes(include=['number']).corr()
81
+ fig3 = px.imshow(num_df, text_auto=True, color_continuous_scale='YlOrBr',
82
+ template="plotly_dark")
83
+ fig3.update_layout(height=350)
84
+ st.plotly_chart(fig3, use_container_width=True)
85
+
86
+ st.divider()
87
+
88
+ # 7. VERİ TABLOSU (Her Zaman Açık ve Hatasız)
89
+ st.markdown("### 📋 Veri Tablosu / Data Table")
90
+ # Loglardaki width hatasını düzeltmek için width="stretch" yerine otomatik yapıya geçtik
91
+ st.dataframe(filtered_df.head(10), use_container_width=True)
92
+
93
+ csv = filtered_df.to_csv(index=False).encode('utf-8')
94
+ st.download_button("📥 Veriyi İndir / Download CSV", data=csv, file_name=f'gold_{year}.csv')