ESMATUGBA commited on
Commit
2aa88ae
·
verified ·
1 Parent(s): 663e322

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +37 -18
src/streamlit_app.py CHANGED
@@ -7,22 +7,39 @@ import seaborn as sns
7
  st.set_page_config(layout="wide", page_title="Gold Analysis Dashboard")
8
  plt.style.use('dark_background')
9
 
10
- # 1. VERİ YÜKLEME (Dosya ismi düzeltildi)
11
  @st.cache_data
12
  def load_data():
13
- # Hugging Face'teki dosya adın tam olarak bu:
14
  df = pd.read_csv("gold_feature_engineered.csv")
15
- df['Date'] = pd.to_datetime(df['Date'])
16
- df['YEAR'] = df['Date'].dt.year
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  return df
18
 
 
19
  try:
20
  df = load_data()
21
  except Exception as e:
22
- st.error(f"Dosya yükleme hatası: {e}")
23
  st.stop()
24
 
25
- # 2. SOL PANEL (SIDEBAR) - TIKLAYARAK SEÇME
26
  st.sidebar.markdown("# 📅 Filtreler / Filters")
27
  years_list = sorted([y for y in df['YEAR'].unique() if y >= 2008])
28
 
@@ -34,7 +51,7 @@ year = st.sidebar.selectbox(
34
 
35
  filtered_df = df[df['YEAR'] == year]
36
 
37
- # 3. ANA BAŞLIK (BÜYÜK BOYUT)
38
  st.markdown("<h1 style='text-align: center;'>Gold Price Analysis / Altın Fiyat Analizi</h1>", unsafe_allow_html=True)
39
  st.divider()
40
 
@@ -55,20 +72,21 @@ else:
55
 
56
  st.divider()
57
 
58
- # 5. ANA GRAFİK: ZAMAN SERİSİ (KÜÇÜLTÜLMÜŞ)
59
  st.markdown(f"## 📈 {year} Analizi / Analysis")
60
 
61
- fig1, ax1 = plt.subplots(figsize=(8, 2.5))
62
- ax1.plot(filtered_df['Date'], filtered_df['GLD'], color='gold', linewidth=1.5)
63
- ax1.fill_between(filtered_df['Date'], filtered_df['GLD'], color='gold', alpha=0.1)
 
64
 
65
- # Eksen ayarı (Basıklığı önlemek için veriye odaklanır)
66
- y_min, y_max = filtered_df['GLD'].min(), filtered_df['GLD'].max()
67
- ax1.set_ylim(y_min * 0.98, y_max * 1.02)
68
-
69
- st.pyplot(fig1)
 
70
 
71
- # GRAFİK NOTU
72
  st.markdown(f"#### 📝 Not: Bu grafik {year} yılındaki günlük fiyat hareketlerini gösterir. / Note: This chart shows daily price movements in {year}.")
73
 
74
  st.divider()
@@ -85,7 +103,8 @@ with col_left:
85
  with col_right:
86
  st.markdown("### 🌡️ Korelasyon / Correlation")
87
  fig3, ax3 = plt.subplots(figsize=(5, 3))
88
- sns.heatmap(filtered_df.corr(numeric_only=True), annot=True, cmap='YlOrBr', ax=ax3, annot_kws={"size": 7})
 
89
  st.pyplot(fig3)
90
 
91
  # 7. VERİ TABLOSU
 
7
  st.set_page_config(layout="wide", page_title="Gold Analysis Dashboard")
8
  plt.style.use('dark_background')
9
 
10
+ # 1. VERİ YÜKLEME (Hata Giderilmiş Esnek Versiyon)
11
  @st.cache_data
12
  def load_data():
13
+ # Hugging Face'teki dosya adın
14
  df = pd.read_csv("gold_feature_engineered.csv")
15
+
16
+ # Tarih sütununu bul (date, Date, DATE veya 0. sütun)
17
+ date_col = None
18
+ for col in df.columns:
19
+ if col.lower() == 'date':
20
+ date_col = col
21
+ break
22
+
23
+ if date_col is None:
24
+ date_col = df.columns[0] # Bulamazsa ilk sütunu tarih varsay
25
+
26
+ # Tarih dönüşümü ve Yıl sütunu oluşturma
27
+ df[date_col] = pd.to_datetime(df[date_col])
28
+ df['YEAR'] = df[date_col].dt.year
29
+
30
+ # Kodun geri kalanında hata çıkmaması için sütun adını 'Date' yapalım
31
+ df = df.rename(columns={date_col: 'Date'})
32
+
33
  return df
34
 
35
+ # Veriyi çek, hata varsa ekrana yazdır
36
  try:
37
  df = load_data()
38
  except Exception as e:
39
+ st.error(f"⚠️ Veri yüklenirken bir hata oluştu: {e}")
40
  st.stop()
41
 
42
+ # 2. SOL PANEL (SIDEBAR)
43
  st.sidebar.markdown("# 📅 Filtreler / Filters")
44
  years_list = sorted([y for y in df['YEAR'].unique() if y >= 2008])
45
 
 
51
 
52
  filtered_df = df[df['YEAR'] == year]
53
 
54
+ # 3. ANA BAŞLIK
55
  st.markdown("<h1 style='text-align: center;'>Gold Price Analysis / Altın Fiyat Analizi</h1>", unsafe_allow_html=True)
56
  st.divider()
57
 
 
72
 
73
  st.divider()
74
 
75
+ # 5. ANA GRAFİK: ZAMAN SERİSİ
76
  st.markdown(f"## 📈 {year} Analizi / Analysis")
77
 
78
+ if not filtered_df.empty:
79
+ fig1, ax1 = plt.subplots(figsize=(8, 2.5))
80
+ ax1.plot(filtered_df['Date'], filtered_df['GLD'], color='gold', linewidth=1.5)
81
+ ax1.fill_between(filtered_df['Date'], filtered_df['GLD'], color='gold', alpha=0.1)
82
 
83
+ # Odaklanma ayarı
84
+ y_min, y_max = filtered_df['GLD'].min(), filtered_df['GLD'].max()
85
+ ax1.set_ylim(y_min * 0.98, y_max * 1.02)
86
+ st.pyplot(fig1)
87
+ else:
88
+ st.write("Seçilen yıl için veri bulunamadı.")
89
 
 
90
  st.markdown(f"#### 📝 Not: Bu grafik {year} yılındaki günlük fiyat hareketlerini gösterir. / Note: This chart shows daily price movements in {year}.")
91
 
92
  st.divider()
 
103
  with col_right:
104
  st.markdown("### 🌡️ Korelasyon / Correlation")
105
  fig3, ax3 = plt.subplots(figsize=(5, 3))
106
+ # Sadece sayısal sütunlarla korelasyon
107
+ sns.heatmap(filtered_df.select_dtypes(include=['number']).corr(), annot=True, cmap='YlOrBr', ax=ax3, annot_kws={"size": 7})
108
  st.pyplot(fig3)
109
 
110
  # 7. VERİ TABLOSU