ESMATUGBA commited on
Commit
85691dd
·
verified ·
1 Parent(s): 8b3f2cd

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +19 -30
src/streamlit_app.py CHANGED
@@ -3,40 +3,27 @@ import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
 
6
- # Sayfa Genişlik Ayarı
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)
@@ -49,7 +36,8 @@ year = st.sidebar.selectbox(
49
  index=0
50
  )
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)
@@ -79,13 +67,12 @@ 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
 
@@ -98,17 +85,19 @@ with col_left:
98
  st.markdown("### 🎯 Dağılım / Distribution")
99
  fig2, ax2 = plt.subplots(figsize=(5, 3))
100
  sns.kdeplot(x=filtered_df['GLD'], fill=True, color="orange", ax=ax2)
101
- st.pyplot(fig2)
102
 
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
111
  with st.expander("Verileri Gör / View Data"):
112
- st.dataframe(filtered_df.head(), use_container_width=True)
 
113
  csv = filtered_df.to_csv(index=False).encode('utf-8')
114
  st.download_button("📥 İndir / Download", data=csv, file_name=f'gold_{year}.csv')
 
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
 
6
+ # Sayfa Ayarları (Uyarıları engellemek ve hızı artırmak için)
7
  st.set_page_config(layout="wide", page_title="Gold Analysis Dashboard")
8
  plt.style.use('dark_background')
9
 
10
+ # 1. VERİ YÜKLEME (Titremeyi önlemek için cache mekanizması)
11
+ @st.cache_data(show_spinner=False) # Spinner'ı kapatmak titremeyi azaltır
12
  def load_data():
 
13
  df = pd.read_csv("gold_feature_engineered.csv")
14
 
15
+ # Tarih sütununu otomatik bul
16
+ date_col = next((col for col in df.columns if col.lower() == 'date'), df.columns[0])
 
 
 
 
17
 
 
 
 
 
18
  df[date_col] = pd.to_datetime(df[date_col])
19
  df['YEAR'] = df[date_col].dt.year
 
 
20
  df = df.rename(columns={date_col: 'Date'})
 
21
  return df
22
 
 
23
  try:
24
  df = load_data()
25
  except Exception as e:
26
+ st.error(f"⚠️ Hata: {e}")
27
  st.stop()
28
 
29
  # 2. SOL PANEL (SIDEBAR)
 
36
  index=0
37
  )
38
 
39
+ # Veriyi filtrele (Sadece bir kez yapıyoruz)
40
+ filtered_df = df[df['YEAR'] == year].copy()
41
 
42
  # 3. ANA BAŞLIK
43
  st.markdown("<h1 style='text-align: center;'>Gold Price Analysis / Altın Fiyat Analizi</h1>", unsafe_allow_html=True)
 
67
  fig1, ax1 = plt.subplots(figsize=(8, 2.5))
68
  ax1.plot(filtered_df['Date'], filtered_df['GLD'], color='gold', linewidth=1.5)
69
  ax1.fill_between(filtered_df['Date'], filtered_df['GLD'], color='gold', alpha=0.1)
70
+
 
71
  y_min, y_max = filtered_df['GLD'].min(), filtered_df['GLD'].max()
72
  ax1.set_ylim(y_min * 0.98, y_max * 1.02)
73
+ st.pyplot(fig1, clear_figure=True) # Hafızayı temizlemek titremeyi önler
74
  else:
75
+ st.write("Veri bulunamadı.")
76
 
77
  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}.")
78
 
 
85
  st.markdown("### 🎯 Dağılım / Distribution")
86
  fig2, ax2 = plt.subplots(figsize=(5, 3))
87
  sns.kdeplot(x=filtered_df['GLD'], fill=True, color="orange", ax=ax2)
88
+ st.pyplot(fig2, clear_figure=True)
89
 
90
  with col_right:
91
  st.markdown("### 🌡️ Korelasyon / Correlation")
92
  fig3, ax3 = plt.subplots(figsize=(5, 3))
93
+ # Sadece sayısal sütunları seçerek hızı artırıyoruz
94
+ num_df = filtered_df.select_dtypes(include=['number'])
95
+ sns.heatmap(num_df.corr(), annot=True, cmap='YlOrBr', ax=ax3, annot_kws={"size": 7})
96
+ st.pyplot(fig3, clear_figure=True)
97
 
98
+ # 7. VERİ TABLOSU VE İNDİRME
99
  with st.expander("Verileri Gör / View Data"):
100
+ # Logdaki uyarıyı gidermek için width='stretch' kullandık
101
+ st.dataframe(filtered_df.head(), width=None)
102
  csv = filtered_df.to_csv(index=False).encode('utf-8')
103
  st.download_button("📥 İndir / Download", data=csv, file_name=f'gold_{year}.csv')