Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,268 +1,436 @@
|
|
1 |
-
from flask import Flask, render_template, request, jsonify
|
2 |
-
import pandas as pd
|
3 |
-
import numpy as np
|
4 |
-
import pickle
|
5 |
-
import os
|
6 |
-
from datetime import datetime, timedelta
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
import
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
return
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
df =
|
38 |
-
df
|
39 |
-
df.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
return
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
result
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
'
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import pickle
|
5 |
+
import os
|
6 |
+
from datetime import datetime, timedelta
|
7 |
+
import plotly.graph_objects as go
|
8 |
+
import plotly.express as px
|
9 |
+
from plotly.subplots import make_subplots
|
10 |
+
import plotly.io as pio
|
11 |
+
|
12 |
+
app = Flask(__name__)
|
13 |
+
|
14 |
+
# Load model and scaler
|
15 |
+
def load_model_and_scaler():
|
16 |
+
try:
|
17 |
+
with open('model.pkl', 'rb') as model_file:
|
18 |
+
model = pickle.load(model_file)
|
19 |
+
|
20 |
+
with open('scaler.pkl', 'rb') as scaler_file:
|
21 |
+
scaler = pickle.load(scaler_file)
|
22 |
+
|
23 |
+
return model, scaler
|
24 |
+
except FileNotFoundError:
|
25 |
+
return None, None
|
26 |
+
|
27 |
+
def load_data():
|
28 |
+
"""Load and preprocess the gold data"""
|
29 |
+
try:
|
30 |
+
data = pd.read_csv('gold.csv')
|
31 |
+
data['date'] = pd.to_datetime(data['date'], format='%d/%m/%Y').dt.strftime('%Y-%m-%d')
|
32 |
+
temp_df = data[['date', 'close', 'open']]
|
33 |
+
|
34 |
+
# Preprocessing
|
35 |
+
df = temp_df.copy()
|
36 |
+
df['date'] = pd.to_datetime(df['date'], dayfirst=True, format='%Y-%m-%d').dt.date
|
37 |
+
df.set_index('date', inplace=True)
|
38 |
+
df.index = pd.to_datetime(df.index)
|
39 |
+
df = df.sort_index()
|
40 |
+
|
41 |
+
return df
|
42 |
+
except FileNotFoundError:
|
43 |
+
return None
|
44 |
+
|
45 |
+
def normalize_data(df, scaler):
|
46 |
+
"""Normalize the data using the provided scaler"""
|
47 |
+
np_data_unscaled = np.array(df)
|
48 |
+
np_data_scaled = scaler.transform(np_data_unscaled)
|
49 |
+
normalized_df = pd.DataFrame(np_data_scaled, columns=df.columns, index=df.index)
|
50 |
+
return normalized_df
|
51 |
+
|
52 |
+
def sliding_window(data, lag):
|
53 |
+
"""Create sliding window features"""
|
54 |
+
series_close = data['close']
|
55 |
+
series_open = data['open']
|
56 |
+
result = pd.DataFrame()
|
57 |
+
|
58 |
+
# Add lag columns for 'close'
|
59 |
+
for l in lag:
|
60 |
+
result[f'close-{l}'] = series_close.shift(l)
|
61 |
+
|
62 |
+
# Add lag columns for 'open'
|
63 |
+
for l in lag:
|
64 |
+
result[f'open-{l}'] = series_open.shift(l)
|
65 |
+
|
66 |
+
# Add original 'close' and 'open' columns
|
67 |
+
result['close'] = series_close[max(lag):]
|
68 |
+
result['open'] = series_open[max(lag):]
|
69 |
+
|
70 |
+
# Remove missing values (NaN)
|
71 |
+
result = result.dropna()
|
72 |
+
|
73 |
+
# Set index according to lag values
|
74 |
+
result.index = series_close.index[max(lag):]
|
75 |
+
|
76 |
+
return result
|
77 |
+
|
78 |
+
def predict_next_7_days(model, scaler, data):
|
79 |
+
"""Predict gold prices for the next 7 days"""
|
80 |
+
# Normalize data
|
81 |
+
normalized_df = normalize_data(data, scaler)
|
82 |
+
|
83 |
+
# Create sliding window
|
84 |
+
windowed_data = sliding_window(normalized_df, [1, 2, 3, 4, 5, 6, 7])
|
85 |
+
windowed_data = windowed_data[['close', 'close-1', 'close-2', 'close-3', 'close-4', 'close-5', 'close-6', 'close-7',
|
86 |
+
'open', 'open-1', 'open-2', 'open-3', 'open-4', 'open-5', 'open-6', 'open-7']]
|
87 |
+
|
88 |
+
# Initialize predictions list
|
89 |
+
predictions = []
|
90 |
+
|
91 |
+
# Get last row as initial input
|
92 |
+
last_row = windowed_data.drop(columns=['close', 'open']).iloc[-1].values.reshape(1, -1)
|
93 |
+
|
94 |
+
# Iterate for 7 days
|
95 |
+
for _ in range(7):
|
96 |
+
# Predict value for next day
|
97 |
+
predicted_value_normalized = model.predict(last_row)
|
98 |
+
predicted_value = scaler.inverse_transform(predicted_value_normalized.reshape(-1, 2))
|
99 |
+
|
100 |
+
# Save prediction
|
101 |
+
predictions.append(predicted_value[0])
|
102 |
+
|
103 |
+
# Update input for next iteration
|
104 |
+
new_row_normalized = np.hstack([last_row[0, 2:], predicted_value_normalized[0]])
|
105 |
+
last_row = new_row_normalized.reshape(1, -1)
|
106 |
+
|
107 |
+
# Transform predictions to DataFrame
|
108 |
+
predictions_df = pd.DataFrame(
|
109 |
+
predictions,
|
110 |
+
columns=['close', 'open'],
|
111 |
+
index=pd.date_range(start=normalized_df.index[-1] + pd.Timedelta(days=1), periods=7)
|
112 |
+
)
|
113 |
+
|
114 |
+
# Get last price
|
115 |
+
last_price = scaler.inverse_transform(normalized_df[['close', 'open']].iloc[-1].values.reshape(-1, 2))
|
116 |
+
|
117 |
+
# Calculate daily percentage changes
|
118 |
+
predictions_df['close_change'] = predictions_df['close'].pct_change().fillna(0) * 100
|
119 |
+
predictions_df['open_change'] = predictions_df['open'].pct_change().fillna(0) * 100
|
120 |
+
|
121 |
+
# Calculate total change from today to day 7
|
122 |
+
total_close_change = ((predictions_df['close'].iloc[-1] - last_price[0][0]) / last_price[0][0]) * 100
|
123 |
+
total_open_change = ((predictions_df['open'].iloc[-1] - last_price[0][1]) / last_price[0][1]) * 100
|
124 |
+
|
125 |
+
return predictions_df, last_price[0], total_close_change, total_open_change
|
126 |
+
|
127 |
+
def create_prediction_chart(data, predictions_df, last_price):
|
128 |
+
"""Create an interactive chart showing historical and predicted prices using Plotly"""
|
129 |
+
|
130 |
+
# Get last 30 days of historical data
|
131 |
+
recent_data = data.tail(30)
|
132 |
+
|
133 |
+
# Create subplots
|
134 |
+
fig = make_subplots(
|
135 |
+
rows=1, cols=1,
|
136 |
+
subplot_titles=['Prediksi Harga Emas - 7 Hari Kedepan'],
|
137 |
+
x_title='Tanggal',
|
138 |
+
y_title='Harga (IDR)'
|
139 |
+
)
|
140 |
+
|
141 |
+
# Add historical close price
|
142 |
+
fig.add_trace(
|
143 |
+
go.Scatter(
|
144 |
+
x=recent_data.index,
|
145 |
+
y=recent_data['close'],
|
146 |
+
mode='lines',
|
147 |
+
name='Harga Tutup Historis',
|
148 |
+
line=dict(color='#1f77b4', width=2),
|
149 |
+
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
150 |
+
'<b>Harga Tutup:</b> IDR %{y:,.0f}<br>' +
|
151 |
+
'<extra></extra>'
|
152 |
+
)
|
153 |
+
)
|
154 |
+
|
155 |
+
# Add historical open price
|
156 |
+
fig.add_trace(
|
157 |
+
go.Scatter(
|
158 |
+
x=recent_data.index,
|
159 |
+
y=recent_data['open'],
|
160 |
+
mode='lines',
|
161 |
+
name='Harga Buka Historis',
|
162 |
+
line=dict(color='#2ca02c', width=2),
|
163 |
+
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
164 |
+
'<b>Harga Buka:</b> IDR %{y:,.0f}<br>' +
|
165 |
+
'<extra></extra>'
|
166 |
+
)
|
167 |
+
)
|
168 |
+
|
169 |
+
# Add current day points
|
170 |
+
fig.add_trace(
|
171 |
+
go.Scatter(
|
172 |
+
x=[recent_data.index[-1]],
|
173 |
+
y=[last_price[0]],
|
174 |
+
mode='markers',
|
175 |
+
name='Harga Tutup Saat Ini',
|
176 |
+
marker=dict(color='red', size=10),
|
177 |
+
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
178 |
+
'<b>Harga Tutup Saat Ini:</b> IDR %{y:,.0f}<br>' +
|
179 |
+
'<extra></extra>'
|
180 |
+
)
|
181 |
+
)
|
182 |
+
|
183 |
+
fig.add_trace(
|
184 |
+
go.Scatter(
|
185 |
+
x=[recent_data.index[-1]],
|
186 |
+
y=[last_price[1]],
|
187 |
+
mode='markers',
|
188 |
+
name='Harga Buka Saat Ini',
|
189 |
+
marker=dict(color='green', size=10),
|
190 |
+
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
191 |
+
'<b>Harga Buka Saat Ini:</b> IDR %{y:,.0f}<br>' +
|
192 |
+
'<extra></extra>'
|
193 |
+
)
|
194 |
+
)
|
195 |
+
|
196 |
+
# Add predicted close price
|
197 |
+
fig.add_trace(
|
198 |
+
go.Scatter(
|
199 |
+
x=predictions_df.index,
|
200 |
+
y=predictions_df['close'],
|
201 |
+
mode='lines+markers',
|
202 |
+
name='Prediksi Harga Tutup',
|
203 |
+
line=dict(color='red', width=2, dash='dash'),
|
204 |
+
marker=dict(size=6),
|
205 |
+
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
206 |
+
'<b>Prediksi Harga Tutup:</b> IDR %{y:,.0f}<br>' +
|
207 |
+
'<extra></extra>'
|
208 |
+
)
|
209 |
+
)
|
210 |
+
|
211 |
+
# Add predicted open price
|
212 |
+
fig.add_trace(
|
213 |
+
go.Scatter(
|
214 |
+
x=predictions_df.index,
|
215 |
+
y=predictions_df['open'],
|
216 |
+
mode='lines+markers',
|
217 |
+
name='Prediksi Harga Buka',
|
218 |
+
line=dict(color='orange', width=2, dash='dash'),
|
219 |
+
marker=dict(size=6, symbol='square'),
|
220 |
+
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
221 |
+
'<b>Prediksi Harga Buka:</b> IDR %{y:,.0f}<br>' +
|
222 |
+
'<extra></extra>'
|
223 |
+
)
|
224 |
+
)
|
225 |
+
|
226 |
+
# Update layout
|
227 |
+
fig.update_layout(
|
228 |
+
title={
|
229 |
+
'text': 'Prediksi Harga Emas - 7 Hari Kedepan',
|
230 |
+
'x': 0.5,
|
231 |
+
'xanchor': 'center',
|
232 |
+
'font': {'size': 20, 'family': 'Arial Black'}
|
233 |
+
},
|
234 |
+
xaxis_title='Tanggal',
|
235 |
+
yaxis_title='Harga (IDR)',
|
236 |
+
hovermode='x unified',
|
237 |
+
showlegend=True,
|
238 |
+
legend=dict(
|
239 |
+
orientation="h",
|
240 |
+
yanchor="bottom",
|
241 |
+
y=1.02,
|
242 |
+
xanchor="right",
|
243 |
+
x=1
|
244 |
+
),
|
245 |
+
plot_bgcolor='white',
|
246 |
+
paper_bgcolor='white',
|
247 |
+
font=dict(family="Arial", size=12),
|
248 |
+
height=500,
|
249 |
+
margin=dict(l=50, r=50, t=80, b=50)
|
250 |
+
)
|
251 |
+
|
252 |
+
# Update axes
|
253 |
+
fig.update_xaxes(
|
254 |
+
showgrid=True,
|
255 |
+
gridwidth=1,
|
256 |
+
gridcolor='lightgray',
|
257 |
+
showline=True,
|
258 |
+
linewidth=1,
|
259 |
+
linecolor='black'
|
260 |
+
)
|
261 |
+
|
262 |
+
fig.update_yaxes(
|
263 |
+
showgrid=True,
|
264 |
+
gridwidth=1,
|
265 |
+
gridcolor='lightgray',
|
266 |
+
showline=True,
|
267 |
+
linewidth=1,
|
268 |
+
linecolor='black',
|
269 |
+
tickformat=',.0f'
|
270 |
+
)
|
271 |
+
|
272 |
+
# Convert to HTML
|
273 |
+
html_string = pio.to_html(fig, include_plotlyjs='cdn', div_id="prediction-chart")
|
274 |
+
|
275 |
+
return html_string
|
276 |
+
|
277 |
+
@app.route('/')
|
278 |
+
def index():
|
279 |
+
return render_template('index.html')
|
280 |
+
|
281 |
+
@app.route('/predict', methods=['POST'])
|
282 |
+
def predict():
|
283 |
+
try:
|
284 |
+
# Load model and scaler
|
285 |
+
model, scaler = load_model_and_scaler()
|
286 |
+
if model is None or scaler is None:
|
287 |
+
return jsonify({'error': 'Model or scaler not found. Please train the model first.'}), 500
|
288 |
+
|
289 |
+
# Load data
|
290 |
+
data = load_data()
|
291 |
+
if data is None:
|
292 |
+
return jsonify({'error': 'Data file not found.'}), 500
|
293 |
+
|
294 |
+
# Make predictions
|
295 |
+
predictions_df, last_price, total_close_change, total_open_change = predict_next_7_days(model, scaler, data)
|
296 |
+
|
297 |
+
# Create chart
|
298 |
+
chart_img = create_prediction_chart(data, predictions_df, last_price)
|
299 |
+
|
300 |
+
# Prepare response data
|
301 |
+
predictions_list = []
|
302 |
+
for date, row in predictions_df.iterrows():
|
303 |
+
predictions_list.append({
|
304 |
+
'date': date.strftime('%Y-%m-%d'),
|
305 |
+
'close_price': round(row['close'], 2),
|
306 |
+
'open_price': round(row['open'], 2),
|
307 |
+
'close_change': round(row['close_change'], 2),
|
308 |
+
'open_change': round(row['open_change'], 2)
|
309 |
+
})
|
310 |
+
response = {
|
311 |
+
'success': True,
|
312 |
+
'current_prices': {
|
313 |
+
'close': round(last_price[0], 2),
|
314 |
+
'open': round(last_price[1], 2)
|
315 |
+
},
|
316 |
+
'predictions': predictions_list,
|
317 |
+
'total_changes': {
|
318 |
+
'close': round(total_close_change, 2),
|
319 |
+
'open': round(total_open_change, 2)
|
320 |
+
},
|
321 |
+
'chart_html': chart_img
|
322 |
+
}
|
323 |
+
|
324 |
+
return jsonify(response)
|
325 |
+
|
326 |
+
except Exception as e:
|
327 |
+
return jsonify({'error': f'An error occurred: {str(e)}'}), 500
|
328 |
+
|
329 |
+
@app.route('/data-analysis')
|
330 |
+
def data_analysis():
|
331 |
+
"""Show data analysis page"""
|
332 |
+
try:
|
333 |
+
data = load_data()
|
334 |
+
if data is None:
|
335 |
+
return render_template('error.html', error='Data file not found.')
|
336 |
+
# Create historical price chart with Plotly
|
337 |
+
fig = go.Figure()
|
338 |
+
|
339 |
+
# Add close price line
|
340 |
+
fig.add_trace(
|
341 |
+
go.Scatter(
|
342 |
+
x=data.index,
|
343 |
+
y=data['close'],
|
344 |
+
mode='lines',
|
345 |
+
name='Harga Tutup',
|
346 |
+
line=dict(color='#1f77b4', width=1.5),
|
347 |
+
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
348 |
+
'<b>Harga Tutup:</b> IDR %{y:,.0f}<br>' +
|
349 |
+
'<extra></extra>'
|
350 |
+
)
|
351 |
+
)
|
352 |
+
|
353 |
+
# Add open price line
|
354 |
+
fig.add_trace(
|
355 |
+
go.Scatter(
|
356 |
+
x=data.index,
|
357 |
+
y=data['open'],
|
358 |
+
mode='lines',
|
359 |
+
name='Harga Buka',
|
360 |
+
line=dict(color='#2ca02c', width=1.5),
|
361 |
+
hovertemplate='<b>Tanggal:</b> %{x}<br>' +
|
362 |
+
'<b>Harga Buka:</b> IDR %{y:,.0f}<br>' +
|
363 |
+
'<extra></extra>'
|
364 |
+
)
|
365 |
+
)
|
366 |
+
|
367 |
+
# Update layout
|
368 |
+
fig.update_layout(
|
369 |
+
title={
|
370 |
+
'text': 'Data Historis Harga Emas',
|
371 |
+
'x': 0.5,
|
372 |
+
'xanchor': 'center',
|
373 |
+
'font': {'size': 20, 'family': 'Arial Black'}
|
374 |
+
},
|
375 |
+
xaxis_title='Tanggal',
|
376 |
+
yaxis_title='Harga (IDR)',
|
377 |
+
hovermode='x unified',
|
378 |
+
showlegend=True,
|
379 |
+
legend=dict(
|
380 |
+
orientation="h",
|
381 |
+
yanchor="bottom",
|
382 |
+
y=1.02,
|
383 |
+
xanchor="right",
|
384 |
+
x=1
|
385 |
+
),
|
386 |
+
plot_bgcolor='white',
|
387 |
+
paper_bgcolor='white',
|
388 |
+
font=dict(family="Arial", size=12),
|
389 |
+
height=400,
|
390 |
+
margin=dict(l=50, r=50, t=80, b=50)
|
391 |
+
)
|
392 |
+
|
393 |
+
# Update axes
|
394 |
+
fig.update_xaxes(
|
395 |
+
showgrid=True,
|
396 |
+
gridwidth=1,
|
397 |
+
gridcolor='lightgray',
|
398 |
+
showline=True,
|
399 |
+
linewidth=1,
|
400 |
+
linecolor='black'
|
401 |
+
)
|
402 |
+
|
403 |
+
fig.update_yaxes(
|
404 |
+
showgrid=True,
|
405 |
+
gridwidth=1,
|
406 |
+
gridcolor='lightgray',
|
407 |
+
showline=True,
|
408 |
+
linewidth=1,
|
409 |
+
linecolor='black',
|
410 |
+
tickformat=',.0f'
|
411 |
+
)
|
412 |
+
|
413 |
+
# Convert to HTML
|
414 |
+
historical_chart = pio.to_html(fig, include_plotlyjs='cdn', div_id="historical-chart")
|
415 |
+
|
416 |
+
# Calculate statistics
|
417 |
+
stats = {
|
418 |
+
'total_records': len(data),
|
419 |
+
'date_range': f"{data.index.min().strftime('%Y-%m-%d')} to {data.index.max().strftime('%Y-%m-%d')}",
|
420 |
+
'avg_close': round(data['close'].mean(), 2),
|
421 |
+
'avg_open': round(data['open'].mean(), 2),
|
422 |
+
'min_close': round(data['close'].min(), 2),
|
423 |
+
'max_close': round(data['close'].max(), 2),
|
424 |
+
'current_close': round(data['close'].iloc[-1], 2),
|
425 |
+
'current_open': round(data['open'].iloc[-1], 2)
|
426 |
+
}
|
427 |
+
|
428 |
+
return render_template('data_analysis.html', chart=historical_chart, stats=stats)
|
429 |
+
|
430 |
+
except Exception as e:
|
431 |
+
return render_template('error.html', error=f'An error occurred: {str(e)}')
|
432 |
+
|
433 |
+
if __name__ == '__main__':
|
434 |
+
# For Hugging Face Spaces, use port 7860
|
435 |
+
port = int(os.environ.get('PORT', 7860))
|
436 |
+
app.run(host='0.0.0.0', port=port, debug=False)
|