-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (51 loc) · 1.64 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
from flask import Flask, render_template
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from io import BytesIO
import base64
from bs4 import BeautifulSoup
import requests
#don't change this
matplotlib.use('Agg')
app = Flask(__name__) #do not change this
#insert the scrapping here
url_get = requests.get('https://www.exchange-rates.org/history/IDR/USD/T')
soup = BeautifulSoup(url_get.content,"html.parser")
#find your right key here
table = soup.find('tbody')
extract = table.find_all('a')
row_length = len(extract)
temp = [] #initiating a list
for i in range(0, row_length, 2):
#insert the scrapping process here
harga = extract[i].text.replace(',','')
tanggal = extract[i+1].text.removeprefix("USD IDR rate for ")
temp.append((harga, tanggal))
temp = temp[::-1]
#change into dataframe
data = pd.DataFrame(temp, columns = ('harga harian','tanggal'))
#insert data wrangling here
data['tanggal'] = data['tanggal'].astype('datetime64')
data['harga harian'] = data['harga harian'].astype('float64')
tabel_final = data.set_index('tanggal')
#end of data wranggling
@app.route("/")
def index():
card_data = round(tabel_final['harga harian'].mean(), 4) #be careful with the " and '
# generate plot
ax = tabel_final.plot(figsize = (9,4))
# Rendering plot
# Do not change this
figfile = BytesIO()
plt.savefig(figfile, format='png', transparent=True)
figfile.seek(0)
figdata_png = base64.b64encode(figfile.getvalue())
plot_result = str(figdata_png)[2:-1]
# render to html
return render_template('index.html',
card_data = card_data,
plot_result=plot_result
)
if __name__ == "__main__":
app.run(debug=True)